liveBook · Manning

Over-fetching

As explained in Building Web APIs with ASP.NET Core

Over-fetching is the retrieval of more data fields or records than a client requires.

Over-fetching happens when an API client must retrieve more data than it needs. A board-game list might need only each game’s title and ID for a link, while a REST endpoint returns every property of every matching game. The extra traffic may barely matter for a small service but can become a performance problem at scale. section 10.1.1

There are several ways to limit the mismatch between a response and a client’s needs:

  • Exclude properties from JSON with `[JsonIgnore]`. This applies to every client, including clients that need those properties. section 10.1.1
  • Add endpoints backed by smaller DTOs. These can fit particular clients, but similar endpoints increase code and maintenance work. section 10.1.1
  • Use GraphQL so a client requests the fields it needs through one declarative query to one endpoint. Consider the cost of processing custom queries and the lack of native caching support. section 10.2.1 section 10.2.2

Check the returned fields

In BananaCakePop, run a board-games query requesting the first three games, ordered by ID, with only `id`, `name`, and `year` in each node. Check that the response contains those fields and no other board-game properties. A query can also request related domains and mechanics in the same call. section 10.2.4

A faster transport alone does not solve over-fetching. gRPC returns predefined data structures and has no native declarative query language for choosing response fields, so it can face the same problem as REST. section 10.3.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines the problem, compares mitigation trade-offs, and verifies selected fields in a GraphQL response; it does not quantify the traffic saved.
Examples
Uses a board-game list needing titles and IDs and a GraphQL query returning only requested board-game fields.
What it assumes
Introduces the term through familiar API requests and JSON properties rather than assuming a performance model.
What you can do after
Evaluate/understand when fixed API responses waste traffic and when field-selecting queries offer a useful alternative.
How it connects
Connects REST endpoints, DTOs, GraphQL field selection, gRPC response structures, and API performance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.