Kafka Exception OutOfOrderSequenceException
This exception indicates that the broker received an unexpected sequence number from the producer, which means that data may have been lost. If the producer is configured for idempotence only (i.e. if enable.idempotence is set and no transactional.id is configured), it is possible to continue sending with the same producer instance, but doing so risks reordering of sent records. For transactional producers, this is a fatal error and you should close the producer.
Common Causes
- Producer sent batches in a non-sequential order — can happen if in-flight batches are reordered by the network or application logic outside the client
- max.in.flight.requests.per.connection > 1 with retries enabled and a batch was reordered after a retry (pre-Kafka 2.1 issue with idempotent producers)
- Producer instance was restarted mid-session without the broker expiring the old producer ID, causing epoch/sequence desync
Solutions
- Set max.in.flight.requests.per.connection=1 if using older brokers (<2.1); on Kafka 2.1+ with idempotent producers, 5 in-flight requests are safe
- Enable idempotent producer (enable.idempotence=true) which manages sequence numbers correctly and prevents reordering issues
- On producer restart after this error, create a new KafkaProducer instance to get a fresh producer ID rather than reusing the old one
Example Stack Trace
org.apache.kafka.common.errors.OutOfOrderSequenceException: The broker received an out of order sequence number.Diagnostic Commands
# Look for sequence number errors in logs
grep 'OUT_OF_ORDER_SEQUENCE\|OutOfOrderSequence\|sequence number' /var/log/kafka/server.log | tail -20
# Check for under-replicated partitions
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic <topic> 2>&1 | grep -E 'Isr|UnderReplicated'Related
Protocol error: OUT_OF_ORDER_SEQUENCE_NUMBER (code 45)
Related Producer exceptions: BufferExhaustedException · InvalidProducerEpochException · RecordBatchTooLargeException · RecordTooLargeException · TransactionAbortedException · UnknownProducerIdException · UnsupportedForMessageFormatException
OutOfOrderSequenceException in production? Conduktor Console gives you real-time visibility into clients, consumer groups, and broker health. Browse every Kafka exception or protocol error code.