Pagination: Offset vs Cursor-Based
Learn the interview-ready mental model, practical trade-offs, and production patterns for this web fundamentals topic.
Topic content
Pagination strategy significantly impacts performance, consistency, and user experience. Offset-based is simple but degrades with scale and mutable data. Cursor-based (keyset pagination) provides stability for live feeds and infinite scroll but makes arbitrary page jumps harder.
Offset pagination is like using page numbers — easy to jump around but frustrating if someone inserts or removes pages while you’re reading. Cursor pagination is like using a bookmark — you always continue exactly where you left off, even if the book changes.
1Offset-Based Pagination
Uses page number + limit. Simple to implement and great for admin panels or static lists where users expect 'Go to page 5'.
SELECT * FROM posts ORDER BY created_at DESC LIMIT 20 OFFSET 400;
2Cursor-Based Pagination
Uses a token (usually encoded last item's sort key) for stable traversal. Ideal for timelines, feeds, and infinite scroll where data mutates frequently.
SELECT * FROM posts WHERE (created_at, id) < ($1, $2) ORDER BY created_at DESC, id DESC LIMIT 20;
3Key Trade-offs & When to Choose
Offset is simpler but can show duplicates/skips in live data and becomes slow with large offsets. Cursor is more stable and performant at scale but harder to implement 'jump to page N'.
- ✓Offset = simple but fragile with scale and mutations
- ✓Cursor = stable and performant for live data and infinite scroll
- ✓Choose based on UX: numbered pages vs continuous feed
- ✓Use composite sort keys (time + id) for cursor stability
- ✓Consider hybrid approaches when both patterns are needed
- ✓Always measure real-world performance and consistency
- ✓Pagination strategy is a core architecture decision