Multithreaded Kafka-like Messaging System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-05-04

Problem Statement

Design and implement a simplified Kafka-style distributed log-based messaging system that supports concurrent producers and consumers. The system should guarantee ordering within partitions, durability of messages, and efficient multi-threaded processing.


Functional Requirements


1.<span></span>Core APIs
2.<span></span>publish(topic, message) → Append a message to a topic.
3.<span></span>subscribe(topic, consumer_group) → Register a consumer to a topic.
4.<span></span>poll(consumer_group, timeout) → Fetch messages assigned to a consumer.
5.<span></span>commit_offset(consumer_group, partition, offset) → Mark messages as processed.
6.<span></span>Topic & Partitioning
7.<span></span>A topic consists of multiple partitions (append-only logs).
8.<span></span>Messages within a partition are strictly ordered.
9.<span></span>Producers can publish to a specific partition or use a hash-based partitioner.
10.<span></span>Concurrency Requirements
11.<span></span>Multiple producer threads should be able to publish messages concurrently.
12.<span></span>Multiple consumer threads in the same consumer group should be able to consume messages in parallel (partition assignment ensures no two consumers in a group read the same partition).
13.<span></span>Thread-safe management of:
14.<span></span>Partition logs (append + read)
15.<span></span>Offsets per consumer group
16.<span></span>Consumer group membership
17.<span></span>Durability & Buffering
18.<span></span>Messages are appended to in-memory log segments and periodically flushed to disk (batch write).
19.<span></span>Consumers must be able to read even if producers are still writing.
20.<span></span>Scalability Constraints
21.<span></span>Handle up to 1000 concurrent producers and 1000 concurrent consumers.
22.<span></span>Topics can have up to 100 partitions.
23.<span></span>System should support throughput of millions of messages/sec (simulated).


Example Workflow


1.<span></span>Thread-1 (Producer) publishes to topic=orders, partition=2 →

<div><div>lua</div><div>CopyEdit</div><div>{order_id=123, status="PLACED"}</div></div>

2. <span></span>Thread-2 (Producer) publishes to same topic, partition=2 →

<div><div>lua</div><div>CopyEdit</div><div>{order_id=124, status="PLACED"}</div></div>

3. <span></span>Thread-3 (Consumer-Group-A) assigned partition=2 polls → receives messages in correct order:

<div><div>ruby</div><div>CopyEdit</div><div>[123:PLACED, 124:PLACED]</div></div>

4. <span></span>Thread-4 (Consumer-Group-B) (independent group) also polls → receives same messages independently.


Deliverables


1.<span></span>Class Design (e.g., Broker, Topic, Partition, Producer, Consumer, ConsumerGroup).
2.<span></span>Thread-safe implementation of:
3.<span></span>Log appends (producers)
4.<span></span>Log reads (consumers)
5.<span></span>Offset tracking per consumer group
6.<span></span>Support for partition assignment across consumer threads.
7.<span></span>Unit tests verifying:
8.<span></span>Ordering guarantees within partitions.
9.<span></span>No duplicate consumption inside a consumer group.
10.<span></span>Multiple consumer groups receive independent copies.
11.<span></span>Benchmark showing scalability with multiple producer/consumer threads.


Solution

Comments