Kafka: Ordering is guaranteed per partition, never per topic

Two events land in order only if they share a partition - which means only if they share a key.

Code
// Same key -> same partition -> ordered
producer.send(new ProducerRecord<>("orders", "order-42", "CREATED"));
producer.send(new ProducerRecord<>("orders", "order-42", "PAID"));

// No key -> any partition -> no ordering guarantee at all
producer.send(new ProducerRecord<>("orders", null, "CREATED"));
producer.send(new ProducerRecord<>("orders", null, "PAID"));
Output
Keyed:   partition 3, offset 118
         partition 3, offset 119   <- ordered
Unkeyed: partition 0, offset 55
         partition 5, offset 91    <- consumed in any order
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11