Kafka Exception InvalidProducerEpochException
org.apache.kafka.common.errors.InvalidProducerEpochException
Non-retriable
Producer
This exception indicates that the produce request sent to the partition leader contains a non-matching producer epoch. When encountering this exception, user should abort the ongoing transaction by calling KafkaProducer#abortTransaction which would try to send initPidRequest and reinitialize the producer under the hood.
Common Causes
- Two producer instances sharing the same transactional.id are running concurrently — the newer one fenced the older one by bumping the epoch
- Producer was considered dead by the broker (transaction.timeout.ms exceeded) and a new epoch was assigned; the old instance then tried to produce
- Rolling restart or failover created a brief period where two instances of the same transactional producer were active simultaneously
Solutions
- Ensure only one producer instance per transactional.id is active at any time; use leader election or distributed locks to enforce singleton producers
- Increase transaction.timeout.ms to give producers more time before being fenced, but keep it below transaction.max.timeout.ms on the broker
- After fencing, the old producer must be closed and a new instance created — the fenced producer cannot recover and must not be retried
Example Stack Trace
org.apache.kafka.common.errors.InvalidProducerEpochException: Producer attempted to produce with an old epoch.Diagnostic Commands
# Look for producer epoch events in logs
grep 'INVALID_PRODUCER_EPOCH\|InvalidProducerEpoch\|producer epoch\|fenced' /var/log/kafka/server.log | tail -30
# Inspect transaction-state topic health
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic __transaction_state | grep -E 'Leader|Isr|Replicas'Related
Protocol error: INVALID_PRODUCER_EPOCH (code 47)
Related Producer exceptions: BufferExhaustedException · OutOfOrderSequenceException · RecordBatchTooLargeException · RecordTooLargeException · TransactionAbortedException · UnknownProducerIdException · UnsupportedForMessageFormatException
Hitting
InvalidProducerEpochException in production? Conduktor Console gives you real-time visibility into clients, consumer groups, and broker health. Browse every Kafka exception or protocol error code.