Skip to the page

API styles and pagination · mid

Pagination: offset, cursor and keyset

Offset pagination (page=3) is simple and allows jumping to a page but gets slow and shows duplicates or gaps when data changes, while cursor (keyset) pagination asks for items after the last one seen, which is stable and fast but has no page numbers or easy totals.

The notes

What you must know

  • Offset/limit: ?offset=20&limit=10. Pros: page numbers, jump to page N, easy totals. Cons: the database still reads skipped rows (slow deep pages), and inserts/deletes shift items, causing duplicates or missed items.
  • Cursor: the server returns next_cursor (opaque, e.g. base64 of the last item's sort key + id); the client sends it back. Stable under inserts and fast with an index. No random page access, totals are hard.
  • The sort must be unique and stable: sort by (created_at, id), not created_at alone.
  • Infinite feeds, chat history and activity logs -> cursor. Admin tables where users jump to page 37 -> offset (often with a cap).
  • Frontend: keep the cursor in state (or the URL for shareable pages), dedupe items by id when merging pages, handle 'no more results', and cancel stale requests when filters change.

The red pen

Common traps: mistake → fix

  • Mistake: "Offset pagination is fine for a live feed"

    Fix: new posts shift the offsets, so users see duplicates or miss posts.

  • Mistake: "The cursor is just the page number"

    Fix: a cursor points to a position in the data (the last item seen), not a page index.

  • Mistake: "Sort by timestamp only"

    Fix: ties make pages unstable; add a unique tie-breaker like id.

Easy to hard

Questions interviewers ask, easy to hard

  1. Offset vs cursor pagination: what's the difference?
  2. Why do users see duplicate posts in an infinite feed? How do you fix it?
  3. How would you design the API for an infinite product list with filters?
  4. How would you show 'page 5 of 40' with cursor pagination, if product insists?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview