<< back to Guides

πŸ“š API Pagination Techniques – A Comprehensive Guide

Pagination is a technique used to divide large datasets into manageable chunks (pages), ensuring faster responses, better performance, and reduced system load.


πŸ“Œ Why Use Pagination?


πŸ”’ 1. Offset-based Pagination

Definition: Uses offset and limit query parameters to return a slice of results.

Example:

GET /orders?offset=0&limit=3

βœ… Pros

❌ Cons


πŸ” 2. Cursor-based Pagination

Definition: Uses a unique identifier (often a primary key or encoded cursor) to fetch the next set of records.

Example:

GET /orders?cursor=YXNka2pfaWQ6MTAwMg==

βœ… Pros

❌ Cons


πŸ“„ 3. Page-based Pagination

Definition: Uses page number and size per page.

Example:

GET /items?page=2&size=3

βœ… Pros

❌ Cons


πŸ”‘ 4. Keyset-based Pagination

Definition: Uses the last item’s key (typically an indexed field) as a starting point for the next page.

Example:

GET /items?after_id=102&limit=3

βœ… Pros

❌ Cons


⏱️ 5. Time-based Pagination

Definition: Uses timestamps to filter data in a date/time window.

Example:

GET /items?start_time=2024-01-01T00:00:00Z&end_time=2024-01-02T00:00:00Z

βœ… Pros

❌ Cons


🧬 6. Hybrid Pagination

Definition: Combines multiple pagination strategies (e.g., cursor + time, or offset + keyset).

Example:

GET /items?cursor=abc123&start_time=2024-06-01T00:00:00Z

βœ… Pros

❌ Cons


πŸ§ͺ Comparison Table

Method Efficiency Complexity Jump to Page Use Case
Offset-based ❌ Slow on large data βœ… Simple βœ… Yes General pagination
Cursor-based βœ… Fast ⚠️ Medium ❌ No Infinite scroll, real-time feeds
Page-based ❌ Slow βœ… Simple βœ… Yes UI with "pages", legacy APIs
Keyset-based βœ… Very fast ⚠️ Medium ❌ No Ordered datasets with primary keys
Time-based βœ… Fast ⚠️ Medium ❌ No Time-series, event logs
Hybrid βœ…βœ… Best ❌ Complex βœ… Custom High-performance + accurate datasets

πŸ› οΈ Best Practices


πŸ“š Resources


<< back to Guides