hardSenior Backend EngineerE-commerce
What is the Saga pattern for distributed transactions across microservices?
Posted 18/04/2026
by Mehedy Hasan Ador
Question Details
"Order creation spans inventory, payment, shipping services. If payment fails after inventory reserved, how do we compensate?"
Suggested Solution
Saga: Sequence of local transactions with compensating actions
class OrderSaga {
async execute(order) {
try {
await inventoryService.reserve(order);
await paymentService.charge(order); // FAILS
await shippingService.schedule(order);
} catch (error) {
// Compensate in reverse
await paymentService.refund(order);
await inventoryService.release(order);
await orderService.cancel(order.id);
}
}
}