Tiếng Việt · English
What a saga does when a reply arrives
The idempotency article stopped at the point where the three services finish
their work and send a reply back, sometimes more than once. This one goes to the other side: order, the saga orchestrator,
receives those replies and decides what to do. The short answer: it only needs to look at two things.
Before the four cases, one thing is always true: replies to order also travel through Kafka, so they
can arrive more than once. That means every time order handles a reply, it has to be idempotent: receiving the same
reply twice must still give one result. If you haven't read it yet,
Receiving a command twice without doing it twice explains why and how.
Where the replies go
When seating, kitchen or payment finishes its work, it sends a message to the saga-step-responses topic on Kafka.
The message is small: the order's saga ID, the step name (reservation, kitchen or payment),
whether it succeeded or failed, and the error if there was one.
The interesting part is that the three services have no idea what a saga is. They don't know how many steps an order has,
whether the other two are done, or what happens after their reply. They know exactly one thing: when the work is done, send the result to that topic.
Every decision lives in order.
order.Order only looks at two things
Every time a reply arrives, order doesn't need to know much more. It looks at exactly two things:
- The saga's state, from its own records: is the order still running, or already cancelled?
- The step's result, from the reply: did that service succeed, or fail?
Two things with two possibilities each make exactly four cases:
1. Order running, step succeeded
order marks that step as done in its records, then counts how many steps are done. Each dine order has three steps.
With one or two done, it does nothing more and keeps waiting. Once all three are done, it marks the saga as successful: the booking is confirmed.
order just counts.This ties straight back to the idempotency article: replies are recorded by the pair of saga ID and step name, so if the kitchen sends "done"
twice, the records still hold a single "kitchen done" row. order counts rows in its records, not messages, so it can never reach
"3/3" just because one service reported three times.
2. Order running, step failed
order marks that step as failed, then cancels the order immediately, without waiting for the remaining steps to reply. It looks up
the steps already reported as done and sends undo commands to exactly those services: release the table, return ingredients to stock, refund the payment.
The service that failed needs no undo, because it hasn't done anything. Compensation is not rollback
covers this in detail: why only the finished steps are undone, and how undo commands go through the outbox.
3. Order cancelled, step succeeded
This is the strangest case: the order is already cancelled, yet a service reports "done". It happens because in case 2, order cancels
without waiting for anyone. Services still working simply finish and report back. The work really happened (the table is held, the money is taken), so
order can't ignore it: it reopens the order and sends an undo command just to the service that reported done.
Late success explains why dine chooses "decide fast, clean up later" over waiting for all three.
4. Order cancelled, step failed
The order is already cancelled, and this service didn't manage to do anything, so there is nothing to undo. order just records it and does nothing more.
What about replies to undo commands?
Undo commands get replies too, on the same topic, and order handles them against its own records as well:
- Undo succeeded: record it. Once every step being undone is finished, the order ends as cancelled.
- Undo failed: stop and wait for a person, with no automatic retry, because money is involved (Compensation is not rollback covers this).
- An undo reply for a step the records never asked to undo: ignore it and log it.
orderonly trusts its own records.
What if a service dies and never replies?
All four boxes above start with "when a reply arrives". But what if the kitchen takes the command and then dies for good, never replying? No message arrives, so no box is triggered, and the order would sit at "running" forever, holding a table and holding money for an order that goes nowhere.
order can't wait forever. It has a deadline: if a service is still silent past it, it is treated as if it had reported a failure, and the order
goes into box 2. At dine that number is 5 seconds, and Why 5 seconds cancels an order explains why.
In short: the services know nothing about the saga and only send their results to a topic. order takes each result, looks at
the saga's state in its records and the step's result, and picks one of four boxes: record and count, cancel and undo, undo just the late
step, or do nothing. Silence is turned into a "failed" reply by the deadline.
See it for yourself
On the order detail page, the "How order reacts to each reply" section shows which box each reply landed in and what
order decided.
- Success Box 1 three times in a row: 1/3, 2/3, 3/3, then the booking is confirmed.
- Failure after others finished Box 1 twice, then box 2: payment is declined, seating and kitchen are undone.
- Early failure, others finish late Box 2 first, then box 3 twice for the two late services.
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.