Design a Kafka-like message queue with concurrency guarantees

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-08

Problem Statement

Design and implement a distributed, concurrent message queue system that allows producers to publish messages to topics, consumers to subscribe, and guarantees message ordering and delivery. Focus on thread safety, concurrency, and extensibility.


Functional Requirements (FR)


Topics & Partitions


•Support multiple topics, each representing a stream of messages.
•Topics can have partitions for parallelism.
•Messages within a partition are ordered.


Producers


•Producers can publish messages to a topic.
•Support synchronous and asynchronous publishing.


Consumers


•Consumers can subscribe to topics.
•Support consumer groups for load-balanced consumption.
•Ensure each message is delivered to a consumer exactly once per group.


Message Guarantees


•Preserve message order within a partition.
•Ensure thread-safe operations for concurrent producers and consumers.
•Support message acknowledgment to track consumption.


Persistence & Replay (Optional)


•Persist messages to disk for durability.
•Support replaying messages from a specific offset.


Non-Functional Requirements (NFR)


•Concurrency → Multiple producers and consumers can operate concurrently.
•Low Latency → Publish and consume operations should be fast.
•Scalability → Support high throughput by partitioning topics.
•Reliability → Ensure no message loss and proper acknowledgment tracking.
•Extensibility → Support future features like replication, retention policies, and fault tolerance.


Core Entities & Attributes


Topic


•topicId, name, partitions[]


Partition


•partitionId, messages[] (FIFO queue), currentOffset


Message


•messageId, payload, timestamp, metadata


Producer


•producerId, publish(topic, message)


Consumer


•consumerId, subscribe(topic), consume()


ConsumerGroup


•groupId, members[], offsetMap (partition → last consumed offset)


Core Components


MessageBroker

•Central system to manage topics, partitions, and routing messages to consumers.

TopicManager

•Handles creation of topics and partitions.

PartitionQueue

•Stores messages in order and supports thread-safe enqueue/dequeue.

ConsumerManager

•Tracks consumer groups, offsets, and manages message delivery.

Persistence Layer (Optional)

•Stores messages for durability and replay.


APIs / Operations


Producer APIs


•publish(topicId, message) → Add message to topic partition.


Consumer APIs


•subscribe(topicId, consumerId) → Subscribe to a topic.
•consume(consumerId, topicId) → Fetch next available message(s).
•acknowledge(consumerId, topicId, offset) → Confirm message consumption.


Topic APIs


•createTopic(name, numPartitions)
•getTopicPartitions(topicId)


LLD Design Goals


•Class Diagram → MessageBroker, TopicManager, Topic, PartitionQueue, Message, Producer, Consumer, ConsumerGroup.


Design Patterns:


•Observer Pattern → Notify consumers when new messages arrive.
•Singleton → Single MessageBroker instance per cluster.
•Strategy/Factory → For different partition assignment strategies.
•Concurrency Handling → Thread-safe enqueue and dequeue for partitions. Locks or concurrent queues for message delivery.
•Trade-offs →

In-memory vs. persistent storage for messages.

Synchronous vs. asynchronous acknowledgment for throughput vs. reliability.


Solution

Comments