_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN
Let's talk

Pagination Patterns

12. 08. 2024 1 min read intermediate

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.

apipaginationdatabaserest
Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.