Offset-Based Pagination
What you'll learn: How to implement pagination using offset and limit parameters, the simplest pagination pattern in REST APIs.
The Core Idea
Offset-based pagination works like flipping through pages in a book by saying "skip the first X items, then give me Y items." You send two parameters with your request:
offset: how many items to skip from the beginninglimit: how many items to return
For example, GET /users?offset=20&limit=10 means "skip the first 20 users, then give me 10 users" (users 21-30).
The Math
- Page 1:
offset=0, limit=10→ items 1-10 - Page 2:
offset=10, limit=10→ items 11-20 - Page 3:
offset=20, limit=10→ items 21-30
The pattern is: offset = (page_number - 1) × limit
Why It's Popular
Offset-based pagination is straightforward to implement. Your database likely supports it directly (SELECT * FROM users LIMIT 10 OFFSET 20). Clients can jump to any page instantly.
The Page Drift Problem
Here's the catch: if data changes between requests, results shift. Imagine you're viewing page 2 when someone deletes an item from page 1. Suddenly, the last item from page 1 "drifts" onto page 2, and you see it twice. Or new items push everything down, and you miss entries.
This happens because offset is position-based, not identity-based. In rapidly changing datasets (social feeds, real-time dashboards), this becomes problematic.
When to Use It
Offset-based pagination works great for:
- Relatively stable datasets
- Admin panels or internal tools
- When users need random access to pages
Key Takeaway: Offset-based pagination is the simplest pagination approach using offset and limit parameters, perfect for stable datasets, but prone to "page drift" when data changes frequently between requests.