Saga / Process Manager
Escrito por Darjus Kazakevic · revisado el
Consistency across services, without a distributed transaction or its locks.
Some processes cannot be one ACID transaction. Placing an order may reserve stock, charge a card and book a courier: three services, three databases. A saga runs each step as its own local transaction. If step three fails, it runs compensations (refund, then release) for the steps that already committed.
The alternative is two-phase commit (2PC), which locks rows in every service until all agree. It blocks when one service is slow, and most cloud databases and brokers do not support it. A saga keeps services available, but other requests can see the half-finished state.
En esencia — Split a process that spans services into local transactions, and give each step a compensating action that undoes it if a later step fails.
Cuándo recurrir a él
Recurre a él cuando
- One business process writes to the databases of two or more services
- Steps take seconds to days, so holding locks across them is impossible
- Each step has a sensible business undo: refund, release, cancel
Reconsidéralo cuando
- Everything fits in one database: use one local transaction
- The business cannot accept anyone seeing a half-finished state
- Steps cannot be undone and cannot be moved to the end
The moving parts
- Steps and compensations
- Each step is a local transaction that commits on its own. Each has a compensation that undoes its business effect, such as a refund for a charge. A compensation is a new action, not a database rollback.
- Orchestration (process manager)
- One coordinator stores the saga's state and tells each service what to do next. It decides when to compensate. The whole flow is in one place, so it is easy to read, test and monitor.
- Choreography
- No coordinator. Each service listens for events and publishes its own: OrderPlaced → StockReserved → PaymentCaptured. It works well for two or three steps. With more, nobody can see the whole flow.
- Idempotency and timeouts
- Brokers deliver messages at least once, so a step can receive the same command twice. Services can also stop answering. Every handler must be idempotent, and every step needs a deadline.
Reglas de diseño
- Write the compensation before the step
- For every step, decide how to undo its business effect before you build it. If a step has no undo, such as sending an email, run it last, after every step that can fail.
- Prefer orchestration past three steps
- Choreography is fine for two or three services. Beyond that, the flow exists only in people's heads. An orchestrator puts the steps, timeouts and compensations in one file you can read and test.
- Make every handler idempotent
- A ChargeCard command can arrive twice. Use the saga ID plus the step name as an idempotency key, and return the earlier result on a repeat instead of charging again.
- Give every step a timeout
- A service that never answers leaves the saga stuck and the stock reserved. Set a deadline per step, then retry or compensate. A timed-out step may have committed, so undo it too.
Errores habituales
- A step with no compensation. It fails in production, and the stock stays reserved for good.
- Handlers that are not idempotent. A redelivered ChargeCard message charges the customer twice.
- Choreography across eight services. Nobody can say what happens after PaymentFailed.
- No timeouts. Sagas wait forever for a reply that never comes.