BackendPrep
All Guides/APIs & Protocols/API Pagination: Offset vs Cursor
Performance & DatabasesAPIs & ProtocolsIntermediate13 min read

API Pagination: Offset vs Cursor

A deep technical breakdown of Offset vs Cursor-based (Keyset) pagination — analyzing database B-Tree index scan mechanics, the deep offset performance cliff, data drift anomalies, and implementing stable encoded cursor tokens.

Technologies:APIsDatabasesPerformanceSQL
Foundational Principles vs Practical Heuristics
Universal Principle (Inviolable Truth):

In relational databases, `OFFSET N` does not jump directly to row N. The database engine must scan, read, and discard all N preceding rows from disk or buffer memory before returning the requested rows.

Practical Heuristic (Rule of Thumb):

Use Offset pagination for small tables (<50,000 rows) or admin dashboards requiring direct jump-to-page navigation. Use Cursor pagination for infinite-scroll mobile feeds, high-write datasets, and public APIs.

Production Illustrative Example:`SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 1000000;` forces PostgreSQL to scan 1,000,020 rows, taking ~1.8 seconds; equivalent cursor `WHERE id > 1000000 LIMIT 20;` performs an index seek in 0.4ms.
Target Competencies & Learning Outcomes:
  • Understand why SQL LIMIT / OFFSET degrades linearly ($O(N)$) on large tables
  • Explain the data drift problem: missing or duplicated rows during infinite scrolling
  • Implement high-performance Keyset Cursor pagination using indexed WHERE clauses ($O(\log N)$)
  • Encode multi-column sort cursors securely using Base64 tokens

1. Offset-Based Pagination Mechanics

Free Chapter Preview

Offset pagination is the most common implementation in web applications:

HTTP / RFC 9112
GET /api/v1/products?limit=20&page=5

The server calculates: offset = (page - 1) * limit = 80 and executes:

PostgreSQL / SQL
SELECT * FROM products
ORDER BY created_at DESC
LIMIT 20 OFFSET 80;

Advantages:

  • Simple to implement with ORMs.
  • Allows clients to jump directly to any arbitrary page (e.g. Page 7).
  • Returns total count (SELECT COUNT(*)) for displaying total pages in UI navigation.

2. The Offset Performance Cliff ($O(N)$ Degradation)

In this section, we examine the production failure mechanics, internal socket buffers, and kernel-level trace diagnostics...

100% Free Knowledge Library

Sign In to Read the Full Guide

Unlock all remaining 5 chapters of API Pagination: Offset vs Cursor. Get complete production code snippets, architectural trade-offs, and track your reading completion.

Explore Other Guides
✓ Free Google / GitHub Sign-In•✓ No Subscription Required•✓ Instant Access