advanced

Kafka ordering guarantees

Rely on order only within a partition and design keys, retries, concurrency, and reprocessing around that constraint.

Order is guaranteed only within a single partition. Cross-partition order is undefined. Producers must use the same key for related events; consumers must not parallelize processing of the same key across threads if order matters.

Trade-off: global ordering requires one partition — limiting throughput; per-key ordering scales with key distribution.

On interviews: guarantee order of login then logout for one user; why retries can reorder without idempotent sequence handling.

Common pitfalls: multiple keys for same entity; consumer thread pool breaking partition order; reprocessing from old offset interleaving with new events.

Checklist:

  • Key all causally related events consistently.
  • One in-flight handler per partition or per key.
  • Design for out-of-order across services anyway.
  • Test reorder scenarios under retries.