Design thread-safe Pub-Sub system using send(topic, msg) and receive(topic) APIs

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-03-23

Problem Statement

Design a Publish-Subscribe (Pub-Sub) system where multiple publishers can send messages to topics, and multiple subscribers can receive messages from topics. The system should be thread-safe, support concurrent publishers and subscribers, and provide APIs to send and receive messages.


Functional Requirements


1.Publish Message
•send(topic, message) → Publisher sends a message to a specific topic.
•All subscribers of that topic should eventually receive the message.
2.Subscribe & Receive Message
•receive(topic) → Subscriber receives the next available message from the topic.
•Support multiple subscribers per topic.
•Optionally, support acknowledgment to ensure reliable delivery.
3.Topic Management
•Topics can be created dynamically.
•Support multiple topics simultaneously.
4.Message Ordering
•Messages for a topic should be received in the order they were sent (per topic).
5.Optional Features
•Message retention (store messages for late subscribers).
•Durable subscriptions or ephemeral subscriptions.
•Capacity limits per topic.


Non-Functional Requirements


•Thread Safety: Handle concurrent publishers and subscribers without race conditions.
•Scalability: Support high-throughput messaging and multiple topics.
•Low Latency: Minimize message delivery delay.
•Memory Efficiency: Efficiently manage queues per topic.


Edge Cases & Constraints


•Concurrent send calls to the same topic.
•Concurrent receive calls from multiple subscribers.
•Subscriber requests when no messages are available → block, wait, or return empty.
•Topic with very high volume → prevent memory overflow.


Design Hints


•Key Classes / Interfaces:
•PubSubSystem → main API exposing send(topic, msg) and receive(topic).
•Topic → maintains a thread-safe queue of messages.
•Subscriber → represents a subscriber (optional for tracking).
•MessageQueue → internal structure to store messages per topic.
•Data Structures:
•Concurrent Queue / BlockingQueue → to maintain message order per topic and allow safe concurrent access.
•HashMap<Topic, Queue<Message>> → map topics to their queues.
•Design Patterns:
•Observer Pattern → subscribers notified when messages arrive.
•Singleton → for a centralized Pub-Sub system (optional).


Example Flow


1.Publisher calls send("Sports", "Goal by Player X") → message added to Sports topic queue.
2.Subscriber calls receive("Sports") → retrieves the next message from the queue.
3.Multiple subscribers calling receive("Sports") → each receives messages according to delivery semantics (e.g., broadcast or queue-style).
4.Concurrent publishers sending messages → queues handle thread-safe insertion.


Evaluation Areas


•Clean class design and modular APIs.
•Proper thread safety and concurrency handling.
•Correct message ordering per topic.
•Extensibility to add features like message retention, filtering, or durable subscriptions.


Solution

Comments