Skip to content
  • Pagination · API Design

Pagination

LearnViaNotes @learnvianotes· 1 min read

Never return unbounded lists. Even if there are only 10 rows today, there will be 10 million tomorrow. Pagination is non-optional.

Offset

GET /products?offset=40&limit=20
// SQL: SELECT * FROM products LIMIT 20 OFFSET 40

{ "data": [...], "pagination": { "total": 1250, "offset": 40, "limit": 20 } }

Cursor (recommended)

GET /products?limit=20

// Response:
{ "data": [...], "pagination": { "next_cursor": "eyJpZCI6MTIzfQ==", "has_more": true } }

// Next request:
GET /products?after=eyJpZCI6MTIzfQ==&limit=20

Keyset

GET /products?after_id=123&limit=20
// SQL: WHERE id > 123 ORDER BY id ASC LIMIT 20

Strategy comparison

StrategyPerformanceStable under inserts?Random accessBest for
OffsetPoor (table scan)NoYesSmall datasets
CursorExcellentYesNoInfinite scroll, feeds
KeysetExcellentYesNoTime-series, append-only

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report