KatInterview

Partition Assignment and Rebalancing in Distributed Consumers

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsmessagingscalability

Last updated

Explain how a distributed message queue assigns partitions to consumers, handles ordering, and recovers from failures through rebalancing.

Distributed Consumer Architecture

You are building a real-time event processing pipeline using a distributed message queue. Your topic has 10 partitions and a consumer group with 4 active consumer instances reading from it.

Questions

1. Partition-to-Consumer Assignment

Explain how the system determines which consumer reads from which partition. What are the implications when the partition count (10) doesn't divide evenly by the consumer count (4)? How does this affect throughput and scaling?

2. Message Ordering

What ordering guarantees does the system provide? Describe scenarios where message order can be disrupted at the consumer level. If your application requires strict global ordering across all events, what approaches would you take?

3. Failure Recovery and Rebalancing

When a consumer instance fails unexpectedly, explain the complete lifecycle of recovering from the failure:

  • How does the system detect the failure?
  • What happens to progress tracking (offsets)?
  • How are orphaned partitions reassigned?
  • What ensures the failed consumer cannot interfere after recovery?

Concrete Example

Given partitions P0 through P9 and consumers C1 through C4, describe a reasonable assignment. Then walk through what happens when C4 crashes: detection, reassignment, and stabilization.

Hints

Hint 1

Each partition is assigned to exactly one consumer within a group, but a consumer can handle multiple partitions—think about the math when numbers don't divide evenly

Hint 2

Ordering is only guaranteed within a single partition; events in different partitions can be processed in any relative order

Hint 3

The system must track which consumer owns which partition using generation IDs or epochs to prevent stale consumers from corrupting state after a rebalance