Partition Keys & Ordering Guarantees
"In Order" Is Smaller Than You Think
System designsystem design / messaging & streaming
The mental model
A Kafka topic (or Kinesis stream, SQS FIFO queue, Pub/Sub topic with ordering keys) is not one queue. It's a post office with N conveyor belts. A message arrives, a clerk hashes its key, and drops it on belt hash(key) % N. Each belt is strictly FIFO. Nothing — nothing — orders belts against each other.
So ordering is a property of the KEY, never of the topic.
- key = order_id → every event for order 42 lands on one belt, in order ✅
- key = null → round-robin across belts; "cancelled" can be consumed before "created" ❌
Concrete
Topic orders, 12 partitions, 4 consumers → ~3 partitions each. Per-order_id order is exact; different orders process in parallel. That's the whole trade: you buy parallelism by giving up global order, then buy back exactly the ordering you need by choosing the key.
Where the analogy breaks (the intermediate part)
1️⃣ Repartitioning moves belts.
Go from 12 → 24 partitions and key "42" now hashes elsewhere. Kafka does NOT rehash existing data, so old-belt messages can be consumed AFTER new-belt ones for the same key. Adding partitions is a one-way ordering break. Over-provision up front (12–24 for a topic you think needs 4).
2️⃣ Producer retries reorder inside one belt.
max.in.flight.requests.per.connection=5 with retries>0: batch 1 fails, batch 2 lands, batch 1 lands on retry — out of order, same partition. Fix:
enable.idempotence=true
acks=all
Idempotence attaches sequence numbers so the broker rejects out-of-order batches.
3️⃣ Hot keys serialize you.
key = tenant_id where one tenant is 70% of traffic → one partition pinned at 100%, eleven idle. Adding consumers changes nothing; a partition has at most one consumer in a group. Pick the FINEST key that still preserves the order you actually need: user_id, not tenant_id.
4️⃣ Delivered in order ≠ processed in order.
Consumer polls 500 ordered records and hands them to a thread pool or awaits them without sequencing → you just rebuilt the race inside your own app. Keep all work for one key on one worker.
Platform cheat sheet
- Kafka / Kinesis — order per partition
- SQS FIFO — order per MessageGroupId; one in-flight message per group; 300 ops/s per API (3000 batched)
- Pub/Sub — ordering keys, publisher and subscriber in the same region, ~1 MB/s per key
Quick check on a live topic
kafka-consumer-groups --bootstrap-server localhost:9092 \
--group billing --describe
If one partition's LAG is 400k and the rest are ~0, you don't have a throughput problem. You have a key-choice problem.
Your exercise
You have a 6-partition topic keyed by user_id and need 12 partitions for throughput. Design the migration so no single user's events ever go out of order. You can't rehash history — so: do you dual-write to a new topic, or drain first? Where does the cutover offset live, and what's the smallest window where a user's events are paused rather than reordered?