Queues, Logs and Pub-Sub
A message between two services is a call that nobody waits for. The sender hands the work to a broker and answers its own caller at once. The receiver takes the work at its own pace, even if it was down when the work was sent. You buy time, rate and fan-out, and you pay for them with a third system and a result that arrives later.
Messaging comes in three shapes, and they differ in three promises: who gets a message, how long it is kept, and whether it can be read again. An arrow on the board marked "queue" says none of that, and the interviewer will ask which one it means.
A Message Instead of a Call
A broker between two services decouples three things. Time: the consumer can be down for an hour while the producer keeps working, and the messages wait for it. Rate: a burst waits in the broker instead of failing at the consumer, up to a limit that the third lesson of this chapter works out. Count: one event can have many readers, and the producer does not need to know how many.
The condition is that the caller's answer does not depend on the work. The cost is that the work finishes later, so the user sees it as pending, and the broker is one more system that can fail. The trigger is a step the response does not need. The URL shortener in Chapter 7 is the plain case: the redirect answers at once, and the click event goes to a log that a separate pipeline counts.
How one service hands its own slow work to a queue and a worker is Backend Deep Dive's chapter on background work. This lesson starts where several services share the broker.
The Work Queue
A work queue hands each message to one of many competing workers. When a worker takes a message, the broker hides it from the others for a lease. If the worker acknowledges, the broker deletes the message. If the lease runs out first, because the worker crashed or stalled, the broker hands the message to another worker.
Its promises are short. One worker holds a message at a time, there is no order across workers, and nothing is kept after the acknowledgement, so there is nothing to replay. That fits tasks any worker can do and nobody needs again: a video to transcode, an email to send. If a worker finishes a task and its acknowledgement is lost, the lease runs out and the task is delivered a second time; the next lesson makes that second delivery harmless.
The Log
A log is an append-only sequence per partition, kept for a retention period whether or not anyone has read it. Each reader keeps its own offset, its position in the sequence, so readers are independent of one another and any of them can rewind. Order holds inside one partition, and the next lesson prices that order.
A consumer group gives each partition exactly one of its readers, so the partition count caps how many readers a group can run in parallel. Kafka is the common example of a log. Kafka 4.2 made share groups ready for production; they let consumers share a partition's records the way a work queue's workers do, and give up the order inside it: the work-queue shape run on a log. How a broker stores and copies the log is the design problem of Chapter 14; here it is a box with these promises.
The cost of a log is disk. Assume 20,000 events a second of about 1 KB each. That is 20 MB a second. A day is about 100,000 seconds, so one day of events is about 2 TB. Three copies make 6 TB a day, and a week of retention holds about 42 TB. That number sizes the brokers' disks, and it is the price of being able to replay a week.
Throughput decides nothing here. In a vendor's own benchmark, one cluster of three brokers carried about 600 MB a second of 1 KB messages, thirty times this stream. Say it as a vendor's number, and say what it shows: at this rate the log is sized by what it keeps, not by how fast it takes it in.
Publish-Subscribe
In publish-subscribe, every subscriber gets every message. There are two ways to build it. One gives each subscriber its own queue, and the broker copies every message into all of them. The other is one log that each subscriber group reads from its own offset. On a normal day the two deliver the same thing.
The difference shows when a subscriber is new or was down. Its own queue holds only what arrived after it subscribed, so a new subscriber starts from now and the history is gone. A log lets it start anywhere inside the retention, last week included. Either way, fan-out multiplies deliveries: three groups reading the 20,000 events a second make 60,000 reads a second, which is still only 60 MB a second.
Choosing the Shape
The shape follows from the requirements, in the words agreed in Scope. One worker per task and no replay is a work queue. Several independent readers, replay after a bug, or a reader that may fall days behind is a log. Every current subscriber and no history is publish-subscribe on per-subscriber queues.
Products blur these lines, and one product can often be run in more than one shape. So you name the shape and its promise first, and the product second, if at all. You might say: "Click events go to a log kept for a week, because two teams read them and one of them replays after a bug."
A work queue hands each message to one worker and forgets it once acknowledged. Use it when each message is a task any worker can do and nobody needs it again.
A log keeps every message for its retention and lets each reader keep its own place. Use it when more than one consumer needs the same events, or a consumer must be able to replay after a bug.
How Interviewers Probe This
A probe on an asynchronous arrow wants three things in turn: the promise the design needs, the shape that gives it, and its cost in a number.
The interviewer may start with "Why put a queue here instead of calling the service?" A good answer names what the caller no longer waits for and says what happens while the consumer is down for an hour: the messages wait, and nothing upstream fails. Then it gives the cost. The work completes later, and the user sees a pending state until it does.
A later push is "A second team wants the same events, including last week's." A good answer moves the arrow to a log, gives the retention in terabytes, about 42 for a week of this stream, and says the new team reads from its own offset without slowing the first.
The hardest is "The producer can't reach the broker. What happens to the request?" There are three honest answers. Fail the request. Hold the event in a bounded buffer inside the producer, which a crash empties. Or write the event in the same transaction as the data and relay it to the broker later, the outbox that Chapter 5 uses across services and Backend Deep Dive teaches inside one. A good answer picks one by what a lost event costs.
- "A queue makes the system faster." It makes the caller faster. The work takes as long as before, and the end-to-end delay now includes the wait in the queue.
- "Once the broker has the message, the work is done." Nothing has happened yet. The design has to say what the user sees while the message waits, and what they see after it fails.
- "More consumers read a log faster." A consumer group gives each partition one reader, so consumers past the partition count sit idle. The count is chosen ahead, for the parallelism the consumers will need.
- "A broker removes the dependency between two services." It moves it. The producer now depends on the broker, and the design says what the producer does when it cannot reach it.
- Name the promise each asynchronous arrow needs, one reader or many, kept or forgotten, before you name a broker.
- Size a log by its retention: bytes a day, times the days kept, times the copies. Then say whether throughput decides anything.
- Choose the partition count for the parallelism the consumers will need, because a consumer group never runs more readers than partitions.
- Keep the user's answer independent of the asynchronous work, and show work in flight as pending, never as done.
Knowledge Check
Two teams need every order event, and the second team wants to start from last week's events. Which shape fits?
- One work queue that both teams' workers take events from
- A log kept for a week, with one consumer group for each team
- A new queue for the second team, subscribed to the same topic
- Direct calls from the producer to each team's service in turn
A log takes 20,000 events a second of about 1 KB, keeps three copies and retains a week. About how much disk does it need?
- About 2 TB, the size of a single day of events
- About 14 TB, a week of the stream at one copy
- About 42 TB, a week of three copies of the stream
- Very little, since events are deleted once they are read
A topic has 12 partitions, and a consumer group grows from 12 to 20 consumers. What happens?
- The group reads about two thirds faster, with 20 readers sharing the work
- Eight consumers sit idle, because each partition has one reader in the group
- Every message is read twice, because the extra consumers repeat partitions
- The log splits its partitions into 20, one for each of the consumers
Pub-sub is built as one queue per subscriber. A new subscriber joins today. What does it receive?
- Every message from the past week, then everything new
- The last thousand messages, then everything published after
- Only the messages published after it has subscribed
- A share of new messages, split with the other subscribers
A synchronous call to a slow service becomes a message on a queue. What does the caller give up?
- Nothing, because the slow work itself now runs faster
- A finished result in its answer: the user sees pending
- Every message, because brokers drop what workers miss
- All its dependencies, since the broker is never down
You got correct