3. Kafka Producers: Writing Messages to Kafka
3.6 ⚠️ ORDERING GUARANTEES — the reordering bug
"Apache Kafka preserves the order of messages within a partition. If messages are sent from the producer in a specific order, the broker will write them to a partition in that order and all consumers will read them in that order. For some use cases, order is very important. There is a big difference between depositing $100 in an account and later withdrawing it, and the other way around!"
How retries silently reorder your data
retries > 0 AND max.in.flight.requests.per.connection > 1
LOG ORDER: [ … B, A … ] — ORDER REVERSED. Silently.
The bind, and the resolution
- · guarantees message ordering with up to FIVE in-flight requests
- · AND guarantees that retries will not introduce duplicates
(Ch. 8 covers the idempotent producer in depth.)
Do not use the old folklore fix (max.in.flight.requests.per.connection=1) — it costs you throughput and is strictly worse than idempotence.