Event-driven architecture: the ledger everyone reads
Vertical version.
Chapters
Narration
Last time you made a consumer safe to receive the same message twice. The chai stall's order slip rail was still one producer and one kitchen, and a slip torn off that rail is gone the moment somebody reads it. Now payment, inventory and analytics all have to learn that the same order happened. Wire that point to point and every new listener means editing the producer, which never asked to know they existed. So invert it. The producer stops sending anything to anybody and publishes one event to a topic. Whoever cares subscribes. Payment reads it, inventory reads it, analytics reads it, and adding a fourth reader costs the producer nothing, because the producer does not know its consumers exist and does not even have to be awake when they read. That is why it stops being a queue. A log is an append only file. Every event is written at the end and given a number, and reading one does not remove it. Each consumer only remembers its own offset. The analytics job has read up to event six, the billing job is still down at three, and neither of them waits on the other. The log keeps everything for a fixed window, seven days say, and then trims the oldest. And an offset is a cursor you are allowed to move. A consumer crashes, comes back, and carries on from the last number it wrote down, missing nothing in the gap. Write a better analytics job tomorrow and you set its offset back to zero and re read every event that ever happened through the new code. A queue cannot offer you either of those, because what it handed out is gone. Push that further and the log stops being plumbing. If every change is an event, then the events are the truth and the current balance is only a fold over them, which hands you an audit trail and a time machine for nothing. Point the same trick at a database and you get change data capture. The database's own write log is streamed out as events, so nobody has to write to the table and publish the news separately and pray that both of them landed. None of this is free. Every consumer reads at its own speed, so every view built off the log is eventually consistent and briefly wrong. Delivery is at least once, which is exactly the debt from last episode, because a consumer that is not idempotent will apply the same event twice. And lag is the number you watch, since a reader that falls behind faster than the log is trimmed loses the events it never got to. Stop handing the task to a named server. Write down what happened, keep it, and let anybody read it at their own speed.
Based on the board The Ledger Everyone Reads.
Try it in the labs: The ledger everyone reads and The write that happened twice.
Related papers: Kafka: a Distributed Messaging System for Log Processing, Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services and ZooKeeper: Wait-free coordination for Internet-scale systems.
Covered in A Book You Never Erase, the append-only ledger, reversals and derived balances, The Ledger Everyone Reads (event-driven & the log), The write that has to happen twice (CDC and the outbox), The log is the truth (event sourcing and CQRS), Delete means delete (a user asks to be forgotten, and the data is in six places).
Saved in this browser, and to your account when you sign in.