Note
How can pagination stay stable as a list changes?
Cursor Pagination Is a Contract, Not a UI Detail
A stable cursor contract protects ordered reads from duplicates, gaps, precision loss and expensive per-row follow-up queries.
Note
How can pagination stay stable as a list changes?
A stable cursor contract protects ordered reads from duplicates, gaps, precision loss and expensive per-row follow-up queries.
Cursor pagination is often introduced to make an infinite list feel faster. That is useful, but incomplete. Once a client stores and sends a cursor, the API has promised a way to continue an ordered read without silently losing or repeating records.
The useful outcome is not merely a next button. It is a read contract that
stays predictable while data changes, the page size changes, and callers retry.
A cursor has meaning only relative to an order. created_at descending sounds
like one, but timestamps commonly collide. If two records share the same time,
the database is allowed to return either first unless the query adds a stable
tiebreaker.
Define the order completely, for example by timestamp and an immutable unique identifier. The cursor then contains the last tuple the caller saw. The next query asks for records strictly after that tuple in the same direction.
This is why an opaque cursor should not be treated as a page number in disguise. It represents a position in a particular ordering, not an offset in a mutable list.
The continuation predicate must mirror every ordering field. With a descending timestamp and descending identifier, the next page contains records whose timestamp is earlier than the cursor's, plus records at the same timestamp with an identifier that sorts later.
That detail prevents the most common boundary bug: using only the timestamp.
Doing so skips colliding records or returns them again on the next request.
The same fields should appear in the index, the ORDER BY, the cursor payload,
and the continuation predicate. A mismatch in any one of those places changes
the contract.
Time is a frequent source of accidental gaps. A cursor serialized with seconds cannot safely resume an order produced with microseconds. A timestamp converted through a local timezone can move too. Numeric identifiers have similar risks when a client language rounds large values.
Keep cursor values in a lossless, canonical form. Use one timezone convention, preserve the precision used by the database, and encode large identifiers as strings when a client might not represent them exactly. The cursor may be opaque to the client, but its representation must be precise to the server.
The server does not need to count every remaining record to tell a client
whether another page is likely available. Fetch one item more than the requested
limit. Return the first limit items; if the extra item exists, set hasNext
and build the next cursor from the final returned item.
This keeps the continuation point tied to what the caller actually received. Building a cursor from the extra item would skip it. Counting all matching rows on every request may also turn a cheap indexed read into an expensive second query without improving the navigation decision.
A page is a bounded unit of work, which makes per-row follow-up queries easy to see. Loading a related record for each result may look harmless for ten items, then multiply database round trips for every page and every retry.
Choose the relationship strategy deliberately: join data that belongs to the list, batch-load a known set of related identifiers, or omit expensive detail until a detail view needs it. The correct choice depends on cardinality and payload size, but the page query should have a known upper bound on database work.
An empty first page is a valid result, not an exceptional one. Its response should clearly say that no continuation exists. A client may also resend a cursor after records have been removed or after a retry; the server should continue from the stored tuple rather than assume the exact row still exists.
Malformed, unsupported or tampered cursors are different. Reject them with a clear client error instead of falling back to the first page, which can create an invisible loop. Versioning the cursor payload gives the API room to evolve without guessing how to parse older values.
The most valuable tests cover boundaries rather than a single happy path:
When these cases are explicit, pagination stops being a UI implementation detail. It becomes a dependable protocol between a changing dataset and every client that needs to traverse it.