Video in progress

Strong Middle 📨 Kafka and messaging

What is a "zombie" instance in a transactional producer, how do `transactional.id` and the epoch stop it, and how do you choose `transaction-id-prefix` in Spring?

A zombie is an old producer that hung, got replaced by a new one, and then "woke up" and kept writing. When a new producer with the same transactional.id calls initTransactions(), the coordinator bumps the epoch, and the old one gets a ProducerFencedException. In Spring the prefix must be unique per instance.

📖 Answer

Scenario: a pod hits a long GC pause, Kubernetes decides it's dead and starts a new one. The old pod "wakes up" 40 seconds later and tries to commit its open transaction. If nobody stops it, you get duplicate results.

The mechanism:

  • transactional.id is the producer's stable name; the broker attaches a PID and an epoch to it;
  • when a new producer calls initTransactions() with that ID, the coordinator bumps the epoch by one and aborts the open transaction of the old epoch;
  • every request with the old epoch is rejected, the producer gets a ProducerFencedException and can now only be closed.

In the consume-transform-produce chain there's one more protection (KIP-447, Kafka 2.5+): sendOffsetsToTransaction is sent with the consumer group metadata, and the broker rejects the offset commit of a member whose group generation is stale. So you no longer have to tie transactional.id to a partition. Spring Kafka 3.x supports only this mode (EOSMode.V2).

The practical rule in Spring is that the prefix should be different on every instance:

spring:
  kafka:
    producer:
      transaction-id-prefix: billing-tx-${HOSTNAME}-

If two pods run with the same prefix, they "fence" each other in turn and ProducerFencedException shows up in the log nonstop. If the pod name is random (a Deployment), a new ID appears after each restart. That works; it's just that the open transactions of the old IDs get aborted after transaction.timeout.ms, and read_committed consumers may have to wait that long. A stable name in a StatefulSet handles this better.