# org.apache.kafka.common.errors.TransactionalCoordinatorFencedException: The transactional coordinator with epoch 5 has been fenced by a newer epoch 6

- **ID:** `kafka/transactional-coordinator-fenced`
- **Domain:** kafka
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 82%

## Root Cause

A transactional coordinator was replaced by a new coordinator with a higher epoch (e.g., after a leader change), causing the old coordinator's requests to be rejected as fenced.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Kafka 2.8.0 | active | — | — |
| Kafka 3.0.0 | active | — | — |
| Kafka 3.4.0 | active | — | — |
| Kafka 3.6.0 | active | — | — |

## Workarounds

1. **Handle the exception in the producer by retrying the transaction with a new transactional ID or by resetting the producer:

producer.initTransactions();
try {
    producer.beginTransaction();
    // Send messages
    producer.commitTransaction();
} catch (TransactionalCoordinatorFencedException e) {
    // Close and recreate the producer to get a fresh coordinator
    producer.close();
    producer = createNewProducer();
    producer.initTransactions();
    // Retry the transaction
}** (85% success)
   ```
   Handle the exception in the producer by retrying the transaction with a new transactional ID or by resetting the producer:

producer.initTransactions();
try {
    producer.beginTransaction();
    // Send messages
    producer.commitTransaction();
} catch (TransactionalCoordinatorFencedException e) {
    // Close and recreate the producer to get a fresh coordinator
    producer.close();
    producer = createNewProducer();
    producer.initTransactions();
    // Retry the transaction
}
   ```
2. **Ensure the transactional.id is unique per producer instance and that the broker's transaction.state.log.replication.factor is sufficient to avoid coordinator failures:

transaction.state.log.replication.factor=3

Also monitor the broker logs for coordinator changes and consider increasing the number of transaction coordinator threads.** (80% success)
   ```
   Ensure the transactional.id is unique per producer instance and that the broker's transaction.state.log.replication.factor is sufficient to avoid coordinator failures:

transaction.state.log.replication.factor=3

Also monitor the broker logs for coordinator changes and consider increasing the number of transaction coordinator threads.
   ```

## Dead Ends

- **Increase the transaction.timeout.ms to a very high value** — Transaction timeout controls how long a transaction can remain open, not coordinator fencing; it does not prevent epoch conflicts. (85% fail)
- **Disable idempotent producer and transactions** — This avoids the error but disables exactly-once semantics, which may be required for the application; it is a workaround that changes behavior. (70% fail)
