Design an in-memory message queue 00:00:00 Make a prediction You will distinguish independent subscriber progress from competing worker delivery and explain duplicate delivery after a crash. Subscriber A acknowledges message one while subscriber B has not read it. Can the broker remove that message solely because A acknowledged it? No in independent subscriptions. B still needs its own view of the message. Track progress per subscription or retain separate delivery state sufficient to honour the agreed retention policy. 00:00:34 One line of papers, and a peg for each reader A line of newspapers hangs on a rail. Each reader has a peg showing how far they have read. Moving Ana's peg cannot drag Ben's peg forward; they can read the same pages at different speeds. One shared log can support multiple independent cursors. A work queue pooling workers intentionally has different semantics: one item normally goes to one competing worker in the group. An in-memory message queue lets parts of one program talk without calling each other. A publisher appends a message to a topic. Each subscription reads all messages at its own speed. Direct calls tie the speed and the failures of one part to another. An order should not wait for a mail, and should not fail because of it. 00:01:20 Give each subscriber its own progress A publisher adds two messages to a topic. Subscriber A handles both while subscriber B is still working on the first. Store progress per subscription so A's acknowledgement does not erase B's unread messages. Each subscriber should observe the specified order. If B crashes after handling the first message but before acknowledging it, it may receive that message again. State the delivery guarantee and make repeated handling safe if needed. Let the first reader commit position ten while the other has only consumed two messages. The slow reader must still be able to fetch its third message if the contract promises retention. A global last-read position would make one subscriber erase another subscriber's progress. 00:02:07 A work queue distributes differently If two workers belong to one competing-consumer group, an item should normally be assigned to one worker in that group. Two independent subscriptions instead each receive their own view of the item. Ask the interviewer which model they mean before choosing a shared dequeue operation. Two independent subscriptions can both process the same publication. Two workers within one work-sharing group usually divide the work, with retries possible after failure. Ask which arrangement the prompt requires before claiming that a second delivery is either a feature or a bug. A worker can complete a side effect and crash before acknowledging. Redelivery can repeat the side effect, so explicitly design idempotent handling where the guarantee is at least once. Use a competing-consumer queue when work should be divided rather than broadcast. Apache Kafka consumers. A consumer sends its offset in the log with each request and gets a chunk from there. It controls its position and can rewind it. The peg of this design, on disk and over a network. Redis Streams. Consumer groups share a stream. What was handed out and not confirmed is in a pending entries list, and XAUTOCLAIM gives idle entries to another consumer: a lease that ends. 00:03:27 Try an interview follow-up A queue for each subscriber, or one log? I would choose one shared log when readers need replay and independent positions. A slow reader can then affect how long old entries must remain available. Separate queues are useful when subscribers need different limits or push delivery. I would state the retention promise before choosing between those designs. A subscriber performs its side effect for record eight and crashes before recording its new position. What happens when it restarts? The log may deliver record eight again because the committed offset trails the work. A durable deduplication key or an atomic relationship between processing and progress can prevent a duplicate effect. Keeping separate subscriber positions alone cannot guarantee exactly-once effects. 00:04:23 What to take with you A topic is one numbered log, and each subscription has its own position. A poll hands out a message with a lease and a receipt. Ack ends it, nack or silence offers it again, and after the last try it becomes a dead letter. The slowest subscription decides what may be thrown away. When that is not enough, the topic is full, and full must have a meaning: reject, wait or drop. One lock for each topic, handlers outside.