Message Queue: Deep Dives and Curveballs
You closed the last stretch owing three answers: how a log file is laid out, why the in-sync rule over a majority, and what a rebalance does. Minutes thirty to forty-five pay them in that order. Then the interviewer tries four requirements a log was not shaped for, and each gets the smallest change that leaves the board standing.
The Log on Disk
Start with the layout you parked. A partition is a run of files called segments. Assume 1 GB each. At 0.5 MB a second one fills in about 33 minutes, and a week is about 350 of them. Only the newest is written.
Retention deletes the oldest segment whole, so expiry never touches a message. A segment can go only once its newest message is seven days old, so a partition carries up to one segment past the week. That is 1 GB, times 400 partitions, times three copies: about 1.2 TB, and one reason 21 brokers was a floor.
Finding an offset takes three steps. A segment's name gives its first offset, which picks the file. A small index holds a position every few kilobytes, which lands you close. A short scan does the rest. A second small index, from time to offset, is how a time finds its place.
A log can run a work queue's shape on top, as Chapter 6's Queues, Logs and Pub-Sub said of share groups, and then it pays the bookkeeping again. So choose offsets, while one reader takes a partition in order. The trigger for changing is tasks of very different lengths, where one slow task must not hold up the rest. Then use a work queue.
Offsets keep one number per partition per group, so the bookkeeping is nearly free, a replay is a rewind, and one bad message blocks its partition. The way past it is a dead-letter topic: after a few failed tries the consumer writes the message there with its error, saves the offset past it and goes on, and the message's key loses its order for that one message. Use them when messages are processed in order and several groups read them.
Per-message acknowledgements track each message, so one can fail alone, at a cost that grows with every message. Use them when each message is an independent task for any worker.
What Committed Means
Now the in-sync rule, walked with offsets. The leader and both followers have 1,000, so 1,000 is committed, and that is as far as any consumer can read. The leader takes 1,001. One follower has it and the other does not, and the leader dies before saying yes.
The next leader comes from the in-sync set, and either follower qualifies. If it is the one with 1,001, that message is committed once the other catches up. If it is the one without, 1,001 is gone. Either way nobody was told yes, and no consumer ever saw it, so no promise broke.
The producer heard nothing, so it retries. If 1,001 is gone, the retry takes its place, and if it survived, that is a duplicate. The producer numbers its sends and the broker drops what it already holds, as Chapter 6's Delivery Semantics at Scale describes. The number lives in the log beside the message, so whichever follower now leads has it too. So the produce call carries it, and it can also be answered with a refusal.
produce(topic, key, value, producer_id, sequence)
-> yes
-> refused: too few in sync
The old leader returns. It sees a higher leadership number and knows it was deposed. It cuts its log back to where it agrees with the new leader, and follows from there. Its 1,001 was never committed, so the cut loses nothing acknowledged.
The alternative is a majority per partition, with one leader still ordering the log, the tool of Chapter 5's Leader Election and Consensus and not the leaderless kind in Chapter 4's Quorums: one slow follower never stalls a write. The cost is copies. A majority survives two failures at every moment only with five copies, which is 700 TB. Three copies under the in-sync rule, 420 TB, survive two only while all three are in sync. Once a follower has dropped out, a yes sits on two copies and survives one. Survive here means keeping what was acknowledged, and taking new writes is a separate question. With two of three copies gone, the in-sync rule holds the data on the last copy and refuses writes, because it is below its floor of two. Five copies with a majority lose two and keep writing.
So choose the in-sync set, while disks are the bill, a second failure is worth covering on the days all three are in sync, and a stall is tolerable. Assume a slow follower is put out of the set after 10 seconds, which is how long its partition's writes wait. The trigger for changing is a latency target those 10 seconds would break. Say it with the bill: "While all three stay in sync, surviving two failures takes three copies, against five with a majority. A floor of two always keeps the one failure we promised, so I'll pay the occasional stall to save those 280 terabytes, unless the latency target says otherwise."
Expect the reply: why not a majority of three? It is the same 420 TB with no stall, and it always survives one failure, which is all Scope asked for. On the agreed promise it ties on disks and wins on stalls. What the in-sync rule buys is the second failure on the days all three are in sync, paid for in those stalls. That margin is narrow, and you name it before the interviewer does. If the latency target rules out the stalls, this is the design you move to, with no new disks.
One case remains: every in-sync copy is gone. You can wait for one to return, or take a lagging copy and lose what it lacks. That is the product's call, and this promise says wait.
Order, Parallelism and Rebalancing
Now the agreement you parked at the ceiling. Take the group Scope sized: 400 consumers on 400 partitions, one each, at half their limit at peak. One consumer dies. Each consumer sends a heartbeat to one broker that keeps the group's list of members, stored in the cluster like its offsets, so losing that broker moves the job to another. When the heartbeats stop, that broker hands the partition out again.
If the group stops to reassign, assume that takes 30 seconds. At a peak of 400,000 a second, that is 12 million messages behind. The group can take 800,000 a second, so it has 400,000 to spare, and it clears almost all of them in about 30 more seconds. The exception is the consumer now holding two partitions, which has nothing to spare at peak. One dead consumer has cost every partition about a minute.
If only its partition moves, 399 keep flowing, at the price of a more careful protocol: that broker takes back only this partition, and every other consumer keeps reading. With the same 30-second wait, the moved partition is 30,000 messages behind, not 12 million. Its new reader now holds two partitions, and at peak those are all of its 2,000 a second, so that backlog clears when the peak falls or a replacement consumer starts.
Either way, a moved partition restarts from its last saved offset. Assume a group saves every 5 seconds. At 1,000 messages a second on a partition at peak, that is up to 5,000 messages handled twice.
Then the key, where order and parallelism meet head on. One key at 5,000 a second, over a consumer's 2,000, falls behind by 3,000 a second, for good, and no rebalance helps, because it has one reader. Its partition also holds ten times the average bytes, 3.5 TB a week against 350 GB. You can split the key and give up its order, or give it a faster consumer. Say which the product can bear.
Curveballs
"Compliance wants strict global order across this topic." Ask what the order protects, because per-key is usually what is meant, as Chapter 6's Delivery Semantics at Scale prices it. True global order is one partition and one reader: 2,000 a second, a hundredth of 200,000. If a small stream truly needs it, give it its own topic.
"One consumer group is three days behind." That is within retention, so nothing is lost, and the next thing to say is how long it has. Three days at the average rate is 60 billion messages. With all 400 consumers the group takes 800,000 a second, and 200,000 keep arriving, so it gains 600,000. That is about 100,000 seconds, a little over a day.
That day is the fastest case. Throttle the group's cold reads, because old segments compete for the disks other groups use, and a slow group may not slow the others. Set its pace on whatever it writes to, since a backlog is a burst there, as Chapter 6's Backpressure and Load Shedding warns. At half that gain it takes a little over two days. Alarm on the oldest message's age, because at three days behind you have four left.
"Some messages should arrive in an hour, not now." A log cannot hold one message back without holding what is behind it. So use delay topics: a minute, five minutes, an hour. A forwarder reads each one and waits until its oldest message is due, which works because everything in a topic waits the same time, then produces it to the real topic. The cost is a delay rounded to a tier and a second write. Arbitrary times at volume are the trigger for a timer store of their own.
"We need exactly once into a database." The broker's promise ends at its edge. Keep the offset in the same database transaction as the effect, or store a message ID with it, both from Chapter 6's Delivery Semantics at Scale. Neither is the broker's feature, and you say that.
How Each Level Answers
The mid-level candidate gets to a log on disk and to partitions with some prompting, keeps per-message acknowledgements until the multiplication is shown, and names replication. They need a hint to say when the yes goes back.
The senior candidate defines "without losing" in Scope and makes "committed" follow from it. They multiply out the bookkeeping, say that retention sizes the fleet, and walk a failover with offsets, including the leader that returns.
The staff candidate treats the broker as a platform many teams depend on. They ask who pays for a week of retention, who chooses a topic's partitions, and what a poison message means when its team is asleep. They say where a log is the wrong shape, weigh one outage's blast radius across topics, and ask whether one cluster should be one cluster.
- Reciting a product's settings by name. The interviewer asked for the design and hears a manual.
- Promising global order when asked, then being shown the 2,000 a second.
- Letting consumers skip messages to build delay, and losing what an offset means.
- Claiming exactly once as the queue's feature, without saying where it stops. The next question is where, and the answer is outside the broker.
- One failure walked with actual offsets, including what the returning leader does.
- A consumer's lag turned into a catch-up time, not "it will catch up".
- A requirement sent elsewhere when a log is the wrong shape for it.
Knowledge Check
"Compliance wants strict global order across this topic." What is the best move?
- Promise it, with a sequence number that every consumer sorts on as it reads
- Ask what the order protects, then price true global order at 2,000 a second
- Keep 400 partitions, and have consumers merge them back by producer timestamp
- Cut the topic to one partition at once, since compliance outranks the rate
A group is 60 billion messages behind. It takes 800,000 a second while 200,000 keep arriving. How long until it is level?
- About 75,000 seconds, since 800,000 a second clears the 60 billion
- About 300,000 seconds, since three days behind takes three days to read
- About 100,000 seconds, since it gains 600,000 a second on the log
- It never gets level, since that backlog outlives the seven-day log
Some messages should arrive in an hour. Where does such a message wait?
- In the real topic, with each consumer skipping it until its time comes
- In the producer's memory, which holds on to it and sends it an hour later
- In the consumer's memory, which holds on to it after the fetch until it is due
- In a delay topic for that tier, until a forwarder produces it to the real one
"We need exactly once into a database." What does that take?
- The offset or a message ID written in one transaction with the effect
- A broker setting that removes duplicates before they can reach a consumer
- The producer's numbered sends, which stop a message arriving a second time
- A consumer that saves its offset before it does the work on each message
You got correct