Lesson 3 of 9 · 26m
Collections, Pagination, and Queries That Scale
Cursor and offset pagination tradeoffs, stable ordering, filtering contracts, and the failure modes of unbounded collections.
Every API collection is small during the demo and hostile in production. The first customer imports 400,000 invoices, sorts by a nullable column, filters on a field with no index, and your "simple list endpoint" becomes a database incident. Collection design is capacity planning expressed as a contract.
Always bound the response
GET /invoices?limit=100&cursor=eyJjcmVhdGVkX2F0IjoiMjAyNi0xMS0yOFQx...Pick a default page size and a hard maximum. A common default is 50 or 100 items; the exact number matters less than enforcing it. Return pagination metadata in a stable shape:
{
"data": [],
"page": {
"limit": 100,
"has_more": true,
"next_cursor": "eyJjcmVhdGVkX2F0I..."
}
}Do not make clients inspect response length to guess whether another page exists. Empty arrays, explicit has_more, and deterministic cursors prevent edge-case loops.
Cursor pagination survives writes; offsets do not
Offset pagination (?offset=1000) is easy to explain and wrong for changing data. If three rows are inserted while the client pages forward, items are skipped or duplicated. It also gets expensive because the database scans and discards the first thousand rows.
Cursor pagination encodes the position in a stable ordering:
SELECT id, created_at, total
FROM invoices
WHERE (created_at, id) < ($1, $2)
ORDER BY created_at DESC, id DESC
LIMIT 101;The extra row tells you whether there is another page. Cursor values should be opaque to clients. If they contain implementation details, callers will eventually parse them and couple themselves to your schema.
Filtering is a language, so define its grammar
Start small:
/invoices?status=open&customer=cus_41Hm&created_at[gte]=2026-01-01Document which fields are filterable, which operators are supported, and whether multiple values mean OR or AND. Do not accept arbitrary column names; that is a security and performance problem wearing a flexibility costume. Every filterable field should have an index plan or a deliberate rejection.
Sorting needs the same treatment. sort=-created_at,id is a useful convention, but only whitelist fields. Stable pagination requires a unique tiebreaker even when the user asked to sort by a non-unique value.
Large nesting and expansion are collection problems too
GET /customers?expand=invoices looks harmless until one customer has 30,000 invoices. Expansion must obey inner limits and should not recursively expand by default. If clients need a graph traversal, consider a purpose-built reporting or export resource instead of stretching REST collections into ad-hoc joins.
Field notes
- Return consistent ordering even when no
sortis supplied; database order is not a contract. - Do not expose total counts unless they are cheap. Exact counts over large filtered tables often cost more than the page itself.
- Validate cursors as untrusted input. A bad cursor is a 400, not a 500.
- Cursor expiry is legitimate, but say so in the documentation and provide a restart path.
Was this lesson helpful?