CodePark/ questions / Pagination, filtering & versioning
START PRACTICINGPRACTICE
Backend question · HTTP & API design › Pagination, filtering & versioning

Compare offset pagination and cursor pagination for an API. When would you choose each?

GET /posts?limit=20&offset=40
GET /posts?limit=20&cursor=eyJpZCI6OTgxfQ

Offset pagination uses limit and offset (or page). Its strengths:

  • It is simple.
  • Users can jump straight to page 7.
  • Total counts are easy.

Its weaknesses:

  • Large offsets are slow, because the database still walks past all the skipped rows.
  • If items are added or deleted while someone is paging, they see duplicates or miss items.

Cursor pagination returns a nextCursor. This is an opaque value (the client should not read or build it) that encodes the position, such as the last item's sort key and ID. The next request asks for items after that position. It stays fast at any depth, and it stays correct while data changes. But you cannot jump to any page you want. Also, the sort order must be fixed and unique.

When to choose:

  • Offset: small, admin-style tables with page numbers.
  • Cursor: feeds, infinite scroll, large or fast-changing datasets, and public APIs.
  • Offset is simple, allows jumping to any page, and makes total counts easy.
  • Offset is slow on deep pages and shows duplicates or gaps when data changes.
  • A cursor is an opaque position, so paging stays stable and fast.
  • Cursors cannot jump to a page and need a unique sort order.
  • Choose based on dataset size and how fast the data changes.