DISTRIBUTED SYSTEMS & ARCHITECTURE · SUBTOPIC
Cursor Pagination & Idempotent APIs
Why OFFSET slows down on large tables and how idempotency keys make retries safe.
IntermediatePart of API Design & Gateways
Subtopics in API Design & Gateways (2)
In plain words
Cursor pagination says "give me 20 posts after Post #940" instead of "count past the first 10,000 posts and skip them." Idempotency means pressing "Pay" twice because of weak Wi-Fi only charges once.
Think of it like…
Bookmarking page 120 (cursor) vs. counting 120 pages from page 1 every time (offset).
Loading interactive diagram
Loading interactive exercise
Theory & how it works
How Idempotency Keys work
The client generates a UUID (Idempotency-Key: abc-123) before calling POST /orders. The server stores (user_id, key) → saved response in Redis/SQL inside the same transaction. If the client retries 3 times due to timeouts, the server returns the saved original receipt without running the charge again.
Key points & practical guide
Summary checklist
- Use cursor/keyset pagination for feeds and large datasets
- Attach
Idempotency-Keyto all non-idempotent POST/payment calls - Return standard HTTP codes:
409 Conflictor cached200/201on duplicate keys
When to use it
- Every infinite scroll feed and every payment/order endpoint
Watch out for
- Allowing
LIMIT 100000from untrusted clients - cappage_sizeto e.g. 100
Quick check
Loading quiz