dine. a kitchen run by sagas Book a table
Log in with Google (coming soon)

Articles

Tiếng Việt · English

Compensation is not rollback

October 14, 2026 · about 10 min read

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:

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.

Four services, four separate databases. Reservation and kitchen have committed in their own databases; payment reports a failure; order knows the payment failed but lives in another database and cannot roll back for the other two. order knows: payment failed MySQL order no shared transaction reservation hold table ✓ MySQL committed kitchen take stock ✓ MySQL committed payment charge ✕ MySQL nothing Each database can only roll back its own transaction, and a committed transaction has nothing left to roll back.
Four services, four databases. The one who knows the order must be cancelled (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:

  1. The outbox again. In one transaction, order moves the saga to compensating, writes each step to undo as a row in the saga_compensations table, 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.
  2. 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: action is compensate instead of a work command, plus the list of steps to undo, for example ["reservation", "kitchen"].
  3. 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 compensate command 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.
  4. Replies work as before: once done undoing, each service reports to saga-step-responses, and order checks it against saga_compensations to mark that step as undone.
order writes the compensate command to the outbox, the job sends it to the order-events topic with the label action compensate and the list reservation, kitchen. Reservation and kitchen take the compensation branch; payment is not in the list and ignores it. order outbox Kafka · order-events action: compensate steps: [reservation, kitchen] reservation compensate: release table kitchen compensate: return stock payment not in the list: ignore Same topic, same listeners as the work command. Only the label on the message differs.
No new topic, no new path. The outbox makes sure the command is not lost, and the 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.

Ledgers compared: with a rollback, the ledger is empty as if nothing happened; with compensation, the ledger has two lines, a charge and a refund. Rollback one database, not committed yet Ledger (empty) The charge was never committed, nobody had time to see it. The database cleans up by itself. Nothing to explain. Compensation saga, every service has committed Ledger 19:00:00 Charge 313,000 19:03:12 ↺ Refund −313,000 Sums to 0, but both lines remain. You write the refund step yourself. The customer sees both lines on the statement.
The same 313,000 VND. A rollback leaves a ledger as if nothing had happened. Compensation leaves two lines that sum to 0. dine's Payments page shows exactly that: the refund line has a ↺ and a negative amount.

This difference has three consequences, and all three are visible on dine:

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 repliedUndo it?Why
DoneYesThe work was committed there and must be undone.
FailedNoIts local transaction committed nothing, so there is nothing to undo.
No reply yet (over 5 seconds)Not yetorder 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.

Timeline of a failed order: table done at 380ms, kitchen done at 550ms, payment fails at 710ms, the saga moves to compensating, the table undo finishes at 1050ms, the kitchen undo at 1120ms, and the saga ends as failed. reservation kitchen payment 380ms 550ms 710ms 1050ms 1120ms saga started compensating failed
The sample order "Failure after others finished". Green square: reported done. Red square: reported a failure. Blue ring: undo done. From the payment failure until the order ends as 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:

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.

RollbackCompensation
Who does itThe databaseEach service's code
HistoryAs if nothing happenedBoth the original and the undo remain
Others have seen itNoYes
Can failPractically neverYes, even for business reasons
Arrives twiceNever happensMust cope with it
FinishesInstantlyA 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.

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.