Back to blog

A step fails halfway through five services: how do you roll back?

Thắng Đoàn
Thắng Đoàn

A user buys FotoFlow Premium: Billing creates the subscription, then the charge on their card fails. Billing has already committed, the user has no premium, and an angry support ticket arrives. A database transaction cannot span several services. Each service owns its own database, so there is no BEGIN that reaches all of them and no built-in undo.

Two-phase commit, and why it blocks

The classic fix is two-phase commit: a coordinator asks every participant to prepare, do the work, lock the rows, and promise to commit. If all say yes, everyone commits; if anyone says no, everyone rolls back. It is all-or-nothing and it works inside some databases.

The weakness is the waiting. If the coordinator crashes after prepare, participants sit in a prepared state, holding locks on rows, unable to commit or roll back on their own, and everyone else queues behind those locks. One slow or dead coordinator can freeze the whole flow. Across services and the internet, a blocking protocol is usually unacceptable.

The saga: commit step by step, undo on failure

A saga splits the flow into local transactions that each commit immediately: create subscription, charge card, upgrade account, send welcome email. Nothing is held open. Every step has a compensating action that semantically undoes it, and when the charge fails the saga runs them in reverse: cancel the subscription.

That word semantically matters: a compensation is a new action, not a rollback. A refund is not the same as never having charged, and an apology email does not unsend the first one. So order the steps deliberately: put the step most likely to fail first, and the hardest to undo, charging the card, sending emails, last. Use pending states, reserved rather than confirmed, so half-done flows are not presented as done.

Two shapes exist. Orchestration: one orchestrator runs a state machine, tells each service what to do next, and saves every transition so it resumes exactly where it stopped after a crash. Temporal and AWS Step Functions are built for this. Choreography: no conductor, services react to events, SubscriptionCreated triggers ChargeCard, and ChargeFailed triggers the cancellations. Choreography suits short flows; as they grow, an orchestrator is easier to reason about.

What this does not solve

The orchestrator crashes right after Billing created the subscription, before saving that fact. On restart it sees pending and sends the request again, and the user gets billed twice. So every step carries an idempotency key, the saga id plus step, and Billing returns the existing subscription instead of creating another. Sagas are at-least-once by nature; every step and every compensation must be safe to repeat.

The undo itself can fail: the saga must cancel the subscription but Billing's API is down. Giving up means the user pays for nothing, so compensations retry with backoff until they succeed, and one still stuck after hours goes to a queue for a person. A compensation must be able to succeed eventually, and a stuck one should page someone.

And the charge call times out. Did it go through before the connection dropped? A timeout is not a failure. The outcome is unknown. Compensating blindly could refund a charge that never happened or leave a real one live. Instead, ask: query the payment service for that idempotency key, or retry the same key and read the real answer. Every remote step has three outcomes, succeeded, failed, unknown, and only the first two let the saga move on.

Your flow charges the card in the middle. What state is the user left in when the step after it fails?
Your compensation emails an apology. What did the user already see that cannot be unsent?

The rollback exists. Next in the series: Batch at night or stream right now?

Sources

microservices.io: Saga

Martin Fowler: Microservices

Apache Kafka

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.