Tiếng Việt · English
How I started building dine
The article What dine is describes what dine looks like today. This one is about the order it was built in: each piece I added, why I added it, and the places I thought would be simple but turned out not to be. If you plan to build a similar system yourself, this order might save you a few rewrites.
Starting with a message queue
A booking needs three jobs: hold a table, have the kitchen take the order, charge the money. Three different services do those jobs, so first of all I needed somewhere to send commands to all three.
I chose Kafka, because Kafka has consumer groups and keeps the read position (offset) per group.
That way I only need one topic to send a command to every service. I created the topic order-events.
Each service reads order-events with a consumer group of its own. Kafka keeps a separate offset for each group,
so all three groups receive every message, independently of each other: a slow kitchen does not make payment wait.
Messages also do not disappear once read; they stay on Kafka for a while, so a service that dies and comes back
can carry on reading from exactly where it left off.
Kafka is not the only option. RabbitMQ can also deliver one message to several consumers (with a fanout exchange). What I like about Kafka is that messages are kept and can be read again, which is very handy when you need to figure out what an order went through.
A place to receive orders: dine.order
Next I needed an API to receive what users submit, and then send a message to Kafka. I created the repo dine.order.
Three services that do the work
Then I thought about the services that would do the actual work: payment, kitchen, reservations. I created three more repos:
dine.payment, dine.kitchen and dine.reservation.
Each repo has its own database, and each service reads order-events with its own consumer group.
The three services do not know about each other and do not call each other. Each one just listens for commands and does its own part.
dine.order.The interface, and only then the extras
With the services in place, users needed an interface to work with. I created dine.web, the very site you are reading.
I added the two services dine.gateway and dine.auth later, because at first the system ran fine without them.
With only one API to call, there was no need for a shared way in yet. Until outsiders could book tables themselves, there was no need for sign-in.
The article API gateway explains why they became necessary later.
dine.product, the service that returns the menu, is another extra I came up with later. Right now it just provides the menu so the site can show dishes
for guests to choose from. But if it grows, for example letting guests search for dishes by name or ingredient, or filter by price, then the way data is stored
for writing (adding, editing dishes) and the way it is stored for searching start to diverge. That is when CQRS comes in: splitting the write side and the read side
into two separate models, with the read side built specifically for searching and kept up to date from the write side. That is a story for a later article.
My first picture of the saga
At first, the saga in my head was very simple: a job that keeps sending messages to Kafka and listens for the services' replies.
But sending a message to Kafka is not just sending. The order is stored in MySQL, while the command goes through Kafka: two systems, and no transaction can span both. If the process dies right between the two steps, an order sits in the database that no service knows about. Swap the order and you can end up with a command for an order that does not exist. The two places drifting apart like that is the MySQL–Kafka inconsistency problem, and I only noticed it when I thought about the moments when the system dies halfway.
The fix is the transactional outbox: instead of sending directly, write the command as a row in MySQL, in the same transaction as the order. The order and the command either both exist or neither does. My job is still there; the difference is that it reads commands from the outbox table and only then sends them to Kafka. If a send fails, the row is still there and gets sent next time. The article Transactional outbox covers how it works in full, and where it can still go wrong.
A second topic for replies
When the services reply with their results, they cannot share the old topic either. All three are listening to order-events,
so if replies went there too, each service would also receive the other two's replies, and dine.order would read its own commands
mixed in with them.
So I created a second topic, saga-step-responses, used only for the way back. The three services only write to it and never read it.
At first I planned a separate reply topic per service, but then every new service would mean another topic, so I merged them into one.
Each reply carries the step name (step_name) so you can tell which service it came from.
dine.order listens to this second topic, and every time a message arrives it makes a decision: record that the service is done,
mark the order successful once all three are done, or cancel the order and undo the services that already did their part if one fails.
Things I had not thought of at first
At this point the system worked for an order where everything went right. Most of the effort afterwards went into the orders where things go wrong:
- Cancelling an order after some services have finished. There is no rollback, only doing the opposite: Compensation is not rollback.
- A service takes the command and goes silent. You cannot wait forever: Why 5 seconds cancels an order.
- The order is cancelled, yet a service reports done. Late success.
- A command arrives twice. Receiving a command twice without doing it twice.
- Two orders fighting over the last ingredients. Locking the stock.
- A message that can never be processed. Where broken messages go.
- Deploying without breaking running orders. Shutting down gracefully and Zero-downtime deploys.
Each of these is an article in the reading path. The next one starts from the most fundamental question: why not use one shared transaction for all three services.
See it for yourself
- Success Exactly the path in figure 4: one command on order-events, three replies on saga-step-responses.
- Home page Orders running for real; each ticket is a saga waiting for three services to reply.
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.