Data consistency

Saga compensation and eventual consistency

A saga coordinates a business process spanning multiple local transactions. For example, an order service reserves stock and then asks a fulfillment service to allocate a delivery slot. If allocation fails, a compensating action can release the reservation. Compensation is a new business operation, not a rollback of every remote database. 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. Source: https://microservices.io/patterns/data/saga.html Local examples do not establish third-party integration support.

Formal Syntax & Structure

nodejs syntaxnodejs
step → local commit → next step; failure → compensating business operation (design pattern)

Working Example

Example Snippetnodejs
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);
Node.js Reference