Pagination Patterns
1 min read
Choose between offset, cursor, and keyset pagination based on data characteristics.
Choose cursor pagination over offset for large datasets. Name the sort column and explain why offset breaks at scale.
How It Works
Pagination is how you return large result sets in chunks. Offset pagination (LIMIT 20 OFFSET 40) is simple but breaks on large offsets. Cursor pagination (WHERE id > last_seen_id LIMIT 20) is fast and consistent. Keyset pagination extends cursor with composite keys. In interviews, choosing cursor over offset for large datasets shows you understand database performance.
Real-World Example
Twitter's home timeline API uses cursor-based pagination: GET /timeline?cursor=1234567890&count=20. The cursor is the tweet ID of the last item. This allows efficient pagination without the O(N) scan that offset pagination would require.
Test Yourself
Scenario: Design the pagination for an activity feed API. The feed has 100M posts, users scroll deeply (some load 1,000+ items in a session), and posts can be created in real time. Write the endpoint shape and justify it.
Get notified when we launch
One email when the full practice product is live. No spam.