Pagination and idempotency
Local practice: Node.js 22.16.0 on Linux x64; proposed 26.13.1 baseline remains untested. Save each example separately as main.mjs in a disposable directory. No packages or external services required. HTTP examples bind only to loopback and shut down after a finite scenario. Static Node.js code, not verified ByteMind execution.
Select a stable bounded page
const items=[{id:3},{id:1},{id:4},{id:2}];const sorted=[...items].sort((a,b)=>a.id-b.id);const offset=2,limit=2;console.log(JSON.stringify(sorted.slice(offset,offset+limit).map(x=>x.id)));Run:
node main.mjsExpected stdout:
[3,4]Exit status: 0. Expected stderr: empty.
How it works
For stable pagination, choose an explicit ordering with a unique tie-breaker, validate page size and offset or cursor, and define behavior for an empty page. Offset pagination can skip or repeat entries if the collection changes between requests. A cursor can help but still needs consistency rules. GET, PUT and DELETE have idempotent semantics; POST does not generally have this property. The examples sort a tiny fixed snapshot and use repeated replacement in a Map. Real POST deduplication needs durable scoped keys, payload comparison and atomic coordination; this fixture does not establish those guarantees.
Repeat an identical replacement
const catalog=new Map([[1,{title:'Old'}]]);function replace(id,title){catalog.set(id,{title});}replace(1,'New');replace(1,'New');console.log(catalog.size,catalog.get(1).title);Run:
node main.mjsExpected stdout:
1 NewExit status: 0. Expected stderr: empty.
Common mistake
Do not assume all retries are safe or that repeating DELETE must return the same status. Do not rely on an unspecified database row order for pages.
Practice
For an API with 51 records and limit 20, determine the final page size. Explain why retrying a create operation after a lost response may duplicate data.
Further reading: Authoritative source 1. Internal reference routes remain proposed until repository discovery.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pagination and idempotency — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What makes pagination ordering stable for equal primary sort values?
What does idempotency compare?
What is needed for reliable create-request deduplication across replicas?