Kafka + Distributed Systems
Kafka Partitions and Ordering Explained
Kafka guarantees ordering within a partition, not across the whole topic. The key idea is choosing the right message key so related events go to the same partition.
The Short Answer
Kafka preserves ordering inside a single partition. It does not guarantee one global order across all partitions in a topic.
If events for the same entity must be processed in order, give those events the same message key. Kafka uses the key to consistently route related records to the same partition.
The Real Problem
Imagine you are processing order events for an e-commerce system:
OrderCreated
PaymentAuthorized
OrderShipped
OrderCancelledFor one order, the order matters a lot. You do not want to process OrderShipped before OrderCreated.
But Kafka also needs to scale. If everything went into one partition, ordering would be simple, but throughput would be limited. Partitions are how Kafka gets parallelism.
Mental Model: A Topic Is Split Into Ordered Logs
Topic: order-events
Partition 0
Partition 1
Partition 2
Each partition is ordered from left to right. But Kafka does not say that A2 happened before B2 or C2 across the whole topic.
The Key Insight: Order Related Events by Key
If all events for order-123 use the same key, they go to the same partition and keep their relative order.
Good: Same Key
key = order-123
Same key → same partition → order preserved.
Risky: No Useful Key
key = random / missing
Related events may land in different partitions, so global order is not guaranteed.
Concrete Example
Good producer design:
ProducerRecord<String, OrderEvent> record =
new ProducerRecord<>(
"order-events",
orderEvent.orderId(), // key
orderEvent // value
);
producer.send(record);Here, orderId is the key. That means all events for the same order go to the same partition.
Bad producer design:
ProducerRecord<String, OrderEvent> record =
new ProducerRecord<>(
"order-events",
UUID.randomUUID().toString(), // bad key for ordering
orderEvent
);A random key spreads events around, which may increase distribution but destroys per-order ordering.
Ordering vs Parallelism
One Partition
- Simple ordering
- Lower parallelism
- Can become a bottleneck
Many Partitions
- Higher throughput
- More consumers can work in parallel
- Ordering only within each partition
Consumer Groups: Why Partition Count Matters
In a consumer group, a partition is assigned to only one consumer at a time. That is how Kafka preserves order for that partition.
Consumer A
Partition 0
Consumer B
Partition 1
Consumer C
Partition 2
If you have more consumers than partitions in the same consumer group, the extra consumers sit idle. Kafka cannot split one partition across multiple consumers in the same group without breaking partition ordering.
Common Interview Trap
A weak answer is: “Kafka guarantees ordering.”
A strong answer is:
When This Matters in Real Systems
- Order lifecycle events
- Payment state changes
- Account balance updates
- User session events
- Inventory updates
- Ad auction or bidding events keyed by request/user/campaign
Common Interview Follow-Ups
Does Kafka guarantee ordering across a topic?
No. Ordering is guaranteed within a partition, not across all partitions in a topic.
How do you preserve order for one customer or order?
Use a stable message key, such as customerId or orderId, so related events go to the same partition.
What is the tradeoff of using one partition?
You get simpler ordering, but you reduce parallelism and throughput.
Can multiple consumers process the same partition in one consumer group?
No. In a consumer group, a partition is assigned to one consumer at a time. This protects ordering within that partition.