0-Based or 1-Based Pagination? Why It Matters for API Consistency
🔢 0-based vs 1-based Pagination — Why It Matters in RDBMS vs NoSQL
Ever wondered whether pagination should start at 0 or 1? You’re not alone.
In most RDBMS systems (like PostgreSQL, MySQL, SQL Server), pagination is 0-based: • The first page starts at OFFSET = 0 • It aligns with SQL’s OFFSET + LIMIT • Example:
SELECT * FROM table LIMIT 10 OFFSET 0
In contrast, many NoSQL systems (like MongoDB, DynamoDB, Firebase) tend to be 1-based: • Pages often start at index = 1 • Pagination is typically cursor-based or key-based • Example in MongoDB:
db.collection.find().skip((page - 1) * limit).limit(limit)
✅ Why does this matter? • Inconsistent pagination models cause off-by-one errors • They impact frontend logic and pagination UI • They can create confusion when switching between SQL and NoSQL backends
⚠️ Quick Fix Example (for C# LINQ):
Let’s say your API uses 1-based pagination (From = 1, To = 10):
list.Skip(request.From - 1).Take(request.To - request.From + 1);
This correctly returns items 1–10: • Skip(0) starts from the first element • Take(10) grabs exactly 10 items
🛠 When designing APIs for frontends: Be consistent and explicit. Whether you use 0-based or 1-based pagination, document it and handle conversions in the backend — not in the frontend. This reduces complexity and avoids subtle bugs in your UI.
No comments are allowed for this post