0%
API Design Interviews: REST, gRPC, GraphQL, Pagination, Idempotency, Versioning and Webhooks
Pagination, Filtering and Field Selection
Almost every API has list endpoints: the orders a customer has placed, the messages in a channel, the files in a folder, the transactions on an account. In step four of the framework you write them as GET on a collection, and in step five you decide the rules that every list follows. Those rules are pagination, sorting, filtering and field selection. They look like small details, but they decide whether a list endpoint stays fast for the biggest customer, whether a client can walk a changing feed without seeing duplicates, and whether the contract can be changed later without breaking callers. Interviewers know this, which is why a single list endpoint so often becomes a ten minute discussion.
This lesson covers the list rules in the order you would meet them in an interview: why pages exist at all, the three main pagination styles, how cursors should be built, how sorting, filtering and field selection fit in, and how search endpoints and total counts change the picture. Resource names and URL shapes belong to the resource modeling lesson, and the choice between REST, gRPC and GraphQL to the lesson after it. Here we assume a collection already exists and ask how a caller reads it safely.
The unbounded list problem
The simplest list endpoint returns everything. GET /orders reads every order for the user and serialises them into one response. In a demo with ten orders it is instant. Then a wholesale customer with two million orders calls it. The database reads two million rows, the server builds a response of several hundred megabytes, memory spikes, the load balancer times out after thirty or sixty seconds and the client gets a 504. Worse, the client retries, and now two of those queries are running. One large customer can degrade the service for everyone.
The fix is not to hope that big accounts are rare. It is to make every list endpoint bounded by design. A list returns at most a fixed number of items per call, and the response tells the client how to fetch the next batch. This rule holds even for collections that are small today, because collections grow and adding pagination later is a breaking change: clients that assumed they received everything will silently start missing data.
Page size defaults and maximums
Every list needs two numbers in its contract: a default page size and a maximum. A typical choice is a default of twenty to fifty items and a maximum of one hundred to one thousand, depending on item size. A client may ask for fewer or more within the maximum using a parameter such as limit or page_size. If they ask for more than the maximum, you have two reasonable options: clamp to the maximum and return that many, or reject with a 400 error. Clamping is friendlier, but the response must then make clear how many items were returned, so a client that asked for five thousand does not assume it has the whole set. Rejecting is stricter and surfaces bugs earlier. Either is fine in an interview if you say which and why.
The default matters more than it seems. Most clients never set the parameter, so the default is effectively the page size for the majority of traffic. Too small and every screen needs several round trips. Too large and every call carries data nobody shows. A good default matches the most common screen: twenty five rows for a dashboard table, fifty for a message history, ten for search results.
What a page response contains
A paginated response is an object, not a bare array. The object holds the items plus the information needed to continue. Wrapping the list in an object is itself an evolvability decision: it lets you add fields later, such as a total count or a warning, without changing the shape clients parse.
The essential parts are the items, a token or link for the next page, and a clear signal for the end of the list. Some APIs put the next link in an HTTP Link header instead of the body. That is a valid choice and keeps the body clean, but many client developers find body fields easier to read and log. Pick one and use it everywhere.
Who pays for a bad list design
It helps to list the costs out loud in an interview, because they connect pagination to the consumers you named in step one. Mobile clients on slow networks pay in latency and data for every item they do not show. The server pays in memory and CPU for serialising large responses. The database pays for every row it reads, including rows that are read and thrown away. Other tenants pay when one large query saturates a shared database. And the API team pays forever if the contract has no page limit, because adding one later breaks clients. A sentence such as every list is cursor paginated with a default of fifty and a maximum of two hundred, so no single call can read more than two hundred rows, covers all of these at once.
Lists that look small but are not
Interviewers like to probe lists that a candidate assumed were tiny. The members of a team look small, until an enterprise customer puts forty thousand employees in one workspace. The comments on a post look small, until a post goes viral. The line items of an order look small, until a business uploads a purchase order with ten thousand lines. A good habit is to ask, for every collection, what the largest realistic size is, and to paginate anything that can exceed a few hundred items. Nested lists inside a resource, such as the line items embedded in an order, are a common trap: the parent call looks bounded, but it embeds an unbounded child list. Either cap the embedded list and offer a separate paginated sub resource for the rest, or never embed it.
Where pagination appears in the framework
Pagination is a cross cutting concern, so it belongs in step five, but you will mention it first in step four when you write the list endpoints. The efficient pattern is to state the rule once and apply it everywhere: all list endpoints accept limit and cursor, return data, next_cursor and has_more, default to fifty and cap at two hundred. Then, in step five, go deeper only on the lists that stress the rule, such as a fast moving feed, a list sorted by a field that changes, or an export of millions of rows. Stating a single rule for the whole API is also evidence of consistency, one of the signals interviewers score.
Signalling the end of the list
Clients need an unambiguous way to know they have reached the end. The cleanest signal is has_more set to false together with a missing or null next_cursor. Avoid relying on a short page as the signal. A page can be shorter than the limit for reasons other than the end of the data, for example when the server filters out items the caller is not allowed to see after reading them, or when a scan budget runs out. If clients treat a short page as the end, they stop early and miss items. Saying explicitly that only has_more decides whether to continue removes that ambiguity, and it lets the server return short or even empty pages safely when it needs to.
Empty pages deserve a word of their own. A page with no items but has_more set to true is legal and sometimes necessary, for instance when the server examined a slice of a sparse index and found nothing visible to this caller. Clients must keep following the cursor. Documenting this up front saves partners from writing loops that quit at the first empty response.
A quick interview script for lists
When you reach the first list endpoint in an interview, a short script covers what interviewers look for. Name the largest realistic size of the collection. State the page size default and maximum. Say which pagination style you are using and why, in one sentence tied to the consumer. Name the default sort and the tie breaker. Mention the end signal. That is five sentences, under a minute, and it answers most follow up questions before they are asked. The rest of this lesson gives you the material behind each of those sentences, so that when the interviewer does push on one, you can go deeper with confidence.
Say once that every list endpoint takes limit and cursor, returns data, next_cursor and has_more, defaults to fifty and caps at two hundred. Then spend your time only on the lists that break the pattern.