Message Queue: Scope
One phrase in this question carries the whole design, and so far it means nothing: "without losing a message". Minutes zero to ten go on pinning it down, and then on sums with a surprise in them, because a system whose job is moving messages turns out to be sized by what it keeps. The lesson ends on a first API of three calls, two of which will not last.
The question assumes a few words. A broker is the server that holds messages between the two sides. A producer writes to it, and a topic is a named stream of messages. A consumer reads, and a consumer group is a set of consumers that share the reading of one topic, so that every group gets every message and one member of each group handles it.
Five Questions, One of Them a Definition
Each of these changes what you draw. Each is listed with what it changes and the answer this lesson agrees.
- How many messages a second, and how large? This sets the bytes on every disk and every link. Agreed: 200,000 a second at 1 KB each, ten times the stream in Chapter 6's Queues, Logs and Pub-Sub.
- What does "without losing a message" promise, exactly? This sets the rule for when a producer gets its yes. Agreed: once a producer has a yes, the message survives losing any one broker or zone.
- How many consumer groups, at what pace? This rules out deleting a message when it is acknowledged. Agreed: five groups, each reading everything at its own pace.
- How long are messages kept? This sets the disks. Agreed: seven days, read or not.
- Does order matter, and across what? This sets the key that decides where a message goes. Agreed: order per key, and nothing wider.
Slow down on the second one, because the question sounds as if it had already answered it. Put your reading on the table and ask: "'Without losing a message' can mean a lot of things. I'll take it as: once the producer has a yes, the message survives losing any one broker or zone. Is that right?"
Requirements, and Two Things Left Out
Three requirements are functional. A producer sends a message with a key and gets a yes. A group reads every message. A group can restart from an earlier position. Three are non-functional. Nothing acknowledged is lost with one broker or zone gone. Order holds per key. A slow group never slows producers or other groups.
Two things go off the board out loud. Real brokers and their settings are one, since the interviewer asked for a design and not a manual. A consumer's own effects are the other: what a consumer does with a message, and whether doing it twice matters, is Chapter 6's Delivery Semantics at Scale. That one returns as a curveball, and the answer stays where that lesson put it.
Bytes In, Bytes Kept, Bytes Out
Start with the writes. Two hundred thousand messages at 1 KB is 200 MB a second. With the peak assumed at twice that, it is 400 MB a second.
Now the reads, which candidates undercount. Five groups each read everything, so 200 MB a second in becomes 1 GB a second out, and 2 GB at peak. Links work harder going out than coming in.
Then the part the question never mentioned. A day is about 100,000 seconds, so 200 MB a second is 20 TB a day. Seven days is 140 TB. Assume three copies, which the fourth probe will have to earn, and that makes 420 TB. It is ten times Chapter 6's 42 TB, and that lesson's conclusion holds at this size: a log is sized by what it keeps.
Assume 20 TB of disk on each broker. Four hundred and twenty over 20 is 21 brokers. That is a floor, with every disk full to its last byte, and you say so.
Check the network before you trust that answer. One 10-gigabit link moves 1.25 GB a second, and assume you run it at half, so 625 MB. At peak the brokers send 2 GB a second to the groups. They also send two extra copies of 400 MB a second, another 800 MB. That is 2.8 GB a second, which takes 5 brokers, far fewer than the disks need. So the network does not decide this fleet, and the sentence to say is: "The links need five brokers and the disks twenty-one, so this fleet is sized by what it keeps."
The last number comes from the slowest thing in the system. Assume one consumer applies 2,000 messages a second, as in Chapter 6's Delivery Semantics at Scale. The peak of 400,000 a second then takes 200 consumers, with nothing to spare. At half load, it takes 400. A group gives each partition, a slice of the topic with its own order, exactly one reader, so 400 consumers need 400 partitions. Nothing is split yet, and the number waits for the third probe.
The estimator below starts on these numbers: 20 billion messages a day is 200,000 a second over a 100,000-second day. Its two decision lines are the 21 brokers and the 400 partitions, the second dividing the peak by 1,000, half of one consumer's rate. Its bandwidth out counts the groups only, so the 800 MB to the other copies stays a sum you add by hand. Drag the bytes a message down to 100. The brokers fall to 3, and the partitions do not move, because one count comes from bytes and the other from a rate. The agreed number is per second, so stay on the rounded day. The exact day turns the same 20 billion into 463 partitions, which is rounding and not more traffic.
Three Calls to Begin With
Begin with the API the word "queue" suggests. A producer sends a topic, a key and a value, and is answered yes. A consumer takes a message for its group. It then acknowledges that message. Scaling It replaces the last two, so nothing else should lean on them.
produce(topic, key, value) -> yes take(topic, group) -> message acknowledge(topic, group, message)
Three entities sit behind those calls. A topic is the named stream. A message has a key, a value and a time. A consumer group is a name its members share, and it is what the broker keeps a reader's progress against. The key exists for order: messages with one key stay in order, and nothing promises more than that.
- Accepting "without losing a message" as it stands. Every later choice depends on what it covers, and the interviewer will ask which failure you meant.
- Sizing by throughput alone. The links ask for 5 brokers, so you draw a quarter of the fleet, and a week of retention arrives as a probe you have no answer to.
- Forgetting that five groups read five times. Your bytes out come to 200 MB a second where the real number is 1 GB.
- A partition count from habit instead of the slowest consumer's rate. Asked why that number, you have no sum to show.
- The durability promise made precise before anything is drawn.
- 420 TB and 21 brokers reached aloud, with the link dismissed for a stated reason.
- The partition count derived from one consumer's rate.
Knowledge Check
One question can go first. Which answer reshapes the most of what you will draw?
- What a yes to the producer promises when a broker or a zone is lost
- How large each message is, 1 KB on average or closer to 10 KB
- Which programming language each of the five consumer groups is written in
- How many days each message is kept, seven days or closer to thirty
Messages are kept seven days, read or not. What does that do to the fleet?
- Nothing, since the 10-gigabit links set how many brokers you need
- It raises the partition count, since older messages need more of them
- It sets the broker count: 420 TB over 20 TB a broker comes to 21
- It forces every group to finish reading before the seven days run out
Three copies and five groups each multiply the same 200 MB a second. What does each one multiply?
- Both multiply the disks, to fifteen stored copies of every single message
- Copies multiply what goes to consumers, and groups multiply what sits on disk
- Both multiply what producers send in, which comes to 3 GB a second at the door
- Copies multiply what is kept and sent between brokers, and groups what goes out
What should the partition count come from?
- The disks: spread 420 TB so that each partition holds about 1 TB
- One consumer's rate: 400,000 at peak, 2,000 each, run at half load
- The brokers: 21 of them at about twenty partitions each, by habit
- The groups: five of them, with eighty partitions set aside for each
You got correct