Kafka
Consumer Group
Data Streaming
Distributed Systems
Fault Tolerance

What happens to a Kafka consumer group when all consumers are removed

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In Apache Kafka, a consumer group is a critical component that allows multiple consumers to jointly process a topic, dividing the work among themselves. When discussing what happens when all consumers in a group are removed, several aspects need to be considered, including the rebalancing process, offset management, and the handling of messages.

Understanding Kafka Consumer Groups

A Kafka consumer group consists of several consumer instances for distributing message processing. Each consumer in the group reads from exclusive partitions of the topic, ensuring that the message processing is load balanced and each message is processed only once by the group.

Rebalancing of Consumer Groups

When all consumers in a group are removed or lost (this might occur due to failures, shutdowns or network issues), Kafka triggers a group rebalance. The purpose of a rebalance is to redistribute the partition ownership amongst the available consumers in the group, or in this specific case, to acknowledge that there are no current consumers.

During a rebalance, Kafka follows these steps:

  1. Stop Message Delivery: Kafka stops delivering messages to the consumers of this group.
  2. Partition Re-Assignment: Since there are no consumers left in the consumer group, no partitions are assigned.
  3. Consumer Offset Commit: If enable.auto.commit is true, consumers commit their offset before leaving. For already removed consumers, there would be no new commits.

If new consumers join the group after all previous consumers have been removed, a new rebalance will occur, and partitions will be assigned to the new members of the group.

Management of Offsets

Kafka stores the offsets for each consumer group, which indicates the next message to be read by each consumer in the group. When all consumers are removed:

  • Offset Retention: The committed offsets are stored by Kafka for a configurable period (default is 24 hours, configured by offsets.retention.minutes). If no new consumer joins the group within this retention period and commits offsets, these offsets will be discarded.
  • Message Delivery Post-Consumer Re-Join: If new consumers join the group after the offsets are discarded, they start consuming messages based on the auto.offset.reset policy, which might be from the earliest message or the latest, as configured.

Durability and High Availability

Removing all consumers from a consumer group does not affect the persistence and availability of the messages in the partition. Kafka maintains replicas of each partition on different brokers. Thus, even if consumer groups are inactive or deleted, the actual messages are safe and can be consumed later when new consumers are available.

What Happens to the Group Itself

When there are no active consumers left in the group and offsets are expired, the consumer group does not necessarily "disappear" from Kafka. It remains identifiable in Kafka but will remain inactive until new consumers join and activate it, assuming offset configurations and retention policies re-establish their activity.

Table of Key Points

Here's a summarized table about what happens when all consumers from a Kafka Consumer Group are removed:

EventDescription
RebalancingTriggered immediately but leads to no partition assignment.
Offset CommitCommitted if enable.auto.commit is true; consumers commit before leaving.
Offset RetentionStored offsets are kept based on retention configuration.
Message Delivery Upon Re-JoinBased on auto.offset.reset if offsets are expired or deleted.
Group StatusRemains identifiable but inactive in Kafka without consumers.

Conclusion

In essence, removing all consumers from a Kafka consumer group triggers rebalance processes with no consumers available to claim partitions. Although the consumer group exists without active members, it maintains its configuration until the offsets expire or until new consumers join. This model enables Kafka to manage consumer groups dynamically, adjusting to changes in consumer availability without manual intervention.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.