Retries and idempotency
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Deduplicate repeated logical messages
const completed=new Map();let total=0;function apply(id,delta){ if(completed.has(id)){if(completed.get(id)!==delta)throw new Error('conflict');return false;} completed.set(id,delta);total+=delta;return true;}console.log(apply('e1',3),apply('e1',3),apply('e2',2));try{apply('e1',4);}catch(e){console.log(e.message);}console.log(total);Expected output:
true false trueconflict5A duplicate does not change the total and inconsistent reuse is refused. This model has no durability.
How it works
Choose a stable operation ID and a uniqueness scope, such as tenant plus event ID. Store the operation record and business change in the same local transaction. If a duplicate arrives, return the previously recorded result or treat it as already applied. For request keys, compare the original payload or its canonical fingerprint; a key reused with different intent is a conflict. Retain deduplication records at least as long as the allowed replay horizon. For external effects, use the downstream idempotency contract or reconciliation; a local database transaction cannot roll back a remote call.
Commit deduplication with the business effect
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try{ db.exec('CREATE TABLE seen(id TEXT PRIMARY KEY NOT NULL); CREATE TABLE counter(n INTEGER); INSERT INTO counter VALUES(0)'); function apply(id){db.exec('BEGIN');try{ const result=db.prepare('INSERT INTO seen VALUES(?) ON CONFLICT(id) DO NOTHING').run(id); if(result.changes===1)db.exec('UPDATE counter SET n=n+1'); db.exec('COMMIT');return result.changes===1; }catch(e){db.exec('ROLLBACK');throw e;}} console.log(apply('e1'),apply('e1'),apply('e2')); console.log(db.prepare('SELECT n FROM counter').get().n);}finally{db.close();}Expected output:
true false true2A real SQLite unique constraint prevents the same event ID from incrementing the counter twice. The in-memory database is not restart durable.
Common mistakes
Do not use a process-local Set for durable deduplication. Do not acknowledge merely because an event was seen before its effect committed. Do not retry permanent validation errors as transient failures.
Practice and next steps
Simulate a receipt worker crash after storing a receipt but before acknowledging its message. Replay the same event, then replay an event with the same ID and different contents. Specify the expected state and conflict behavior.
Further reading: https://docs.bullmq.io/patterns/idempotent-jobs. Related staged reference: node-retries-and-idempotency-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Retries and idempotency — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
Which failure can cause duplicate delivery?
How should an idempotency key reused with a different payload be handled?
Where should durable deduplication and a local effect be recorded?