Consistency and sagas
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.
Compensate a failed fulfillment step once
let available=5;const order={state:'NEW',reserved:0};function reserve(){if(order.state==='NEW'){available-=2;order.reserved=2;order.state='RESERVED';}}function release(){if(order.state==='RESERVED'){available+=order.reserved;order.reserved=0;order.state='CANCELLED';}}reserve();console.log(order.state,available);// Fulfillment refused the synthetic request.release();release();console.log(order.state,available);Expected output:
RESERVED 3CANCELLED 5Compensation releases the reserved quantity once, even when requested twice.
How it works
Define explicit states and permitted transitions. Orchestration uses a coordinator to direct steps; choreography reacts to events without a single central coordinator. Both require stable correlation IDs, durable progress and retry-safe handlers. Eventual consistency allows a temporary mismatch while work converges, but requires clear user-visible pending states and a recovery plan for stuck work. Compensation may fail or be impossible for irreversible effects; escalate to manual reconciliation when needed. For out-of-order events, use per-aggregate versions or another defined ordering contract and a resync policy. A simple sequence check below is a model, not a general distributed ordering algorithm.
Reject stale events and detect a gap
let state={version:2,status:'RESERVED'};function apply(event){if(event.version<=state.version)return 'stale';if(event.version!==state.version+1)return 'resync';state={...event};return 'applied';}console.log(apply({version:2,status:'NEW'}));console.log(apply({version:4,status:'DELIVERED'}));console.log(apply({version:3,status:'READY'}));console.log(state.version,state.status);Expected output:
staleresyncapplied3 READYOld events are ignored, a gap requires reconciliation, and the next expected event advances state.
Common mistakes
Do not promise distributed atomicity or automatic compensation success. Do not treat every old event as a new transition. Do not describe pending work as final success.
Practice and next steps
Add a failed-compensation state to the order saga. Explain who owns recovery and how repeated release commands avoid increasing available inventory twice.
Further reading: https://microservices.io/patterns/data/saga.html. Related staged reference: node-consistency-and-sagas-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Consistency and sagas — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
What is a compensating action?
A state version is 2 and the next event is version 4. What does the model do?
What should a user see while a saga is not complete?