Pagination, filtering and sorting
Offset pagination and where it breaks, keyset and cursor pagination, filter grammars, and sort keys that stay stable.
A collection endpoint that returns everything works until the collection is large, and then it stops working all at once: the query is slow, the JSON is enormous, and the client that was holding it in memory falls over. Pagination is the fix, and the first way everyone reaches for is the one that quietly returns wrong answers.
Offset pagination, and the row that appears twice
limit and offset — in Spring Data, PageRequest.of(page, size) — is the obvious approach. Skip some rows, take some rows.
Here are ten users, newest first, three to a page. Page one:
### page 1 (offset 0): [user-10, user-09, user-08]Between that request and the next, somebody signs up. Not a contrived event: on any real list this happens constantly. Page two:
### now somebody signs up, between the two requests
### page 2 (offset 3): [user-08, user-07, user-06]user-08 is on both pages. The new row went in at position one, everything shifted down by one, and "skip 3" now skips a different three than it would have a moment earlier. The client shows a duplicate, and the user below the boundary — had the sort been the other way — would have been skipped entirely and never seen at all.
No error. No warning. The API did exactly what it was asked.
And there is a second cost that arrives later. offset 100000 does not jump — the database produces and discards a hundred thousand rows to reach the ones you want. Page 1 is instant, page 4,000 is not, and the endpoint gets slower the deeper anyone goes.
Keyset pagination asks "after this one"
Instead of counting rows to skip, name where you stopped:
select * from customer where id < :lastSeenId order by id desc limit 3Same interruption, same ten rows:
### page 1: [user-NEW, user-10, user-09]
### somebody signs up again
### page 2 (after id 9): [user-08, user-07, user-06]No duplicate, no skip. The second page is defined by a row, not by a count, so inserting anything above it changes nothing. And the query uses the index directly — where id < ? with a limit is the same cost on page 4,000 as on page 1.
The trade is real and worth stating plainly:
| Offset | Keyset | |
|---|---|---|
| jump to page 400 | yes | no |
| total page count | yes (at the cost of a count(*)) | no |
| stable under inserts | no | yes |
| cost at depth | grows | flat |
So the choice follows the UI, not taste. A numbered pager over a small, stable admin table is fine on offsets. An infinite-scroll feed, an export, anything a machine walks — keyset, every time.
Cursors: keyset with the details hidden
Exposing ?afterId=9 works and leaks a decision you may want back. An opaque cursor encodes the same thing:
{
"items": [ ... ],
"next": "eyJpZCI6OSwic29ydCI6ImNyZWF0ZWRfYXQifQ"
}The client treats it as a token, sends it back, and never parses it. You keep the freedom to change what is inside — adding the sort field to the cursor when you add a second sort order, for instance — without a client noticing.
Two rules make cursors safe: encode the sort you used, so a client cannot send a cursor from one ordering into another; and do not sign secrets into it — base64 is not encryption, and a cursor is a URL people will look at.
When there is no next page, the convention that causes fewest bugs is to omit next entirely rather than return null or an empty string. "Is there a next key?" is one check, and clients get it right.
Filtering that does not become a query language
The pressure on any list endpoint is to accept more filters, and the end state is an accidental query language that nobody designed:
GET /orders?filter=status:eq:SHIPPED,total:gt:5000,customer.name:like:anaThat is a parser you now maintain, an injection surface, and a shape no OpenAPI schema describes. The simpler version survives longer:
GET /orders?status=SHIPPED&minTotal=5000&createdAfter=2026-01-01Named parameters, each with a type, each documented, each validated by the previous lesson's constraints. When the filters genuinely are dynamic — a search screen with twelve optional fields — that is where Specification from the JPA course belongs, built from an allow-list of field names rather than from whatever string arrived.
Bound everything
Three limits that cost nothing to add and are awkward to retrofit:
- A maximum page size.
?size=1000000should be capped, not honoured. Clamp silently, or reject with 422 — but decide, because the default of trusting it is a memory problem waiting for a bored client. - A default size. Missing
sizeshould mean 20, not everything. - A maximum depth, if you stay on offsets. Past a few thousand pages, nobody is browsing; something is crawling, and a cursor is what it should be using.
Under the hood: why keyset stays flat and OFFSET does not
Both queries can use the same index — on (created_at desc, id desc), say — and that is exactly why the cost difference is worth understanding precisely rather than taking on faith.
Keyset's WHERE id < :lastSeenId ORDER BY id DESC LIMIT n is a query the index answers directly: a B-tree index stores its entries in sorted order, so the database descends the tree to the leaf holding :lastSeenId — O(log n) comparisons — and then reads the next n entries by walking the leaf pages' sibling pointers. It never touches a row outside the page being returned. The cost of page one and the cost of page four thousand are the same walk, because both start from a specific, known point in the sorted order.
OFFSET's LIMIT n OFFSET k cannot start from a known point, because "the k-th row of the ordered result" is not something a B-tree index encodes — the index knows the order, not a row's position within it. So the query executor still has to traverse the index from the beginning of the sort order, counting entries, and discard the first k before it can start returning rows. In PostgreSQL's query plan this shows up literally as a Limit node sitting on top of the index scan with a nonzero Offset — the planner is not being lazy, discarding is the only correct way to implement OFFSET's own definition. That discarded work is real CPU and, if the index doesn't cover every selected column, real disk reads for rows the client will never see.
Try it yourself
Decide what breaks a keyset cursor that offsets never had to worry about
A team ships keyset pagination sorted by order by priority desc, id desc, where priority is a small integer (1-5) shared by thousands of rows. Midway through building the feature, someone asks: "if a client is on page 3, sorted only by priority desc, and doesn't include id in the cursor at all, what actually goes wrong?" Work it out before reading the answer: is this the same duplicate-row problem offset pagination has, or something else?
Why the "always append a unique column" rule is really about the cursor, not just the ORDER BY
It is a variant of the same problem this lesson's [!MISTAKE] callout already names for ORDER BY — ties in the sort key mean the database is free to return tied rows (same priority) in any order it likes, and it is not required to pick the same order on the next query with the same WHERE priority <= :cursor shape. Without id in the comparison, the cursor cannot say "give me the rows after this specific one" — it can only say "give me rows at or below this priority value", which re-includes every row that ties with the cursor row, whether or not it was already returned on the previous page. Appending id to both the ORDER BY and the cursor's WHERE clause (WHERE (priority, id) < (:priority, :id)) is what actually names a single row rather than a value that thousands of rows share — the fix is identical in shape to the OFFSET fix, but the reason it matters is sharper for keyset, because keyset's entire correctness argument rests on the cursor identifying one exact row.
Misconceptions
- "Keyset pagination means exposing raw database IDs to the client." Only the naive
?afterId=9form does. An opaque cursor carries the same information base64-encoded (or further obscured), so the client never sees or depends on the column name, and the server stays free to change what the cursor encodes. - "Keyset pagination gives you a total count for free, the way offset does." It does not — keyset never counts anything, which is exactly why it stays flat. If a UI needs "page 3 of 40", that is a separate
count(*)query (or an approximate one), and it carries the same cost regardless of which pagination style serves the actual rows.
Sources
- PostgreSQL documentation, "LIMIT and OFFSET" (
SELECTreference) — how theLimit/Offsetexecution node behaves over an ordered scan. - Markus Winand, Use the Index, Luke — "Pagination Done the Right Way", the keyset-versus-offset argument this lesson follows.
- RFC 8288, Web Linking —
Linkheader conventions some APIs use to carrynext/prevpagination cursors.