ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/coordinator/tasks/task-9-4-sagas
TASK

Implementation

Sagas: sequence of local transactions with compensations. On failure, compensate in reverse order.

Sample Test Cases

Execute all steps in sequenceTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"saga_execute","msg_id":2,"saga_id":"saga1","steps":[{"name":"reserve_inventory","action":"reserve","compensation":"release"},{"name":"charge_payment","action":"charge","compensation":"refund"},{"name":"ship_order","action":"ship","compensation":"cancel_shipment"}]}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c1","body":{"type":"saga_execute_ok","in_reply_to":2,"msg_id":1,"saga_id":"saga1","status":"completed","steps_executed":["reserve_inventory","charge_payment","ship_order"]}}

Hints

Hint 1
Each step has compensating action
Hint 2
On failure, run compensations in reverse
Hint 3
Eventual consistency
OVERVIEW

Theoretical Hub

Sagas

Sagas break transactions into local steps with compensations. No locks held. Eventually consistent.

Key Concepts

sagacompensationeventual consistency
main.py
python
Implement Saga Pattern - The Coordinator | Build Distributed Systems