Tiếng Việt · English
Why 5 seconds cancels an order
A booking is sent to three services at once. The table service answers "done", payment answers "done", and the kitchen says nothing at all. No error, no "done" either. How long should the system wait? At dine, the answer is 5 seconds. This article is about why you need a limit like that, and why the number itself is nothing special.
Silence is not the same as an error
When a service reports an error, order knows one thing for sure: that service did nothing. The next move is clear:
cancel the order, undo the services that already finished.
When a service goes silent, order knows nothing. It could be that:
- the service died before it got to do the work;
- the service did the work, but died before it could report back;
- the service is still alive, just slow, and will answer in a moment.
From where order stands, these three cases look exactly the same: no news. This is a basic fact of
distributed systems: over a network, you cannot tell a dead service from a slow one.
All you can do is pick a deadline, and decide when it arrives.
You cannot wait forever
Without a deadline, the order would sit in "processing" indefinitely. Worse, whatever has already been done stays held: a held table that nobody else can book, money that has been charged while the guest has no idea whether the order went through. One dead service ends up stalling the healthy ones too.
A timeout does not bring the dead service back to life. It only lets the system make a decision instead of waiting: if there is still no news when time is up, treat it as a failure, and handle it like any other failure.
order treats the kitchen as failed, cancels the order and undoes the table and the payment.Picking the number: 5 seconds is dine's choice
There is no timeout value that is "right" for every system. 5 seconds is the number I picked for dine, based on what dine does and how its services behave. Another project, with a different business, will need a different number, and may have to adjust it over time.
The way to think about it is fairly simple: it is a balance between two kinds of mistake.
- Too short, and orders are cancelled for nothing. A service that is just a little slow gets treated as dead. The order is cancelled even though everything was fine, and the work that service finishes afterwards has to be undone.
- Too long, and the guest has to wait, while the table and the money stay held for no gain.
At dine, holding the table, taking the stock and charging the money are all internal jobs that normally finish in under 1 second. 5 seconds is well above that, leaving room for the usual slow moments, without making the guest wait long. dine started out with 30 seconds. When the "Incident test" feature was built, the number came down to 5 seconds: the services already finish in under 1 second, so waiting 30 seconds only made the person testing sit through half a minute for nothing.
Elsewhere, the number can be very different:
| Step | A sensible limit might be | Why |
|---|---|---|
| Holding a seat, taking stock inside your own system | a few seconds | Internal work, usually very fast. |
| Payment through an external gateway | tens of seconds to a few minutes | Depends on a third party; the customer may even have to confirm an OTP. |
| Waiting for staff approval, a warehouse confirming stock | hours, days | A person is involved; it cannot be measured in seconds. |
You can also set a separate limit for each step instead of one shared number. dine only needs one, since all three steps are alike.
Implementation: one polling loop, nothing fancy
"A timeout for every order" makes it tempting to think each order needs its own timer. dine does not do that. Every step of an order is already recorded in the database with its creation time and status, so all it takes is one background task that asks the database a single question every second:
"Is there any step still waiting for a reply, created more than 5 seconds ago, whose order has no outcome yet?"
For each step it finds, the task does two things:
- Mark the silent step as failed, just as if that service had reported an error itself.
- Cancel the order, and undo if needed. If some services have already reported done, the order moves to "undoing" and those services receive an undo command (the article Compensation is not rollback covers this in detail). If none has finished, the order is simply cancelled, with nothing to undo. The silent service does not receive an undo command, because nobody knows what it did.
This approach has a few advantages:
- Simple. It is just a loop and one query, running in the same process as the part of
orderthat sends messages. - Not afraid of restarts. There is no in-memory timer to lose. If the process dies and comes back, the next scan still finds exactly the steps that are overdue, because everything lives in the database.
- Reuses the existing path. A timeout is not a separate flow; it is just another way of detecting a failure: a failure that is reported, or a failure inferred from silence. Once detected, both go down the same cancel-and-undo path.
The trade-off is a little less precision: since the scan runs once a second, an order is actually cancelled after about 5 to 6 seconds, not exactly 5.0. For booking a table, being off by a second makes no difference.
If the silent service comes back
If the kitchen really did die, the command sent to it is still sitting in Kafka. When the kitchen comes back, it reads on, processes it, and answers. By then the order has been cancelled:
- If the kitchen reports a failure: nothing to do, the order is already cancelled.
- If the kitchen reports done: the ingredients really were taken, so
ordersends the kitchen an undo command of its own. That is the "late success" case, the price of not waiting forever.
In other words, the timeout does not have to guess right. When it guesses wrong (the service was only slow), the system still rebalances itself, at the cost of one extra undo. Picking a sensible number is about making wrong guesses rare.
In short: a timeout does not fix a dead service. It only keeps the system from hanging along with it. The dead service still needs to be monitored and restarted; that is a job for operations, not for the saga.
See it for yourself
- One service goes silent The kitchen takes the command and never answers: after 5 seconds the order is cancelled and the 2 finished services are undone.
- Early failure, others finish late The order is already cancelled when a service reports "done": that work is undone on its own.
To try it yourself, log in, press Book a table, turn on “Incident test” and set one service to “No reply”. That service will take the command and stay silent, like a service dying at that very moment. Count about 5 seconds, and you will see the order cancelled.
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.