What are the pagination strategies in REST APIs?
Answer
Three main pagination strategies are used in REST. Page/offset pagination: GET /posts?page=3&limit=20 — simple to implement and understand but unstable with live data (inserting an item shifts all subsequent pages). Offset/limit pagination: GET /posts?offset=40&limit=20 — similar to page/limit but more flexible. Cursor-based (keyset) pagination: GET /posts?after=cursor123&limit=20 — the cursor is typically an encoded last-seen ID or timestamp. It is stable (insertions/deletions do not affect other pages) and performs better on large datasets (no OFFSET in SQL). The response should include pagination metadata: { "data": [...], "meta": { "total": 500, "next": "/posts?after=xyz", "hasMore": true } }. Cursor-based is preferred for large, frequently-changing datasets like social media feeds.
Previous
What are the main API versioning strategies in REST and what are their tradeoffs?
Next
What is rate limiting and how is it communicated in REST APIs?
More REST API Design Questions
View all →- Intermediate What is HATEOAS and how is it implemented?
- Intermediate What are the main API versioning strategies in REST and what are their tradeoffs?
- Intermediate What is rate limiting and how is it communicated in REST APIs?
- Intermediate How does HTTP caching work in REST APIs with ETag and Cache-Control?
- Intermediate What are the OAuth 2.0 grant types and when do you use each?