Skip to content
syntaqx

Requests & Responses, chapter 04 of 11

Pagination

Cursor pagination by default, page numbers only for small stable lists, metadata in the Link header, and why offsets break on data that keeps changing.

3 min readUpdated

Any list that can grow gets split into pages. The questions are how a client asks for the next page, where the navigation lives, and what happens when rows change while someone is paging through. Most pagination bugs come from the last one.

The body of a list response is a bare array, with no envelope to hold a cursor. Where to go next lives in the Link header, a standard every HTTP library already parses:

HTTP/1.1 200 OK
Link: <https://api.syntaqx.com/payments?cursor=eyJpZCI6IjEyMyJ9&limit=25>; rel="next"

Clients follow the URL in rel="next" as-is and never build page URLs themselves, so the server can change the scheme without breaking anyone.

Rel Meaning
next The next page. Omitted on the last page, which is how a client knows it's done
prev The previous page. Omitted on the first
first, last Only where they're cheap and meaningful (page-based lists)

Every link carries the same filters, sort and limit as the request that produced it (Filtering & Sorting), so following next never silently changes the result set.

Cursors by Default#

The client passes an opaque cursor from the previous response, and the server returns the items after it:

GET /payments?limit=25
GET /payments?cursor=eyJpZCI6IjEyMyJ9&limit=25

Why Not Offsets?

?page=3&per_page=25 translates to OFFSET 50 LIMIT 25, and offsets have two problems that grow with the data:

  • Rows shift under the reader. If a new payment arrives while a client is on page 2, everything moves down one, and page 3 starts with an item the client already saw. Deletes do the opposite and skip one. For an export or a sync job, that's a duplicate or a silent gap.
  • Deep pages get slow. To return rows 100,000 to 100,025, the database reads and throws away the first 100,000. Page 1 is fast and page 4,000 times out.

A cursor encodes where you are (the sort values and id of the last item returned) instead of how many to skip. The next query is WHERE (created_at, id) < (…) ORDER BY created_at DESC, id DESC LIMIT 25, which an index answers at the same speed on any page, and inserts elsewhere in the list don't move the reader. The id tiebreaker matters: without it, two items with the same created_at can be skipped or repeated at a page boundary.

NOTES: (UNLESS OTHERWISE SPECIFIED) 1 ILLUSTRATIVE LIMIT, TO KEEP THE DRAWING SHORT; THE CHAPTER'S DEFAULT IS 25 AND ITS MAXIMUM 100. 2 OPAQUE TO CLIENTS; THE SERVER READS IT AS WHERE (CREATED_AT, ID) < K1, WHICH AN INDEX ANSWERS ON ANY PAGE. 3 THE OFFSET WALK RETURNS THE SAME ROW TWICE: THE NEW ROW SHIFTED EVERY ROW DOWN ONE. A DELETE DOES THE OPPOSITE AND SKIPS A ROW. THE CURSOR WALK IS IMMUNE TO BOTH BECAUSE IT NEVER COUNTS. NOT TO SCALE, ONE CELL PER ROW. VALUES AND FORMULAS ARE IN THE TABLE. i9 i8 i7 i6 i5 i4 i3 i2 i1 L K1 2 PLAN VIEW · PAGE 1, NEWEST FIRST NOT TO SCALE · ONE CELL PER ROW 1 i10 i9 i8 i7 i6 i5 i4 i3 i2 i1 OFFSET O P1 3 CURSOR K1 P2 PLAN VIEW · PAGE 2, AFTER A NEW ROW ARRIVES NOT TO SCALE · THE SAME LIST, ONE NEW ROW AT THE TOP VARIABLES · ILLUSTRATIVE CODE VARIABLE VALUE L rows per page, the limit 3 O rows an offset skips for page 2 L × (2 − 1) = 3 K1 cursor: sort values and id of the last row returned (created_at, id) of i7 P1 offset page 2: rows O + 1 to O + L rows 4 to 6 = i7, i6, i5 P2 cursor page 2: first L rows after K1 i6, i5, i4 Dimensional drawing of two ways to fetch page 2 of a list sorted newest first, with an illustrative limit of 3. Page 1 returns i9, i8 and i7, and the cursor marks i7. Then i10 arrives at the top. The offset walk skips three rows and returns i7, i6 and i5, repeating i7; the cursor walk returns the three rows after i7, which are i6, i5 and i4, with nothing repeated or skipped.
An offset counts from the top, so a new row shifts every page; a cursor remembers the last row, so it doesn't.

The cost is real: a cursor can't jump to page 40 or say how many pages there are. For APIs that's the right trade, because machine clients walk lists front to back, and correctness under change matters more than random access.

Cursors Are Opaque

The example cursor happens to be base64-encoded JSON, but clients treat it as an opaque string: never decode it, build it, or store it long term. That lets the server change what's inside (add a sort key, switch the index) without a breaking change. The server validates every cursor, and one that's malformed, expired, or used with different filters than it was issued for returns 422 with the standard error format.

Page Numbers for Small, Stable Lists#

Page-based pagination is still the right tool when a person is browsing a list that's small and rarely changes, and wants to jump to page 5 or see "page 2 of 7":

GET /plans?page=2&per_page=25
HTTP/1.1 200 OK
Link: <https://api.syntaqx.com/plans?page=1&per_page=25>; rel="prev", <https://api.syntaqx.com/plans?page=3&per_page=25>; rel="next", <https://api.syntaqx.com/plans?page=7&per_page=25>; rel="last"

If the list can grow without bound, or a client will walk all of it, use a cursor.

Limits#

Parameter Default Maximum
limit (cursor) 25 100
per_page (page-based) 25 100
  • A ceiling on every list. Without one, a single ?limit=1000000 is a denial of service against your own database. 100 keeps each response fast while a full walk stays at ten requests per thousand items.
  • Over the maximum is a 422, not a silent clamp. A client that asked for 500 and quietly got 100 will believe it has everything.
  • No total count by default. COUNT(*) over a large, filtered table can cost more than the page itself, and the number is stale by the time the client reads it. Endpoints where a total is cheap and genuinely useful can document one.

References#

Specifications

Reference

In practice