Skip to main content

Offset vs keyset (cursor) pagination

Why does OFFSET 50000 LIMIT 20 become slow and buggy on live feeds, and how does keyset pagination fix it?

Official Reference

Model Answer & Explanation

OFFSET forces Postgres to scan and discard 50,000 rows on every page, making deep pages O(N). Concurrent inserts/deletes also shift offsets, causing duplicate or skipped items. Keyset pagination filters WHERE (created_at, id) < ($last_ts, $last_id) ORDER BY created_at DESC, id DESC LIMIT 20—staying O(log N) via index seek.

Follow-up Interview Question · Why is including a unique tie-breaker like id essential alongside created_at in cursor pagination?

Module Context & References

PostgreSQL & Redis · Popular interview theory High-yield indexing, MVCC, query plan and Redis caching questions
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.