Lesson 2 · Minutes 10-20

Message Queue: First Design

Problem

The word "queue" is an honest place to start, so minutes ten to twenty take it at its word. You draw one broker process, a queue in memory for each consumer group, and a yes as soon as a message has joined them. There is no log here yet and no second machine, and you do not apologise for either.

Chapter 6's Queues, Logs and Pub-Sub already supplied the two parts of this: a copy for each subscriber, and the lease. You walk one message through them, price the design at a size where it is enough, and say exactly which failure it leaves open. The interviewer then pushes four times. Those pushes are the quiz at the end, in the order Scaling It answers them.

A Queue for Each Group

One broker process holds, for each topic, an in-memory queue for each consumer group, so each group has its own copy of every message. A producer's message joins all of those queues, and the producer is answered yes.

A consumer takes a message, and the broker hides it from the rest of the group for a lease, a stated time to finish the work. If the consumer acknowledges it, that group's copy is deleted. If the lease lapses first, because the consumer crashed or stalled, the copy comes back for another member of the group.

The first design
Group Aacknowledges Group Bcrashes, gets it back One brokera queue per group Restartmemory is gone Producergets a yes message take ack lost readwritefailure

One Message, Two Groups

Follow one message on a topic with two groups. The producer sends it. The broker puts one copy in Group A's queue and one in Group B's, and says yes.

A consumer in Group A takes its copy, does the work and acknowledges. That copy is deleted. Group B's copy is untouched, because the two queues share nothing.

A consumer in Group B takes its copy and crashes. Nobody acknowledges, so the lease lapses and the copy reappears in Group B's queue. Another consumer takes it and acknowledges. Some of that work may now have run twice, the duplicate that Chapter 6's Delivery Semantics at Scale expects, and nothing was lost.

Enough at a Thousand a Second

This is a fair answer at a stated size. A crashed consumer loses nothing, and a slow group waits in its own queue without slowing anyone else. Assume 1,000 messages a second at 1 KB, which is 1 MB a second, with consumers keeping up. Two seconds of backlog is 2 MB of memory.

For background work that can be recomputed, this is enough, because after a bad restart someone runs the job again. Say so, and say what the design has not earned: it keeps "without losing a message" against a consumer's crash, and against nothing that happens to the broker.

What It Has Not Earned

Messages live in one process's memory. A group stopped for a day holds 100 GB there, since 1 MB a second runs for about 100,000 seconds, and that is the whole memory of a mid-size machine. Acknowledged messages are gone, so nobody can read them again, which leaves a group's restart from an earlier position unmet. Consumers sharing one queue finish out of order, so order per key does not hold yet either. And one broker is both the ceiling and the failure. You fix none of this yet.

Offer the worst of these as a count of lost messages, in about ten seconds: "This keeps the promise against a consumer crash, not a broker crash. At a thousand a second with two seconds of backlog, a restart loses about two thousand messages, so that crash is what I'd deal with next."

Then flag the word you have been using on trust: "I'm calling this a queue for now. Whether a queue is the right shape at all comes up as soon as someone asks to read yesterday."

Interview Pitfalls
  • Drawing a partitioned, replicated log at minute ten. It is the right destination, and the interviewer learns nothing about why you went there.
  • Saying the first design meets "without losing a message". The next question is which failure it meets it for, so say that first.
  • Treating the two groups as a detail, then deleting what one group still needs when the other acknowledges.
  • Leaving the broker's own crash for the interviewer to find. It is the first thing they will push on, and it was yours to offer.
What Interviewers Listen For
  • Exactly which failures the first design survives, and which it does not.
  • One message walked to two groups, one acknowledged, one redelivered.
  • The limit given as a count of lost messages.

Knowledge Check

"You restart the broker for a deploy. Where are the messages that nobody had taken yet?"

  • "Gone, so have producers hold on to every message until a consumer confirms the work."
  • "Snapshot the in-memory queues to disk every minute, and reload them on the way back up."
  • "On disk: append each message to a file before acknowledging it to the producer."
  • "In a replicated cache, since memory is so much faster than any disk that we could use."

"A new consumer group needs every message too, and it wants to start from yesterday. What has to change?"

  • "Stop deleting on acknowledgement: keep a week's log and let each group hold an offset."
  • "Copy each message into a queue per group, and have the broker track each one's acks."
  • "Keep an acknowledged bit per group on every message, and scan for the first unset one."
  • "Archive acknowledged messages to object storage, and replay them through the queue."

"One broker's disk and one broker's network link are now your ceiling. How do you get past them?"

  • "Compress messages in batches at the producer, which makes each disk and link go further."
  • "Move old segments to object storage, so that the one broker's disks never fill up."
  • "Let any broker take any message, and have consumers merge them back by timestamp."
  • "Split each topic into partitions across brokers, and route each message by its key."

"The broker leading a partition acknowledges a write and dies in the same instant. Is that message gone?"

  • "No, it is on that broker's disk, and the partition returns when the broker restarts."
  • "No, because we say yes only once the in-sync copies hold it, and one of those leads next."
  • "Possibly, but the producer retries on a timeout, so any loss is covered by the retry."
  • "No, since followers copy it in the background a millisecond or two after the yes."

You got correct