liveBook · Manning

Consumer group rebalancing

As explained in Kafka Streams in Action, Second Edition

A consumer group rebalance is the redistribution of Kafka topic partitions among consumers in a consumer group.

Consumer group rebalancing redistributes topic-partition assignments when group membership changes. Consumers sharing a group ID act as one logical consumer: each partition is assigned to only one group member, while separate groups consume independently. The partition count caps active consumers; members beyond that count are idle. section 4.3.2 section 4.7

A rebalance commonly follows a consumer failure, a missed poll interval, or a new or returning member. If one of three consumers owning two partitions fails in a six-partition topic, its two partitions move to the active consumers. Kafka also detects inactive members through session timeouts and heartbeats, rather than always waiting for max.poll.interval.ms. section 4.3.2 section 4.3

Keep these operational distinctions in view:

  • Set group.id to place consumers in the same logical group; assignment and rebalancing occur within that group. section 4.3
  • Call KafkaConsumer.poll() within max.poll.interval.ms; exceeding it marks the consumer nonactive and triggers a rebalance. section 4.3
  • Prefer the cooperative rebalance protocol in most cases because it stops only partitions being reassigned, reducing downtime compared with eager stop-the-world rebalancing. section 4.3.2 section 4.7
  • Consider static membership where consumers often disconnect and return within a reasonable time, to provide greater group stability. section 4.7

A polling consumer and membership loss

A Java consumer subscribes to topic names and repeatedly calls poll(Duration.ofSeconds(5)) to process ProductTransaction records. Its group membership depends on continuing to poll within the configured maximum interval. If it stops polling long enough to become nonactive, Kafka redistributes its assigned partitions to other active consumers; after it returns, another rebalance can restore a balanced assignment. section 4.3 section 4.3.2

Rebalancing also carries application metadata in Kafka Streams. Instances with the same application ID can share their partition ownership and application.server information during rebalance. Interactive Queries uses that metadata to identify the instance responsible for a key’s partition and forward a query when the local instance does not own it; the query-serving and inter-instance communication layers still require implementation. section 13.2

Figure 4.14 Rebalancing with cooperative approach processing continues and only stops for partitions marked for reassignment.
figure

Compare cooperative reassignment with eager rebalancing: unaffected partitions continue processing while only partitions marked for reassignment stop. section 4.3.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains failure and timeout triggers, partition reassignment, eager and cooperative protocols, and Kafka Streams metadata sharing.
Examples
Uses six topic partitions shared by three consumers and a Java sales consumer polling ProductTransaction records.
What it assumes
Builds from KafkaConsumer configuration, consumer groups, topic partitions, and the poll() processing loop.
What you can do after
Operate/configure it by setting group IDs and poll timing, then selecting cooperative rebalancing to reduce reassignment downtime.
How it connects
Connects consumer groups with KafkaConsumer.poll(), session timeouts, static membership, CooperativeStickyAssignor, and Interactive Queries.

Also explained in

Compare how 3 books explain consumer group rebalancing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.