Topic 037 / Core
Pagination, filtering and sorting
Chapter 3.5 · API Contract Mechanics
What and why
A list endpoint (→ 31, e.g. GET /orders) that returns every row at once
doesn't scale — the response grows without bound as the data grows, and most clients only
want one page at a time, filtered and sorted. Pagination splits a large result set into
bounded pages; filtering narrows it by criteria; sorting orders it — all three
belong in the endpoint's contract (→ 36). Builds on: → 31.
Diagram
Offset pagination: ?offset=20&limit=10
re-runs the query from scratch, positioned by row count — a row inserted
ahead of the current page shifts every later offset
Cursor pagination: ?cursor=<opaque-key>&limit=10
resumes exactly after the last-seen item's key — stable under inserts,
no recount needed regardless of depth
How it works
- Every list endpoint declares a default page size and a hard maximum.
- Offset pagination numbers results by position: simple, but a row inserted or deleted ahead of the current page shifts every subsequent offset, and reaching a deep offset gets costlier as it grows.
- Cursor pagination encodes a position — usually a sort key plus a tiebreaker like the ID — into an opaque token; "give me the rows after this cursor" stays stable even as rows are inserted elsewhere, and its cost doesn't grow with depth.
- Filtering restricts rows by field-level criteria declared in the contract.
- Sorting orders by a declared field or fields; cursor pagination additionally needs a stable, deterministic sort order (ties broken consistently) for a cursor to keep meaning the same thing from one request to the next.
Practical example
(illustrative). A feed of many millions of rows: jumping to "page 1000" with offset pagination means the database effectively skips everything before it — cost grows with depth. A cursor request for "the next 10 after row X" is a bounded lookup regardless of how deep X is.
When to use it
- Use cursor pagination when: the collection is large, frequently written to, or accessed at arbitrary depth (feeds, logs).
- Use offset pagination when: the list is small, rarely changes, and users jump to an arbitrary page number more than they need stability (a short admin list).
- Avoid when: returning an unbounded list with no limit at all — one slow client request becomes everyone's problem.
- Failure scenarios: offset pagination showing duplicate or skipped rows because the underlying data changed between page requests; a client constructing a cursor by hand instead of treating it as opaque, breaking the moment its encoding changes.
- Simpler alternative: a single hard-capped page with no pagination at all, when the collection provably never grows past that cap.
Practice
(decision kind — judge 3 scenarios; 15 min, no lab services). For each, choose offset or cursor pagination and defend it in one sentence: (1) an admin settings list under 50 rows; (2) a user's order history; (3) a public activity feed under heavy write volume. Done when each scenario has a choice and a defense naming what breaks under the other option.
Key takeaway
Cursor pagination trades the ability to jump to an arbitrary page number for stability and depth-independent cost — the right trade for anything large or write-heavy. Done when you can: justify offset versus cursor pagination for a given collection from its size and write pattern alone. Used next in → 38 (pagination and idempotency both live inside the same API contract, → 36).
References
None — this topic makes no external claims.
Source directory