496303471d
---ci--- project: atelier phase: 7 milestone: v0.1 status: complete phase_role: final milestone_complete: true requirements: covered: [ATELIER-01, ATELIER-02, ATELIER-03, ATELIER-04, ATELIER-05, ATELIER-06, ATELIER-07, ATELIER-08, ATELIER-09, ATELIER-10, ATELIER-11, ATELIER-12, ATELIER-13, ATELIER-14, ATELIER-15, ATELIER-16, ATELIER-17, ATELIER-18, ATELIER-19, ATELIER-20, ATELIER-21, ATELIER-22, ATELIER-23, ATELIER-24, ATELIER-25, ATELIER-26, ATELIER-27, ATELIER-28, ATELIER-29, ATELIER-30, ATELIER-31, ATELIER-32, ATELIER-33, ATELIER-34, ATELIER-35] partial: [] ship: milestone: v0.1 type: NFR tag: v0.0.7 merge: milestone/v0.1-atelier -> main release: https://git.cloudinit.dev/cloudinit-bot/atelier/releases/tag/v0.0.7 ---/ci--- Milestone v0.1 — Initial Framework (NFR, complete). 8 core principles (C1-C8), 11 domains, 110 domain principles, 27 derived docs, 4 good + 3 bad examples, 4 language docs, full matrix, 3 review docs. All 35 requirements covered. 7 patches (v0.0.0 pre-execution through v0.0.7 final). v0.0.7 IS the v0.1.0 milestone release.
2.4 KiB
2.4 KiB
Pagination — Derived Rules
Derives from
domains/api/first-principles.mdP7 (Performance) and P3 (Predictability).
Three Patterns
1. Offset/Limit (?page=2&limit=20)
- Simple, supports jumping to a page.
- Unstable under inserts: page 2 becomes page 1's content after an insert.
- Slow for large offsets:
OFFSET 10000scans 10000 rows. - Use for: small, stable collections, admin UIs.
2. Cursor (?cursor=base64token&limit=20)
- Stable under inserts: the cursor points to a position, not a page number.
- Fast: indexed lookup, no scan.
- No random access (cannot jump to page 5).
- Use for: infinite scroll, feeds, large collections, anything user-facing.
3. Keyset (?after_id=123&limit=20)
- Like cursor but uses the actual sort key (e.g.,
after_id=123). - Most stable and fast. Requires a unique, monotonic sort key.
- Use for: ordered collections with a natural unique key.
Defaults (P3 Predictability)
- Default
limit: 20 or 50. Never unbounded. - Max
limit: 100 or 200. Rejectlimit=10000with 400. - Default sort: by created_at descending, or by the resource's natural order.
- Always return the total count if cheap; never if it requires a separate COUNT query on a large table.
Response Shape (P2 Clarity)
{
"data": [...],
"pagination": {
"cursor": "next-base64-token",
"has_more": true
}
}
cursoris null when there is no next page.has_moreis the boolean convenience (some clients prefer it).- Never return
dataas a bare array — always wrap so you can add pagination without breaking.
Link Header (alternative)
Link: <https://api.example.com/users?cursor=X>; rel="next", <https://api.example.com/users?cursor=Z>; rel="prev"
- Useful for HTTP-level clients (curl, browser fetch).
- Less convenient for JSON-parsing clients.
Consistency (P8 Consistency across endpoints)
- Every collection endpoint paginates the same way.
- A client that learns pagination on
/usersshould know it on/orders. - Mixed pagination (cursor here, offset there) is a tax on every consumer.
What Violates Pagination
| Violation | Principle |
|---|---|
| Returning 10000 items by default | P7 Performance |
limit with no max |
P8 Security (DoS) |
| Page numbers on a frequently-inserted table | P3 Predictability |
| Bare array response (no pagination wrapper) | P1 Contract Fidelity (can't add pagination later without breaking) |