Tiếng Việt · English
Late success: the order is cancelled, yet a service reports done
Payment declines the card, and the order is cancelled at once. A few hundred milliseconds later, the table service reports "done", and then the kitchen reports "done" too. The order is already cancelled, so how can anyone still be finishing work? This is not a bug; it is perfectly normal in a system where the services run in parallel and do not wait for each other. This article is about how dine deals with it.
What happens
When an order is created, order sends commands to all three services at the same time. Each one does its own job
and answers when it is done; whoever is fastest answers first. No ordering is guaranteed.
Suppose payment answers first, and the answer is "failed". order cancels the order right away. At that point no service
has reported done yet, so there is nothing to undo. But the table service and the kitchen had already received their commands and are midway through.
They do not know the order has been cancelled, so they finish and report "done". The table really was held, the ingredients really were taken.
Why not wait for all three before deciding
There is one way to never run into this: when a service reports a failure, order keeps waiting until all three have answered,
and only then cancels, undoing everything in one go.
dine does not do that, because "waiting for all three" can mean waiting a long time. If one service has gone silent, the order would hang for the full 5 seconds before it could be cancelled, even though the failure was clear from the first second. Cancelling right away means the guest learns the outcome sooner, and whatever has been finished is released sooner.
The price is that, now and then, one more late job has to be cleaned up. dine chooses to decide fast, clean up later. This is a choice, not a requirement: another system might choose to wait for everyone before deciding, if for them undoing costs more than waiting.
What has happened must be undone
When order receives a "done" reply for an order that has been cancelled, it cannot ignore it.
The table really was held; ignore it, and that table stays held forever for an order that no longer exists.
So order does exactly this:
- Reopen the order, from "cancelled" to "undoing".
- Send a separate undo command to exactly the service that just reported done late. Nobody else gets it again.
- When that service reports the undo is done, the order goes back to "cancelled".
If several services are late, each one gets its own undo command as soon as it reports done, without waiting for the others. The order only goes back to "cancelled" once every undo command has finished. The undo itself works exactly as described in Compensation is not rollback: release the table, return the stock, refund the money.
"Cancelled" is not necessarily the end
This leads to something slightly odd: dine's two outcomes, "done" and "cancelled", are not equally final.
- "Done" only happens when all three services have reported done. Nobody is left to answer, so nothing can arrive late. This outcome is final.
- "Cancelled" is decided as soon as the first service reports a failure, when the others may not have answered yet.
So "cancelled" means "
orderhas decided to stop", not "everything has settled". It can still be reopened to clean up late work.
This follows directly from the "decide fast, clean up later" choice above. When reading the state of an order at dine, keep this in mind: a "cancelled" order has only truly settled when it has no step left that is being undone.
What if a late reply is a failure
The opposite case is much simpler: the order is cancelled, and a service arrives late reporting "failed".
That service did not get anything done, so there is nothing to undo. order just records it and does nothing more.
Put together, every reply order receives falls into one of four cases:
| Reply | Order still running | Order already cancelled |
|---|---|---|
| Done | Recorded. Once all three are done, the order succeeds. | Reopen, undo this service on its own. |
| Failed | Cancel the order, undo the services that finished. | Do nothing. |
The bold cell is the subject of this article. The two cells in the "Order still running" column came up in earlier articles. dine's order detail page shows which cell each reply falls into, when you open the technical chart below the diagram.
See it for yourself
- Early failure, others finish late Payment declines first, the table and the kitchen report done afterwards: each one is undone on its own.
- Failure after others finished For comparison: the services finish before the failure, so a single shared undo command is enough.
To try it yourself, log in, press Book a table and turn on “Incident test”: have the payment declined and set payment to answer first. On the diagram, the other two services will show a striped "late" box.
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.