Message Queue: Scaling It
Minutes twenty to thirty turn this queue into a replicated log, and you never have to announce it, because four pushes do it for you. A routine deploy restarts the broker, and every unread message was in its memory. A new team asks for all of it from yesterday, and yesterday has been deleted. One machine's disk and one machine's link run out. A broker says yes and loses power before anyone else holds the message.
The Restart
"Your deploy restarts the broker. Tell me what's still there afterwards." Right now, nothing, so every message is appended to a file before the yes.
Appending is the cheap way to touch a disk, and the gap is wide. A hard disk does about 170 random operations a second. In order, it moves about 250 MB a second, which is 250,000 messages of 1 KB. That is over a thousand times as many. An SSD narrows that gap a long way, and writing in order still wins. So, for now, each topic gets one file, written only at its end, and many messages are flushed together, a flush being the call that forces bytes onto the disk.
The price is a flush on the path to every yes, shared by whatever else arrived with it. A restart now finds everything it ever said yes to. A disk that dies still takes it all, and the fourth push comes back for that.
Reading Yesterday
"A new team wants all of this, starting from yesterday morning. Can you give it to them?" Not from a queue. A queue deletes on acknowledgement, so this is the question you parked when you called it a queue, and the answer is that it was the wrong shape for what they want.
So nothing is deleted on a read. The file is kept seven days, read or not, and that makes it a log. Each group holds one number, the offset, its position in the log, which Chapter 5's Clocks and Ordering promised and Chapter 6's Queues, Logs and Pub-Sub described. Once the topic is split, that is one number per partition. A new team just starts at yesterday morning's offset, and reading stops being a change to the data.
Multiply out the alternative yourself. Seven days at 200,000 a second is 140 billion messages. Assume an acknowledged record takes 16 bytes a message for each group. That is about 2.2 TB, and about 11 TB for five groups. Offsets for Scope's 400 partitions and five groups, at 8 bytes each, are 16 KB. Eleven terabytes against sixteen kilobytes is the whole argument.
Two calls change. A consumer now fetches from an offset, and it saves an offset where it used to acknowledge. Consumers pull, so a slow group costs the broker disk and no work. Two more join them: one reads a saved offset back after a restart, and one turns a time into an offset, which is how yesterday morning becomes a number. Many systems call saving an offset committing it. This chapter says saving, because "committed" is about to mean something else.
produce(topic, key, value) -> yes fetch(topic, partition, offset, max_bytes) -> messages save_offset(group, topic, partition, offset) -> ok load_offset(group, topic, partition) -> offset find_offset(topic, partition, time) -> offset
This costs three things. The disks are 140 TB for one copy, and 420 TB with the three the fourth push will earn, the price of being able to replay a week. A restart re-reads from the last saved offset, so duplicates are normal, as Chapter 6's Delivery Semantics at Scale says they are. And there is no retry for one message, so a poison message, one that keeps failing, blocks its partition until someone decides what to do with it.
The Ceiling
"Your one broker's disk is full and its link is saturated. Where does the next message go?" To another broker, by way of partitions. A topic is split across many brokers, and each message goes to a partition by a hash of its key, so one key stays in one log, in order. Each partition has its own file.
At one copy, 140 TB over 20 TB is 7 brokers. The 400 partitions come from the consumer's rate, not from the disks. On average, each takes about 0.5 MB a second and holds 350 GB for the week.
A small metadata service records which broker holds what. A client asks it, remembers the answer, and goes straight to the broker from then on, so the service never sits on a message's path. Within a group, each partition has one reader, and that is what gives order per key back.
Order now holds only within a partition. A hot key cannot be split without losing its order, the one-key case in Chapter 3's Hot Partitions. Adding partitions later moves keys, as Chapter 6's Delivery Semantics at Scale warns, so you size for growth now, and running at half load is part of that. And a group has to agree who reads what, which you park by name: rebalancing, the reshuffle when a consumer dies or joins.
Dying After the Yes
"Say the broker told the producer yes, and then lost power. Is that message safe?" Not while one broker holds it. So each partition gets one leader, which takes its writes and serves its reads, and two followers in other zones, which copy the leader's log. That brings back the 420 TB and the 21 brokers from Scope.
Now the rule. The in-sync set is the leader plus the followers keeping up with it. A write is committed once every copy in the in-sync set holds it, with at least two in that set. Only then does the yes go back. Consumers read only up to the committed offset, so nobody reads what could vanish.
The metadata service becomes a small consensus service, holding who leads each partition and who is in its in-sync set, so there is exactly one leader, as Chapter 5's Why Agreement Is Hard promised. Leader Election and Consensus is how it decides. When a leader goes silent, it picks the next one from the in-sync set, which by the rule holds everything acknowledged. Every write the leader copies carries a leadership number that only grows, and the followers check it, so they refuse a deposed leader: the fencing token of Leases, Locks and Fencing.
Take the two losses you promised. A broker leads about 19 of the 400 partitions. Assume the 10-second timeout from Why Agreement Is Hard, and those partitions take no writes for at least that long. Producers hold those messages and retry, and none of them has a yes yet, so the promise is untouched. Put seven brokers in each zone, and losing a zone takes one copy of every partition. Two remain, so the floor of two still holds.
A broker that never returns leaves its partitions on two copies, which the floor allows, and no disk has room for its 20 TB. So a replacement broker joins in the same zone, and the consensus service has it copy each partition from its leader. At half of one link that takes about nine hours, and if another copy goes meanwhile, that partition refuses writes and loses nothing.
The consensus service can fail too. It runs in all three zones, so losing one leaves it a majority. If it loses its majority, leaders keep taking messages, but nothing can fail over or leave an in-sync set until it returns.
The price is on every yes. The leader sends to both followers at once and waits for the slower one, a round trip between zones of 1 to 2 milliseconds. A slow follower stalls its partition's writes until it is put out of the set. With too few in sync, writes are refused, which is consistency over availability, and here the promise asks for it. Say the rule as one thing: "Committed means every copy in the in-sync set has it, and at least two do. The next leader comes from that set, so a yes survives losing any one broker or zone."
Twenty-One Brokers and One Rule
Producers send to the leader of each partition, chosen by key, and each leader copies to two followers in other zones. That is 400 partitions on 21 brokers, in append-only files kept seven days. A consensus service sits beside the path. Consumer groups fetch up to the committed offset, and save their offsets to the cluster under the same rule as any message, so losing a broker does not lose a group's place.
Only the producer and consumer boxes survive from the first design. The queue for each group became one log and, for each group, one offset per partition, so a slow group still waits alone. The lease went too, since a partition has one reader in its group and there is nobody to hide a message from. The figure marks the rest.
Three things on that board are still promises, so name them before the interviewer does: "How a log file is laid out, why the in-sync rule over a majority, and what happens when a group rebalances: those are my three deep dives."
- Keeping per-message acknowledgements on a log out of habit, then being asked to multiply them out. The answer is 11 TB, and you are holding it.
- Saying "replicated" without saying when the yes goes back. The interviewer's next question is exactly that, and it decides what a failover loses.
- Letting consumers read all the leader has. A failover then takes back what a consumer already acted on.
- A partition count from today's traffic, then finding out what adding partitions does to keys, in front of the interviewer.
- The bookkeeping multiplied out: 11 TB against 16 KB.
- "Committed" given a definition, with the next leader's choice following from it.
- The round trip on each yes named as the price of the promise.
Knowledge Check
You have swapped deletion on acknowledgement for a seven-day log. What is the main thing that costs?
- A slower read for every group, since old messages push the new ones back
- About 420 TB of disk, whether or not anyone ever reads a message again
- About 11 TB of bookkeeping, to record which group has read each message
- A second write for every message, to mark it read once a group is finished
A consumer saves its offset, handles 3,000 more messages, and restarts. What happens next?
- Nothing repeats, because the broker knows what the consumer has finished
- The whole partition starts again, since a restart throws the offset away
- Those 3,000 are skipped, since the log moved on during the restart
- Those 3,000 are handled again, from the last saved offset onward
One key sends far more than a consumer can apply, and its order must hold. What happens?
- Its partition takes all of it, since one key must stay in one log to stay in order
- The broker splits the key across partitions, which spreads it and keeps the order
- Adding partitions helps, since the hash then moves this key somewhere with room
- Adding consumers helps, since several of them can then read that partition together
Why do consumers read only up to the committed offset?
- To keep fetches cheap, since the newest part of the log is the slowest to read
- To let the leader delete what was read before the followers have to copy it
- So that no consumer acts on a message that a failover could still take back
- To keep each group's consumers exactly level with those of the other groups
You got correct