Video in progress

Strong Middle 📨 Kafka and messaging

What problem does static membership (group.instance.id) solve?

Normally when a consumer restarts, it leaves the group and comes back as a new member, which causes two rebalances. With group.instance.id, the broker recognizes it as the same member, and if it returns within session.timeout.ms, no rebalance happens at all.

📖 Answer

In a rolling deploy or a pod restart: a dynamic member sends LeaveGroup when it shuts down, causing a rebalance; when it comes back, JoinGroup causes another one. 10 pods means 20 rebalances, each lasting a few seconds.

With static membership each instance gets a stable ID. On shutdown it doesn't send LeaveGroup. The broker keeps its partitions "reserved" until session.timeout.ms runs out. If the pod returns within that time, it gets the same partitions back without a rebalance.

In Kubernetes it fits nicely with a StatefulSet, because the pod name is stable:

spring:
  kafka:
    consumer:
      properties:
        group.instance.id: ${HOSTNAME}
        session.timeout.ms: 60000

Trade-offs:

  • if a pod really dies, its partitions are read by nobody for the whole session.timeout.ms, so the lag grows for that long;
  • if two instances start with the same ID, one is kicked out with FencedInstanceIdException;
  • in Spring, if concurrency > 1, Spring makes the ID unique for each inner consumer by appending -0, -1.

It also works with the random pod names of a Deployment, but then the ID is different after a restart and you get no benefit.