Tiếng Việt · English
How one order travels through the system
An order at dine passes through four services, a few processes and a few dozen milliseconds of messages going back and forth on Kafka. When an order is cancelled, the first question is always: what happened, and in what order? The diagram on the order detail page answers that question. This article is about how it is made, and why dine built it instead of using an existing tool.
Nobody knows the whole story
In a monolith, to find out where an order went, reading the log is enough: everything is in one place, in the right order.
At dine, each service only knows its own part. The table service knows which table it held. Payment knows how much it charged. No service knows how the order ended, and the logs of the four services live in four different places. To understand a failed order, you would have to open each one and line up the timestamps by hand. That is slow, error-prone, and certainly not something you can show to an outsider.
One row for each job
The idea is very simple: every time order does a job for an order, it writes down one row.
Each such row is called a span. At dine there are only three kinds of job:
| Kind | Who does it | What the row records |
|---|---|---|
| Receive order | API process | When the booking request arrived and the order was written to the database. |
| Send command | saga process | When the command was taken from the outbox and sent to Kafka, how long it waited in the outbox, which services it went to. |
| Receive reply | saga process | Which service replied, done or failed, and what order decided: which state the order moved from and to, and whether an undo was sent to anyone. |
Each row also records the order ID, the start and end time, the name of the process that did the work, and the exact position of the message in Kafka. Thanks to that position, the original message can be found again when needed.
These are all the rows of the sample order "Failure after others finished", in time order:
| Time | Job | Details |
|---|---|---|
+0ms | Receive order | Write the order, the saga and the command into the outbox, taking 4ms. |
+93ms | Send command | "Do" command to 3 services, after waiting 90ms in the outbox. |
+380ms | Receive reply | Table: done. The order is still running. |
+550ms | Receive reply | Kitchen: done. The order is still running. |
+710ms | Receive reply | Payment: failed. The order moves to undoing; undo the table and the kitchen. |
+806ms | Send command | "Undo" command to the table and the kitchen. |
+1050ms | Receive reply | Table: undone. |
+1120ms | Receive reply | Kitchen: undone. Nothing left to clean up; the order ends as cancelled. |
Eight rows; read them top to bottom and you have the whole story. The diagram on the order detail page is just these eight rows redrawn.
Starting with order, then the other three
order is the coordinator: every command going out and every reply coming back passes through it. So a single service writing rows
is enough to see almost the whole path of an order, including the most important decisions.
What is missing is what happens inside the other three. So the table service, the kitchen and payment write rows of their own too,
two per command: one when the command is received and processed, one when the reply is sent. Each writes into its own
database and sends them to nobody. Rows from two different services are linked by the message's position in
Kafka: the message order sends is exactly the message the other side receives.
This splits a leg into all its parts: how long the command sat on Kafka, how long the other side took to process it, how long the reply waited in
the other side's outbox, and how long it took to get back to order. When a service receives a duplicate command, the diagram also
shows clearly that it did not redo the work, only resent the original reply.
From rows to pictures
Each service keeps its rows in its own database, and has a public API that returns the rows for one order. The browser asks all four services, merges the results itself and draws two pictures:
- The simple diagram, shown by default: each column is a service, each row is a moment a reply arrived, in time order. A square means it has replied, a circle means not yet. Anyone can read it, without knowing what Kafka is.
- The technical chart, shown when you press "Show technical chart": each row is a process that is really running, each bar is a job, each dot is a message sitting in Kafka. This picture shows where the time goes: waiting in an outbox, sitting on Kafka, or waiting for the other side to process.
All the drawing happens in the browser. The server only returns raw data, so changing how things are drawn only means editing the web page, without touching the services.
Recording must not break the order
There is one rule dine holds firmly: writing rows is only for observation and must never affect the order. A row is written after the main work is done. If writing it fails, there is just one log line reporting the error, and the order carries on as if nothing happened. A missing row means one missing box in the diagram. Breaking the order because a row failed to write would mean breaking the very thing being observed, and that is not worth it.
Why not Jaeger or OpenTelemetry
Recording the path of a request through many services is a well-known problem, called distributed tracing, and there are very good tools for it, such as OpenTelemetry and Jaeger. dine still built its own, for two reasons:
- It needs to be shown to outsiders. Those tools are made for operations teams to look at, in a separate interface. dine wants anyone visiting the site to see an order's path, so it would need its own API and page anyway.
- It needs to show decisions, not just timing. An ordinary trace answers "how long did each job take".
What dine wants to show is "what
orderdecided": which case this reply fell into, how the order's state changed, who got an undo. That information has to be recorded by hand whatever tool you use.
Given those two points, a table sitting next to the order data is the simplest approach: no extra system to run, written and read with the database that is already there.
Building it yourself has a price too. It follows no standard, so it cannot plug into existing tools. Each service that wants its own part has to build it. The table grows with the number of orders and needs cleaning up. For a much larger system, with many teams working on it, OpenTelemetry is still the more sensible choice. At dine's scale, building it is just enough and easy to understand.
See it for yourself
Open any sample order, read the diagram from top to bottom, then press Show technical chart below it to see the same story at the level of processes and messages.
- Failure after others finished The order with the eight rows in the table above.
- Success The shortest path: receive the order, send the command, three replies.
- One service goes silent There is a "receive reply" row with no message attached: that is the moment the 5 seconds ran out.
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.