Design a Message Broker System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-06-09

Problem Statement

Implement a simplified Message Broker System (similar to Kafka/RabbitMQ) that allows producers to publish messages and consumers to subscribe and consume them. The focus should be on internal mechanics, thread management, and queue handling.


Functional Requirements


1.<span></span>Producer → Broker → Consumer Flow
2.<span></span>Multiple producers can publish messages to a topic.
3.<span></span>Multiple consumers can subscribe to a topic.
4.<span></span>The broker ensures messages are delivered from producers to consumers in the right order.
5.<span></span>Topic Management
6.<span></span>Support creation of multiple topics.
7.<span></span>Each topic has its own internal message queue.
8.<span></span>Each consumer subscribes to one or more topics.
9.<span></span>Message Handling
10.<span></span>Producers can publish(topic, message).
11.<span></span>Consumers can subscribe(topic) and poll() messages.
12.<span></span>Messages should be delivered at least once (no loss).
13.<span></span>Concurrency & Threading
14.<span></span>Multiple producers and consumers can run concurrently.
15.<span></span>Use threads to simulate message production and consumption.
16.<span></span>Synchronization is required to avoid race conditions.
17.<span></span>Queue Management
18.<span></span>Each topic maintains a queue internally (FIFO).
19.<span></span>Support backpressure: if consumer is slow, producer should not block indefinitely.
20.<span></span>Handle scenarios where queues grow large (bounded/unbounded).
21.<span></span>Offset Management
22.<span></span>Each consumer tracks its offset per topic (how many messages it has consumed).
23.<span></span>Consumers should be able to resume consumption from the last read offset.


Non-Functional Requirements


1.<span></span>Thread Safety → Safe concurrent reads/writes.
2.<span></span>Scalability → Support multiple topics and consumers.
3.<span></span>Fairness → Messages should be distributed fairly across consumers of a topic.
4.<span></span>Extensibility → Easy to add persistence or partitioning later.


Deliverables for the Machine Coding Round


1.<span></span>Core Classes / Interfaces
2.<span></span>Broker → Manages topics, subscriptions, and message routing.
3.<span></span>Topic → Holds queue of messages.
4.<span></span>Producer → Publishes messages to topics.
5.<span></span>Consumer → Subscribes and polls messages.
6.<span></span>Message → Payload structure (id, data, timestamp).
7.<span></span>Concurrency Model
8.<span></span>Internal queues implemented with thread-safe data structures.
9.<span></span>Worker threads to handle delivery of messages from broker to consumers.
10.<span></span>Synchronization to manage producer/consumer concurrency.
11.<span></span>Sample Workflow
12.<span></span>Producer publishes → Broker stores in queue → Consumer thread polls → Broker updates consumer offset.


Example Extensions Interviewer May Ask


1.<span></span>Add acknowledgment mechanism to guarantee delivery.
2.<span></span>Add retry logic if consumer fails.
3.<span></span>Add partitioning and consumer groups (like Kafka).
4.<span></span>Add persistence so broker can restart without data loss


Solution

Comments