Without pagination you return millions of records at once. Offset is simple but slow, cursor is fast but more complex.
Offset Pagination¶
– Simple, but slow on large tables SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 1000; – DB must skip 1000 rows!
API¶
GET /api/products?page=51&limit=20
Cursor (Keyset) Pagination¶
– Fast even on millions of rows SELECT * FROM products WHERE id > :last_id – Cursor = last ID ORDER BY id LIMIT 20;
API response¶
{ “data”: […], “meta”: { “next_cursor”: “eyJpZCI6MTAyMH0=”, “has_more”: true } }
When to Use Which¶
- Offset: Admin panels, small datasets, need ‘go to page X’
- Cursor: Feeds, infinite scroll, real-time data, large datasets
Key Takeaway¶
Cursor pagination for production (fast, stable). Offset only for admin/small datasets.