Lesson 1 of 9 · 25m
The Partitioned Log: Ordering, Keys, and Parallelism
Understand Kafka as replicated append-only logs, why ordering is per partition, and how key choice shapes both correctness and throughput.
Kafka clicks the day you stop picturing a message queue and start picturing a distributed commit log that consumers read at their own pace. I have watched teams spend weeks chasing "lost ordering" when the system was doing exactly what they configured: ordering within a partition, parallelism across partitions. Kafka is simple in the way a sawmill is simple — powerful machinery, sharp edges, no forgiveness for a bad mental model.
A topic is many logs, not one pipe
A topic is divided into partitions. Every record lands in exactly one partition and receives an offset: a monotonically increasing position in that partition:
topic: payments.authorized partitions: 12
partition 0 offset 98122 -> key=acct-41 amount=19.00
partition 0 offset 98123 -> key=acct-87 amount=72.10
partition 1 offset 77104 -> key=acct-41 amount=12.99
partition 1 offset 77105 -> key=acct-19 amount=50.00Ordering is guaranteed only inside one partition. acct-41 appears twice here, but across different partitions, so consumers can process those records in either relative order. If business correctness requires "all events for this account in order," they must share a partition.
The key chooses the partition
With the default partitioner, a record key is hashed and mapped onto a partition:
# producer behavior, conceptually
partition = hash(key) % num_partitionsThe practical rule: choose the key that represents the ordering boundary. Customer ID, order ID, device ID, or account ID are common choices. Do not choose a high-cardinality random UUID merely because it distributes evenly if downstream logic needs entity ordering.
In the Java producer:
ProducerRecord<String, String> record =
new ProducerRecord<>("payments.authorized", payment.accountId(), payload);A null key is not evil, but it is a decision. It usually means you want spread, not entity ordering.
Adding partitions is not free relighting
Increasing a topic's partition count changes where future keys land. Existing records do not move. Worse, relative ordering for a key can split across the old and new partition assignment after the increase:
kafka-topics.sh --bootstrap-server kafka-1:9092 --alter --topic payments.authorized --partitions 24Treat repartitioning as a data-model migration, not an innocuous tuning knob. If consumers buffer by key, coordinate deployment and flush boundaries deliberately.
Partition count controls ceiling, not speed by magic
Consumers in one group can process partitions in parallel, but no two group members can own the same partition. Twelve partitions means at most twelve actively consuming members in that group. More partitions increase filesystem spread, metadata, replication traffic, recovery time, and end-to-end latency variance. More is not automatically better.
Design from the invariant backwards
Ask what must be serialized: balance changes per account? state transitions per order? inventory mutations per warehouse? That answer determines keying and partitioning. Ask what can happen concurrently: independent customers, unrelated devices, separate shipments. That answer determines parallelism.
Field notes
- Ordering is per partition, full stop. Any architecture needing global ordering will eventually serialize down to one partition or another system.
- A hot key can overload one partition even when cluster-wide throughput looks healthy.
- The same key generally stays in the same partition only while partition count is unchanged and the partitioner is compatible.
- Use keys that are stable, business-meaningful, and bounded in cardinality; do not key by timestamp unless you enjoy hotspots.
- Partition count is easiest to increase and painful to decrease. Leave room, but not a thousand empty rooms.
Was this lesson helpful?