← Lessons

quiz vs the machine

Gold1400

System Design

Pagination Tokens and Cursors

Why cursor based pagination beats page numbers for large, changing result sets.

5 min read · core · beat Gold to climb

Two ways to paginate

When a result set is too big to return at once, an API returns it in pages.

  • Offset pagination uses a page number or offset, like skip 40 take 20. It is simple but slow on deep pages because the database still scans skipped rows, and it can skip or repeat items when the underlying data changes between requests.
  • Cursor pagination returns an opaque cursor or next token that points to the last item seen. The next request says give me items after this cursor.

Why cursors win at scale

  • The query becomes where id greater than cursor order by id limit n, which uses an index and stays fast no matter how deep you page.
  • Because the cursor anchors to a real row, inserts and deletes elsewhere do not shift the window, so items are not skipped or duplicated.

The cursor is opaque on purpose. Clients should treat it as a blob and pass it back unchanged rather than parsing it.

Key idea

Cursor pagination anchors each page to the last seen item giving fast index based queries and stable results even as data changes.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does offset pagination slow down on deep pages?

2. How should a client treat a pagination cursor?