Video in progress

Strong Middle 📨 Kafka and messaging

What is the `__consumer_offsets` topic, and how is the group coordinator chosen?

Committed offsets and group metadata are stored in the internal, compacted __consumer_offsets topic (50 partitions by default). The hash of group.id picks one of these partitions, and the leader broker of that partition becomes the group's coordinator.

📖 Answer

When a consumer calls commitSync(), the offset is written not to ZooKeeper (that was a very old era) but to a regular Kafka topic. The key is (group, topic, partition) and the value is the offset. The topic is compacted, so the latest value remains for each key.

Finding the coordinator:

partition = abs(hash(group.id)) % offsets.topic.num.partitions   // default 50
coordinator = the leader broker of that partition

The coordinator receives heartbeats from the group members, manages the rebalance and writes the commits. If the broker dies, the new leader of that partition becomes the coordinator and re-reads the state from the topic.

Things worth knowing on prod:

  • This topic's offsets.topic.replication.factor must be 3. A dev cluster with one broker gets 1, and I've seen that configuration migrate to prod.
  • Offsets don't live forever: once the group has been empty for longer than offsets.retention.minutes (7 days by default), they are deleted. When a service that was down for a week comes back, it starts according to auto.offset.reset, and the unexpected "re-reads everything" often comes from this.
  • Too many groups can land on one "hot" coordinator, but that's rarely a problem.