API Pagination: Choose Between Offsets and Continuation
Pagination splits a large collection across multiple responses so a client can process a manageable portion and request the next page. API designers need to define ordering, behavior under concurrent changes, page size, and how clients continue; otherwise clients can miss or repeat records or request expensive pages.
Pagination splits a large collection across multiple responses so a client can process a manageable portion and request the next page. API designers need to define ordering, behavior under concurrent changes, page size, and how clients continue; otherwise clients can miss or repeat records or request expensive pages.
Why split a collection?
Returning every item at once can increase response size, server memory and processing, and network cost. Pagination lets clients consume results incrementally and limits response size. The Microsoft REST API Guidelines describe both client-driven paging with $top and $skip and server-driven continuation. These are recommendations in that guide, not a universal HTTP contract; each API defines its own parameter names and response shape.
Three ways to continue
| Method | How the next page is represented | Useful when | Trade-off |
|---|---|---|---|
| Offset | Starting position and size, such as limit=20&offset=40 | Collections are small and stable, and users need page-number navigation | Deep offsets can cost more; inserts or deletes before the offset can cause duplicates or omissions |
| Continuation token / cursor | An opaque token or next URL from the server | The server should control query state and simplify the client | Document token expiry and which query options must stay fixed; do not decode or alter an opaque token |
| Keyset | Continue after the last item’s sort key | Traversing large collections with stable sort keys | Requires a unique ordering or tie-breaker; arbitrary page-number jumps are difficult |
These approaches are not mutually exclusive. A public cursor can wrap an internal keyset position, and a continuation URL can carry filter and sort state. Distinguish the client-facing contract from the server’s query implementation.
Offsets and changing data
Suppose GET /items?limit=2&offset=0 returns [A, B]. If a new item X is inserted at the front before the next request, offset=2 may return [B, C], repeating B. If an earlier item is deleted, a later item can shift across the page boundary and be skipped. An offset identifies a position in the current ordered result, not a fixed record.
Use a deterministic unique sort. If several records share createdAt, add a unique ID as a tie-breaker. Microsoft’s guidance recommends stable ordering and documenting possible omissions or repeats as records are created or deleted. PostgreSQL also documents that LIMIT/OFFSET needs a unique ORDER BY for predictable subsets and that large offsets may be inefficient. Actual cost depends on the storage engine and query plan.
Follow continuation tokens safely
Server-driven pagination returns continuation information or a nextLink when another page is available. The API contract defines how the final page is represented. Microsoft’s guideline example uses an opaque @nextLink; clients should use the returned URL as given instead of editing it.
{
"value": [{"id": "item-41"}, {"id": "item-42"}],
"nextLink": "https://api.example.test/items?cursor=opaque-token"
}This is a synthetic response. Request nextLink according to the API contract and keep filters and sorting consistent across pages. Parsing the token or appending query options can invalidate the encoded query state. Define recovery for expiry or errors, such as restarting from page one or asking the user to refresh. Do not assume tokens are permanent bookmarks.
Agree on the API-client contract
Document the default and maximum page sizes, whether the server may return fewer items than requested, the final-page signal, token lifetime, whether filters or sort may change, and the cost and meaning of total counts. Specify evaluation order when filtering, sorting, and pagination are combined.
Clients should not assume every response contains exactly the requested number of items. Stop when the contract’s continuation link or terminal marker is absent. To reduce duplicate side effects during retries or concurrent changes, a client can temporarily record IDs it has processed. This does not provide a database snapshot; use a separate snapshot or export contract when a consistent point-in-time extract is required.
Choosing an approach
Offset can be simple for small, slowly changing lists and page-number interfaces. Set a maximum offset and verify ordering and deep-page cost.
Continuation tokens can suit large collections or APIs where the server controls page state. Keep tokens opaque and define expiry and end-of-results behavior.
Keyset pagination can suit stores that efficiently seek after the last sort key when arbitrary page jumps are unnecessary. Use a unique tie-breaker and fixed sort direction.
With any method, preserve filters and ordering across pages and document how concurrent changes can affect results.
Sources
[Microsoft REST API Guidelines: Pagination](https://github.com/microsoft/api-guidelines/blob/vNext/Guidelines.md#98-pagination)
[Microsoft Azure REST API Guidelines: Collections](https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#collections)
[PostgreSQL 17: LIMIT and OFFSET](https://www.postgresql.org/docs/17/queries-limit.html)
[Elasticsearch: Paginate search results](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results)