Pub Sub System
Publish-Subscribe Message Broker
Design an in-process publish-subscribe broker: topics, named subscriptions that each get every message, worker groups that share one subscription, acknowledgements with redelivery, and a dead-letter list.
Overview
A publish-subscribe broker sits between the code that produces events and the code that reacts to them. A publisher sends a message to a named topic and does not know who reads it. Each reader creates a subscription on the topic, and the broker gives every subscription its own copy of every message. The publisher and the readers never refer to each other, so a new reader is added without touching the publisher.
Interviewers use this problem to test four ideas. Fan-out and sharing: every subscription gets every message, but the workers behind one subscription split its messages between them. Delivery guarantees: what happens when a worker takes a message and crashes before finishing it. Isolation: a slow reader must not slow down the publisher or the other readers. Thread safety: many publishers and workers call the broker at once.
A strong answer separates the topic (where messages go in) from the subscription (where they wait to be read), makes delivery pull-based so the broker never runs reader code while holding a lock, and states the guarantee it gives (at least once) together with what the reader must do about duplicates.
Scoping the problem
Before drawing anything, ask seven questions. Each one changes the design, and each has a usual answer you can propose if the interviewer leaves it to you.
- Does every reader get every message, or do readers share the work? This decides fan-out versus a work queue, and most systems support both. The usual answer is both: each subscription gets every message, and the workers on one subscription share them.
- Push or pull? With push the broker calls the reader, which runs reader code inside the broker. With pull the reader asks, which lets each reader go at its own pace. Choose pull; push can still be offered as a thread that polls and calls a callback.
- Which guarantee: at most once, at least once or exactly once? This decides whether acknowledgements and redelivery exist. The usual answer is at least once, with idempotent readers.
- Is order required? A total order is cheap in one process, but across partitions it holds per partition only. Promise that messages reach each subscription in publish order, and say that redelivery can reorder them.
- What happens to a message that keeps failing? Without a limit it is retried forever and blocks others. Move it to a dead-letter list after N deliveries.
- Can a subscription filter? A filter saves readers from discarding what they do not want. Yes: a predicate on the message headers.
- Must messages survive a restart? Durability adds a log on disk and offsets. Treat it as a follow-up, not the core.
Functional requirements
- create_topic(name) and delete_topic(name)
- subscribe(topic, name, filter, ack_timeout, max_deliveries) returns a named subscription; subscribing twice with the same name returns the same one
- publish(topic, payload, headers) gives the message an id and copies it to every subscription whose filter accepts it
- poll(member, now, max_messages) hands ready messages to one worker; workers on one subscription never get the same message at once
- ack(message_id, member) finishes a message only for the worker that holds it; expire(now) returns late messages to the front of the queue, or to the dead letters after max_deliveries
- Publishing to an unknown topic raises UnknownTopic
Non-functional requirements
- Decoupling: publishers never know which subscriptions exist
- Isolation: publish only appends to queues; a slow or stopped reader never blocks the publisher or other subscriptions
- Order: every subscription receives messages in publish order; only redelivery can reorder
- Thread safety: one broker lock and one lock per subscription, always taken in that order
- At-least-once delivery with a bounded number of retries; time is injected so tests are exact
Interview questions
- Your broker is at least once. How does a reader avoid charging a customer twice?
- How would you keep order per customer while still using several workers?
- Why pull instead of push? What does a slow subscriber do to your broker?
- How would you make messages survive a restart and let a new subscription read old messages?
- What should happen to a message that fails every time?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio