Back to roadmap
Module 5 · APIs & Application LayerDay 04320 min

Pagination, Filtering, Sorting

Cursors beat offsets at scale.

Day 043

Pagination, Filtering, Sorting

Client
client
API
service
DB index seek
datastore
Signal path
Cursor pagination flow
Client
client
cursor=…
API
service
API
service
flow
DB index seek
datastore
Memory hook

Pagination, Filtering, Sorting: cursors beat offsets at scale

Mental model

turn boundaries into contracts

Design lens

Cursor-only loses 'jump to page 10'.

Recall anchors
OffsetCursorFilters

Why it matters

Offset pagination is simple but slow at deep pages and unstable under writes. Cursor pagination uses a stable key and scales to billions of rows.

Deep dive

Offset: SELECT … OFFSET 10000 LIMIT 20 — DB must read 10020 rows.

Cursor: WHERE id > :last LIMIT 20 — index seek, fast.

Total counts are often expensive; consider 'has_more' instead.

Filters/sorts must match indexes or queries get slow at scale.

Demo / scenario

Browse latest posts; deep pagination is slow.

  1. Replace OFFSET with WHERE created_at < :cursor.
  2. Index (created_at, id) supports it.
  3. Drop totalCount endpoint; use 'next' cursor only.
  4. P99 drops from 1500ms to 50ms at deep pages.

Tradeoffs

  • Cursor-only loses 'jump to page 10'.
  • Mixed sort orders need composite cursors.
  • Total counts can be cached approximate.

Diagram

cursor=…
Client
API
DB index seek
Cursor pagination flow.

Mind map

Check yourself

Loading quiz…

Sources & further reading