Video in progress

Strong Middle 📨 Kafka and messaging

How can message order break when the producer retries, and how does max.in.flight.requests.per.connection relate to it?

If several requests are in flight on one connection without a response, and the first fails and is resent, the second gets written before it. Without idempotence you had to set in-flight to 1 to keep order; with idempotence enabled, up to 5 is safe.

📖 Answer

The scenario, without idempotence, retries > 0, max.in.flight=5:

batch A (should be offset 100) -> NOT_LEADER error
batch B -> written successfully (offset 100)
batch A retry -> written (offset 101)

In the log B sits before A. The "debit" and "credit" events for one account can end up processed in reverse order.

The old fix is max.in.flight.requests.per.connection=1: the next batch doesn't go out until the previous one is acknowledged. Order is preserved, but on a high-latency network throughput drops sharply.

The modern fix is the idempotent producer. The broker checks the sequence numbers: it won't accept B until A arrives (OutOfOrderSequenceException, and the producer resends). That's why order is guaranteed with in-flight up to 5.

enable.idempotence=true          # the default since Kafka 3.0
acks=all
max.in.flight.requests.per.connection=5
retries=2147483647
delivery.timeout.ms=120000

Be careful: if you explicitly set acks=1 or in-flight > 5, the Kafka 3.x client may silently turn idempotence off or throw a config error (depends on the version). When the producer starts, check the real values in the ProducerConfig values block of the log.

delivery.timeout.ms matters more than retries: it's the overall time limit for a single send().