KatInterview

Kafka Consumer Groups: Partition Ownership and Rebalancing

System DesignModel answer
MediumModel answerSystem Designkafkaconsumer-groupsrebalancing

Last updated

Explain how a Kafka consumer group splits partitions, handles offsets and crashes, scales with consumer count, and behaves during a rebalance.

Context

A payments platform publishes financial events to a Kafka topic that has 3 partitions. A single consumer group reads from this topic and processes each event. You need to explain how the group behaves and how to make it scale and stay correct.

Questions

  1. Assignment. The group has 5 active consumers subscribed to the 3-partition topic. How are partitions distributed among them, and what state are the two consumers without a partition in?
  2. Offsets and crashes. Explain what a consumer offset is. If a consumer crashes after it has processed a message but before it commits the offset, what happens to that message once the group rebalances? What configuration and coding practices give you at-least-once processing?
  3. Scaling. To raise throughput, the team proposes adding more consumers than there are partitions. Would this help? Justify your answer using Kafka's partition ownership model, and mention what you would do instead.
  4. Rebalancing. Walk through the sequence of events in a partition rebalance. What happens to latency and availability while it runs, and how can the impact be reduced?

Answer in a structured way, with concrete settings and trade-offs. Short code is welcome for the commit logic in question 2.

Hints

Hint 1

Start from one rule: within a group, each partition is read by exactly one consumer at a time, while one consumer may hold several partitions.

Hint 2

An offset marks the next record to read. Ask yourself in which order 'do the work' and 'commit the offset' must happen so that a crash can only cause a repeat, never a loss, and how a handler can tolerate repeats.

Hint 3

For the rebalance, list the triggers first (join, leave, missed heartbeat, poll timeout, metadata change), then the steps (join, leader assigns, sync). Then contrast eager with cooperative and static membership.