Quick revision
The short answer
The API creates topics or queues with partitions and retention, send(topic, key, payload) returns an id after a durable write, and consumers receive(subscription, max) and then ack(ids) or commit(offset). In a log model each partition is an append-only series of segment files and each consumer group tracks an offset, so replay and many subscribers are cheap and order holds per partition; in a queue model each message has a state with a visibility deadline, which suits independent tasks but costs more bookkeeping. Each partition is replicated to three nodes and acknowledged after a majority or all in-sync replicas have it, delivery is at least once, and 200,000 messages of 1 KB is 200 MB/s in and about 600 MB/s of disk writes.
What to remember
In this answer, we designed a message queue with an API for topics, durable sends, and consumption by offset commit or per-message ack. The log model stores partitions as append-only segments with one offset per consumer group, making replay and many subscribers cheap with order per partition, while the queue model tracks each message's state and visibility deadline, suiting independent tasks with per-message retries. Replicating each partition to three nodes and acknowledging after a majority or all in-sync replicas makes accepted messages durable, delivery is at least once so consumers must be idempotent, and 200,000 messages of 1 KB means 200 MB/s in and about 600 MB/s of replicated disk writes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The API creates topics or queues with partitions and retention, send(topic, key, payload) returns an id after a durable write, and consumers receive(subscription, max) and then ack(ids) or commit(offset). In a log model each partition is an append-only series of segment files and each consumer group tracks an offset, so replay and many subscribers are cheap and order holds per partition; in a queue model each message has a state with a visibility deadline, which suits independent tasks but costs more bookkeeping. Each partition is replicated to three nodes and acknowledged after a majority or all in-sync replicas have it, delivery is at least once, and 200,000 messages of 1 KB is 200 MB/s in and about 600 MB/s of disk writes.
Why this question is asked
Message queues sit between almost every pair of services in a modern system, so knowing how one works inside, and which kind to choose, is basic system design knowledge. Interviewers ask this to see whether we understand the two big storage models, offsets on a log and per-message acknowledgements on a queue, and whether we can explain durability and size a cluster. In this answer, we will define the API, look at the log model and the queue model in turn, compare them, make the system durable with replication, and size it for 200,000 messages per second.
1What the Queue Must Do and the API
Producers hand off messages and move on; consumers process them later at their own pace. The queue must not lose an accepted message, must absorb bursts, and must let a consumer that crashes pick up where it left off. The API reflects that: a topic or queue is created with a number of partitions, which are the units of parallelism and ordering, and a retention period. Sending returns only after the message is durably written. Consuming has two styles, matching the two storage models we will see next.
The key decides the partition, usually by hashing it, so all messages for customer 42 land in partition 17 in the order they were sent.
2The Log Model: Partitions, Segments and Offsets
In the log model, used by Apache Kafka, Apache Pulsar's storage and Amazon Kinesis, each partition is an append-only sequence of messages. Every message gets the next offset, a number that says its position. On disk the partition is a series of segment files of perhaps 1 GB each; new messages are appended to the newest segment, and whole old segments are deleted when they pass the retention period.
Consumers do not remove messages. Each consumer group, a set of consumers sharing the work, just records an offset per partition saying how far it has processed. That has big consequences. Many independent groups, billing, analytics and search indexing, can read the same topic without copies. A group can replay from an older offset after a bug fix. And because a partition is read in order by one consumer of a group at a time, order holds within a partition. The bookkeeping is tiny: one number per partition per group.
| Offsets | Content | Billing group | Analytics group |
|---|---|---|---|
| 0 to 902,099 | processed by billing | behind its offset | being read |
| 902,100 to 902,599 | in flight | committed up to 902,100 | already read |
| 902,600 to 905,000 | not yet read by billing | next | already read |
| older than 7 days | segments deleted | gone | gone |
3The Queue Model: Per-Message State and Visibility Deadlines
In the queue model, used by RabbitMQ, Amazon SQS and similar services, each message has a state. It is available until a consumer receives it; then it is in flight until a visibility deadline, for example 30 seconds. If the consumer acknowledges it in time, it is deleted. If not, because the consumer crashed or was slow, it becomes available again and another consumer gets it.
This suits independent tasks such as sending an email or resizing an image: each one succeeds or retries on its own, any number of consumers can share the work without partition assignment, and one slow task never blocks the others. The price is more bookkeeping, a state and a timer for every message, and weaker ordering, since a redelivered message comes back after later ones have been processed.
4Log or Queue: Which Fits Which Work
Neither model is better; they fit different work. Event streams that several teams consume, need replay or need order per key fit a log. Background tasks that are independent, need per-message retries and do not care about order fit a queue.
| Property | Log with consumer offsets | Queue with per-message ack |
|---|---|---|
| Many independent subscribers | cheap: one offset per group | copy per subscription |
| Replay old messages | yes, until retention | no, deleted on ack |
| Ordering | per partition | weak once redelivered |
| One slow or failing message | blocks its partition | only itself |
| Bookkeeping | one number per partition | state and timer per message |
5Durability and Delivery Guarantees
A message accepted from a producer must survive a broker failure. So each partition is replicated to three brokers, one leader and two followers, placed in different racks or zones. The producer sends to the leader, the followers copy from it, and the leader acknowledges the producer only after a majority, or in Kafka's terms all in-sync replicas, have the message. With Kafka this is acks=all together with min.insync.replicas=2, so a write needs at least two copies before it is acknowledged.
7 messages in order. Play them, or step through them one at a time.
Delivery is at least once. If a consumer processes a message and crashes before committing its offset or acknowledging, the message is delivered again. So consumers must be idempotent: processing a message twice must have the same effect as once, for example by recording processed message ids or using an upsert keyed by the order id.
6Sizing for 200,000 Messages per Second
200,000 messages of 1 KB is 200 MB/s of incoming data. With three replicas, the cluster writes about 600 MB/s to disk. Consumers add reads: if three groups read everything, that is another 600 MB/s of outgoing traffic, mostly served from the operating system's page cache because consumers read recent data.
A partition on one broker can comfortably take around 10 MB/s, so we need at least 20 partitions for the incoming rate and, with headroom for growth and parallel consumers, perhaps 48 or more. A broker with fast disks and a 10 to 25 Gbps network can handle a few hundred MB/s of combined traffic, so 6 to 9 brokers carry this load with room for a broker failure. All disk access is sequential appends and reads, which is why a log can move this much data on ordinary hardware.
7Follow-up questions
Why not delete messages from a log once all groups have read them?
Because the log does not track who read what message, only positions, and new groups or replays may need old data. Time or size based retention is simpler and makes replay possible; compacted topics keep only the latest message per key when we need a long-lived current state.
Can we get exactly-once delivery?
Not across arbitrary side effects. Some logs offer exactly-once within the system, for example Kafka's idempotent producer and transactions for read-process-write pipelines, but once a consumer calls an external service, idempotent processing is still what guarantees correctness.
How does a consumer group divide partitions?
Each partition is assigned to one consumer in the group, and the group rebalances when consumers join or leave. So the number of partitions caps the parallelism of a group, which is another reason to choose enough partitions up front.
8Conclusion
In this answer, we designed a message queue with an API for topics, durable sends, and consumption by offset commit or per-message ack. The log model stores partitions as append-only segments with one offset per consumer group, making replay and many subscribers cheap with order per partition, while the queue model tracks each message's state and visibility deadline, suiting independent tasks with per-message retries. Replicating each partition to three nodes and acknowledging after a majority or all in-sync replicas makes accepted messages durable, delivery is at least once so consumers must be idempotent, and 200,000 messages of 1 KB means 200 MB/s in and about 600 MB/s of replicated disk writes.
Open with
The API would be create topic with partitions and retention, send with topic, key and payload that returns an id only after a durable write, and then either receive followed by commit offset for log style, or receive followed by ack of message ids for queue style.
Then, if they want more
In the log model, each partition is an append-only sequence of segment files and each consumer group just tracks an offset, so messages stay until retention, replay and many independent subscribers are cheap, and order holds within a partition.
In the queue model, each message has a state: available, in flight until a visibility deadline, and acknowledged; unacknowledged messages reappear after the deadline. That suits independent tasks with per-message retry, but it costs more bookkeeping and weakens ordering.
For durability I replicate each partition to three brokers in different zones and acknowledge the producer after a majority or all in-sync replicas have the message, like acks=all with min.insync.replicas of 2 in Kafka. Delivery is at least once, so consumers must be idempotent.
For sizing, 200,000 one-KB messages is 200 MB/s in, about 600 MB/s of disk writes across three replicas plus consumer reads, spread over a few dozen partitions on perhaps 6 to 9 brokers, all with sequential disk I/O.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the API, storage model and delivery guarantees of a distributed message queue...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/8ce1ffdf64/