Design a Kafka-Like Distributed Queue
Model partitioned topics, consumer groups, offset commits, rebalancing, and at-least-once delivery in a thread-safe in-memory broker.
Design the low-level object model and interfaces for a Kafka-like distributed queue.
For this exercise, assume the core is one thread-safe in-memory broker process. A topic is created with a fixed positive partition count. Each partition stores immutable records in append order and assigns monotonically increasing offsets starting at zero.
A producer may provide a key; equal keys for one topic must select the same partition, while unkeyed records use a deterministic distribution policy. Consumers join a named consumer group and subscribe that group to one topic.
The broker assigns every topic partition to exactly one active member of the group whenever possible. Each member holds a renewable lease from an injected clock; an explicit leave or an expired lease removes the member. Membership changes create a new group generation and trigger a deterministic rebalance.
A member may poll only partitions it owns in its current generation. It explicitly commits the next offset to consume for an owned partition; commits may move forward but never backward. A crash is detected when its lease expires.
If a crash or rebalance occurs before commit, the unchanged committed offset permits redelivery, so the guarantee is at-least-once. Poll and commit requests carrying a stale generation or stale ownership must be rejected.
Networking, durable storage, replication, retention, partition-count changes, transactions, and exactly-once processing are outside the core scope. You may discuss them only as follow-ups.
Define observable errors and preserve the stated invariants under concurrent produce, poll, join, heartbeat, leave, lease-expiry, and commit calls.