Under Kafka's KIP-848 consumer group protocol, how do the group, target-assignment and member epochs drive reconciliation, and when is a member fenced?
answer
- three counters, one per layer
- bump, compute, then converge
- revoke before you receive
- wait for the previous owner
- stale epoch means start over
basics
~20 sThe coordinator bumps the group epoch on membership, subscription or metadata changes and computes a target at that epoch; each member revokes partitions moving away, then advances its member epoch and takes freed ones. Stale epochs or missed deadlines fence it.
solid answer
~50 sUnder KIP-848 the **group epoch** counts changes the coordinator has seen: a member joining or leaving, a subscription change, a change in partition metadata. The **target-assignment epoch** is the group epoch for which the server-side assignor last computed the desired assignment. Each member's **member epoch** says which target it has reconciled to. When a member is behind, the coordinator first asks it to revoke partitions that move away; it stays at its old epoch, state `UNREVOKED_PARTITIONS`, until its heartbeat stops reporting them. Then it moves to the target epoch and gets new partitions, but only those whose previous owner has released them; it waits in `UNRELEASED_PARTITIONS` for the rest. A heartbeat with an unexpected epoch gets `FENCED_MEMBER_EPOCH`, and a member that does not revoke within its rebalance timeout (`max.poll.interval.ms`) is fenced; it then calls `onPartitionsLost` and rejoins with epoch 0.
go deeper
Recall that there are three epochs: the group's, the target assignment's and each member's, and that members catch up to the target one at a time.
Walk through the states STABLE, UNREVOKED_PARTITIONS and UNRELEASED_PARTITIONS, and explain why revocation must finish before a partition is reassigned.
Diagnose from symptoms: a member stuck revoking because of slow batches, fencing after max.poll.interval.ms, the lost-response tolerance, and epoch-validated offset commits.
Connect the epochs to operational limits: assignment batching since 4.3, coordinator load from metadata churn, and why rebalance timeouts now bound how long one slow member can hold others waiting.
## Three epochs, three layers Kafka 4.0's consumer group protocol (KIP-848, `group.protocol=consumer`) replaces the classic protocol's single **generation id** (one number the whole group moves through together) with three counters held by the broker's **group coordinator**. Each describes a different layer: | Epoch | Owned by | Moves when | |---|---|---| | **Group epoch** | the group | the coordinator records a change: a member joins or leaves, a subscription changes, a resolved regex changes, or partition metadata of a subscribed topic changes | | **Target-assignment epoch** | the target assignment | the server-side assignor computes a new desired assignment for the current group epoch | | **Member epoch** | each member | that member has converged on the target assignment of a given epoch | The design idea is that the group can move ahead without anyone stopping. The target assignment changes as soon as the coordinator recomputes it, and each member catches up on its own time through its heartbeats. Since Kafka 4.3, recomputations are batched: `group.consumer.assignment.interval.ms` (default 1000 ms) sets the interval between assignment updates, where earlier releases effectively used 0. ## The reconciliation state machine Every `ConsumerGroupHeartbeat` request carries the member's epoch and the partitions it currently owns. The coordinator's reconciliation engine compares them with the target and puts the member in one of three states: 1. **`STABLE`**: the member's epoch equals the target-assignment epoch and it owns exactly its target. Nothing to do. 2. **`UNREVOKED_PARTITIONS`**: some owned partitions moved to someone else. The member **stays at its old epoch**, and its heartbeat response shrinks its assignment. The client runs `onPartitionsRevoked`, stops fetching those partitions, and the next heartbeat no longer lists them. Only then can it progress. 3. **`UNRELEASED_PARTITIONS`**: the member has moved to the new epoch and received every partition that is already free, but some of its target partitions are still owned by their previous member. It keeps working on what it has and receives the rest as soon as the other member releases them. The rule that makes this safe is **revoke before reassign**: a partition is never handed to its new owner while another member still holds it. That gives single ownership without a group-wide barrier, which is what the classic protocol used the barrier for. ## How fencing works The coordinator **fences** a member when it can no longer trust the member's view. A fenced member gets the `FENCED_MEMBER_EPOCH` error. Fencing happens when: - the heartbeat's member epoch is **greater** than the one the coordinator knows; - the heartbeat's epoch is **smaller**, except for one tolerated case: it equals the member's previous epoch and the owned partitions are a subset of the current assignment, because the response that carried the bumped epoch may have been lost; - the member enters `UNREVOKED_PARTITIONS` and has not finished revoking when its **rebalance timeout** expires; that is the `RebalanceTimeoutMs` the client derives from `max.poll.interval.ms`. The coordinator logs that the member failed to transition from its epoch in time and removes it. Session expiry is separate: a member that sends no heartbeat within `group.consumer.session.timeout.ms` (default 45000 ms) is removed too. On the client, a fenced `KafkaConsumer` invokes `ConsumerRebalanceListener.onPartitionsLost` for everything it held, clears its assignment and rejoins with **member epoch 0**, which is the value that means "join". Epoch -1 in a heartbeat means "leave". Offset commits are also fenced. A consumer-protocol member commits with its member epoch; a commit for a partition that is neither assigned to it nor pending revocation is rejected with `StaleMemberEpochException`, and so is a commit carrying an epoch the coordinator has not issued yet. ## Diagnosing a stuck or flapping member - **A member stuck in `UNREVOKED_PARTITIONS`** is usually slow to process its current batch or blocking in `onPartitionsRevoked`. Every member waiting for those partitions sits in `UNRELEASED_PARTITIONS`. Unless the member revokes, it is fenced when its rebalance timeout expires. - **Repeated `FENCED_MEMBER_EPOCH` in client logs** means the member keeps losing the coordinator's trust: lost responses, long GC pauses past the session timeout, or a process that keeps being restarted. - **Group epoch climbing quickly** with no deploys going on points at a member that keeps joining and leaving, or at frequent subscription or metadata churn, such as a regex matching topics that are often created. ## Contrast with the classic generation In the classic protocol a new generation starts only when everyone has rejoined, and a member with an old generation id is rejected wholesale. Here, members can sit at different epochs at the same moment, and that is normal: it is how the protocol rebalances one member at a time.
- Why does a member in UNREVOKED_PARTITIONS stay at its old member epoch instead of moving straight to the target epoch?Because moving to the new epoch is what entitles it to new partitions. If it advanced first, the coordinator could hand it a partition in the same response that still asks someone else to give one up, and the protocol could no longer prove single ownership. Keeping it at the old epoch until it stops reporting the revoked partitions enforces revoke before reassign.
- A consumer's heartbeat response was lost after the coordinator bumped its member epoch. Is it fenced on the next heartbeat?Not necessarily. The coordinator accepts a heartbeat carrying the member's previous epoch when the partitions it reports are a subset of its current assignment, precisely to cover a lost response. It then replies with the current epoch. Any other older or newer epoch is fenced with `FENCED_MEMBER_EPOCH`.
- What should a ConsumerRebalanceListener do differently in onPartitionsLost compared with onPartitionsRevoked under the consumer protocol?`onPartitionsRevoked` runs while the member still owns the partitions, so it can commit offsets for them. `onPartitionsLost` runs after fencing, when the partitions may already belong to someone else; a commit would be rejected as stale, so it should only clean up local state and not try to commit.
- All members of a KIP-848 group always share the same member epoch
- A member receives its new partitions before revoking the ones moving away
- Any heartbeat with an older member epoch is fenced, without exception
- The group epoch only changes when a member joins or leaves
- A fenced member keeps its partitions and simply retries the heartbeat
- Slow processing cannot get a consumer-protocol member fenced, only missed heartbeats
Assumes Kafka 4.3: the reconciliation model is GA since 4.0, and group.consumer.assignment.interval.ms (1 s batching) was added in 4.3.