Tiếng Việt · English
Compensation is not rollback
One booking: the table is held, the kitchen has taken the stock, and then the payment is declined.
The first two jobs have already committed in two different databases; there is no open transaction left to call
ROLLBACK on. The order must be cancelled, but the table really was held and the stock really was taken. This article is about how dine
undoes things that have already happened, and why "undo" here is nothing like a rollback.
Why a rollback is no longer possible
In an application with a single database this is easy: all three jobs sit in one transaction, the payment fails, you call
ROLLBACK, done. Not at dine. Look at the example above:
- The table service has written the reservation into reservation's MySQL and committed.
- The kitchen has taken the stock in kitchen's MySQL and committed.
- Payment reports a failure, but the only place that sees the whole picture, that knows "the payment failed so the whole order must be cancelled", is
order, which lives in a fourth database.
order cannot reach into reservation's or kitchen's database and call ROLLBACK. Those two transactions
closed long ago, and each database can only roll back its own transactions. There is no transaction spanning all four databases
(the article Why saga instead of 2PC explains why dine does not do that).
The only thing left is to ask each service to do the opposite action itself: that is compensation.
order) and the places holding
the data to undo (reservation, kitchen) live in different databases.How: Kafka again, the outbox again
The compensation command has no road of its own. It travels exactly the same road as the original work command:
- The outbox again. In one transaction,
ordermoves the saga tocompensating, writes each step to undo as a row in thesaga_compensationstable, and writes the compensation command into the outbox table. There is never a saga that is compensating while its compensation command fails to go out, for the same reason as in the article Transactional outbox. - The first topic again. The outbox job sends the command to
order-events, the very topic used for work commands. The only difference is the label:actioniscompensateinstead of a work command, plus the list of steps to undo, for example["reservation", "kitchen"]. - The services are already listening to that topic. None of the three needs to subscribe to anything new. They receive the message and look at
the label: a work command goes down the work branch; a
compensatecommand with their name in the list goes down the compensation branch (release the table, return the stock, refund the money). If their name is not there, they ignore it. - Replies work as before: once done undoing, each service reports to
saga-step-responses, andorderchecks it againstsaga_compensationsto mark that step as undone.
compensate label tells each service to take its undo branch.Rollback erases history, compensation adds to it
In a database, ROLLBACK acts as if the transaction never ran. Nobody else ever saw its data,
because it was never committed. The database does all the work; you don't write a single line of code.
A saga has nothing like that. Each service has committed its own work, and the moment it commits, that work is a fact: others can read it, and may already have made decisions based on it. The only way to undo it is to do a new action with the opposite effect. That is called compensation, and at dine there are three of them: release the table, return the ingredients to stock, refund the money.
This difference has three consequences, and all three are visible on dine:
- Others have already seen the in-between state. For a few hundred milliseconds before the table is released, the Seats page shows that table as taken. Another order arriving right then may have been told there was no room, just because an order that is about to be cancelled was holding the table. Compensation gives the table back, but not that other order's chance.
- History stays intact. There is no
DELETE. The charge is still there, plus a refund. One look at the ledger tells you what happened, which is a good thing when you have to reconcile. - Compensation is business logic, not plumbing. "Return the ingredients to stock" is something a cook understands. Each compensating step is written as a real feature of that service, with its own conditions and its own errors.
Who gets undone
When an order is cancelled, order sends undo commands only to the services that reported done. That sounds obvious,
but two cases are easy to get wrong:
| The service replied | Undo it? | Why |
|---|---|---|
| Done | Yes | The work was committed there and must be undone. |
| Failed | No | Its local transaction committed nothing, so there is nothing to undo. |
| No reply yet (over 5 seconds) | Not yet | order does not know what it did. If it later reports done, a separate undo is sent then. |
Why not send the undo to all three "just to be safe"? Because then each service has to guess: "I'm told to refund
an amount, but did I actually charge it?". With money, a wrong guess means real money lost. dine follows the opposite rule:
order only trusts its own ledger. It records which services reported done, and only asks those to undo.
If an "undo done" reply arrives for something the ledger never asked for, order ignores it and logs it,
without updating anything based on it.
The third row of the table leads to a rather odd situation: the order is already cancelled and a service still reports "done".
That service's work is real, so order sends it an undo command of its own. The article Late success covers this on its own.
How the order ends
Every message of an order carries the same key, the order id, so they all land in the same Kafka partition: a compensation command can never arrive before the work command of that same order.
The saga only moves to failed when every step in the list has reported its undo as done.
At dine, failed means "the order is cancelled and cleaned up", while compensating means
"we decided to cancel and are cleaning up". An order that stays in compensating for long is a sign that something is wrong.
failed takes about 0.4 seconds, and during that time
the table is still held.A guest cancelling is compensation too
A saga does not only undo on errors. When a booking has been confirmed by all three services and the guest presses cancel (dine allows cancelling until
10 minutes before the booking time), order uses exactly the same mechanism: one undo command to all three services, releasing the table,
returning the stock, refunding the money. The difference is that the saga stays completed, because the order-creation saga did succeed,
while the order itself becomes cancelled.
That is why it pays to write the compensating steps properly from the start, even if failures are rare: an ordinary business case like "the guest changed their mind" needs them too.
What a compensating step must withstand
Arriving twice
Undo commands also go through the outbox and Kafka, so they too can arrive twice, like any other message. Releasing a table twice is harmless. Refunding twice is losing money. The compensating step must recognise "I have already refunded this order" and not do it again; in other words it must be idempotent. This has to be planned when designing the data: the refund transaction is tied to the saga id and checked before refunding.
It must not "fail" for business reasons, if that can be avoided
Releasing a table and returning stock happen inside dine and always succeed. The refund, however, depends on the bank, and the bank can say no. A compensating step that can fail for business reasons is the hardest kind, because there is no step after it to save it.
No ordering needed
The three services work in parallel, so they also undo in parallel. In a sequential saga, people usually undo in reverse order (the step done last is undone first). At dine the three steps don't depend on each other, so no ordering is needed.
When the undo fails too
There are two very different kinds of failure, and they are handled in opposite ways:
- Infrastructure failure: the database loses its connection, Kafka is restarting. Retrying a few seconds later may work, so retry, and move it to a dead-letter queue if it still fails.
- Business failure: the bank clearly answers "cannot refund", for example because the card is closed. Retrying gives the same answer. Worse, with money, retrying automatically before you are sure the previous attempt really failed is the fastest way to refund twice.
dine simulates the second kind: a small share of refunds are refused by the "bank". When that happens, order
records the undo step as failed and stops. The saga stays put in compensating, and the Payments page says
"Bank refused the refund, being handled by hand". Someone handles it outside the system (for example with a bank transfer), and once they confirm,
order sends a separate command saying "handled by a person". The payment service only records it and does not call
the refund again. After that the saga ends like any finished undo.
You cannot keep "compensating the compensation" forever. Somewhere there has to be a stopping point where a human takes over. Good design is not about never needing a person; it is about the system stopping in the right place, keeping enough information, and doing nothing more on its own until someone confirms.
Designing for fewer undos
Compensation has a cost: more code, more in-between states, more lines on the customer's statement. There are two ways to reduce it, and dine uses only part of them.
Put the hardest step to undo last
In a sequential saga, people put the easy-to-undo steps first and the hardest one (charging money, shipping goods, sending an email) last. If that hard step succeeds, everything after it must be work that is certain to succeed. If it fails, only the easy steps need undoing. The saga literature calls this step the pivot.
dine does not do this: the three steps run in parallel so the order is confirmed quickly, with nobody waiting for anybody. The price is that payment may have charged the money while holding the table failed, and then the money has to be refunded, something that would never happen if the steps ran in sequence. Faster, but more refunds. This is a deliberate trade-off, not an oversight.
Hold instead of doing it for real
In real life, many systems don't charge right away but only place a hold on the card (authorization), and charge for real (capture) once they are sure. Cancelling a hold is much cheaper and cleaner than a refund. This is not 2PC: the bank does not sit waiting for your command, the hold expires on its own after a while, and you still have to send the cancellation yourself, so it is still compensation. It just makes the compensating step less painful. dine's payment is simulated, so it has no such step.
| Rollback | Compensation | |
|---|---|---|
| Who does it | The database | Each service's code |
| History | As if nothing happened | Both the original and the undo remain |
| Others have seen it | No | Yes |
| Can fail | Practically never | Yes, even for business reasons |
| Arrives twice | Never happens | Must cope with it |
| Finishes | Instantly | A few hundred milliseconds, or waits for a person |
See it for yourself
In each order's diagram, the "Order decides to undo" row is when the undo command is sent, and the boxes marked ↺ are each service's reply to that command.
- Failure after others finished Table and kitchen finish first, payment refuses: one undo command for the 2 finished services.
- Guest cancels after confirmation No error at all: the guest changes their mind, and all 3 services are undone.
- Refund failed, waiting for staff The compensating step fails too: the saga stops at "undoing" and does not retry by itself.
- Payment ledger Refund lines carry a ↺ and a negative amount, right next to the original charge.
To create an order that gets undone yourself, log in, press Book a table and turn on “Incident test” to have the payment declined, or book and then cancel.
Spotted a mistake, or have a question? Write to [email protected].
dine is a personal project about distributed systems, written in Go: orchestrated sagas, transactional outbox, Kafka. More articles.