Handling Rebalances

This lesson discusses the hooks available to Kafka consumers to invoke before partition ownership is revoked or granted in a rebalance.

A consumer may want to do cleanup work before a partition it owns is reassigned during a rebalance. They may also want to prepare before being assigned a new partition. Both use cases are addressed by the interface ConsumerRebalanceListener. The interface offers two methods that can be implemented by classes to insert custom logic just before a partition is revoked or assigned.

  • onPartitionsRevoked: This method is invoked after a consumer stops consuming events but before a rebalancing takes effect.

  • onPartitionsAssigned: This method is invoked after partitions have been assigned to a consumer but before it starts consuming messages.

Example use of onPartitionsRevoked

We can implement the interface and pass an instance of the implementing class at the time of invoking the subscribe() call of the consumer object. The code widget below demonstrates an example of the onPartitionsRevoked method in use to commit offsets just before a consumer loses ownership of a partition.

Get hands-on with 1200+ tech skills courses.