Design a Pub/Sub System
📣 What is a pub/sub system? It is a post office for messages inside a program or between services. Publishers drop a message into a named topic, such as
orders, without knowing who will read it. Everyone who subscribed to that topic gets a copy. Publishers and subscribers never talk to each other directly, so either side can be added, removed or slowed down without changing the other.
You will also hear this problem asked as "design an event bus" or "design async message delivery". They are the same design. An event bus is a pub/sub broker that lives inside one process, where topics are often event types instead of strings. Async delivery is the part where publish returns right away and the subscriber gets the message a little later, on another thread. We build the broker first, then make delivery asynchronous and thread safe in the Implementation section, and turn it into a typed event bus as the first Extensibility follow-up.
Requirements
"Design a pub/sub system. Publishers send messages to topics, and subscribers receive the messages of the topics they subscribed to."
Short prompt, many hidden decisions. Most of them are about time: when does a subscriber get a message, what if it is slow, and what if it was not there yet when the message was sent?
Clarifying Questions
You: Should publish wait until every subscriber has handled the message?
Interviewer: No. Publishing must be fast and must not depend on how slow a subscriber is. Delivery happens asynchronously.
You: Does a new subscriber get messages that were published before it subscribed?
Interviewer: No history. A subscriber only gets what is published while it is subscribed.
You: Can one subscriber listen to several topics? In what order does it get the messages?
Interviewer: Yes, it can. It should get its messages in the order they were published, across all its topics.
You: What happens when a subscriber falls behind and messages pile up?
Interviewer: Memory is limited. Each subscriber can buffer a fixed number of messages. If it falls further behind, we drop its oldest messages, because new data matters more here, but we keep a count so someone can alert on it.
You: Do messages have to survive a crash, or be acknowledged and retried?
Interviewer: No. Keep it in memory. Best effort is fine for this system.
You: What happens to messages already waiting for a subscriber when it unsubscribes?
Interviewer: It stops getting new ones. It can still read what it already received.
You: Will publishers and subscribers run on different threads?
Interviewer: Yes. Many threads publish at once, and delivery runs in the background.
The fourth answer is the one that shapes the design most. "Drop the oldest" is a real policy that real systems use for things like price ticks and sensor readings, where a stale value is worthless. For payments or orders it would be the wrong answer, and that system is a message queue. Asking this question shows you know the difference.
💡 Interview tip: Pub/sub prompts always hide a "slow consumer" question. Ask it yourself: block the publisher, drop messages, or grow without limit? Each answer gives a different system, and the interviewer wants to see that you know there is a choice.
Final Requirements
- Topics are created by name. Creating a topic that already exists is an error.
- A subscriber can subscribe to and unsubscribe from any number of topics. Subscribing twice to the same topic, or to a topic that does not exist, is an error.
- Publishing a message to a topic gives a copy to every current subscriber of that topic and returns how many that was. Publishing to an unknown topic is an error.
publishnever waits for a subscriber. Messages wait in a per-subscriber inbox and are delivered later, oldest first, in publish order.- A subscriber only gets messages published while it is subscribed. Unsubscribing keeps what is already in its inbox.
- Each inbox holds at most N messages. When a message arrives at a full inbox, the oldest one is dropped and counted.
- The system is safe to use from many publisher threads and background delivery threads at once.
Out of scope
- Persistence, acknowledgements and retries (that is the message queue design).
- Delivery across machines and the network protocol.
- Message history and replay for late subscribers.
- Authentication and per-topic permissions.