Topics, Partitions, and the Commit Log
Explore the physical mechanics of partition sharding, parallel write distribution, and why Kafka guarantees ordering strictly within individual partitions.
A naive message queue stores all messages in a single centralized queue file. As message volume increases, that single file becomes a severe disk I/O bottleneck, and concurrent consumers must lock the queue to pull messages safely.
Apache Kafka scales past millions of operations per second by replacing single queues with sharded, append-only commit logs known as partitions.
In this lesson, you will examine the physical mechanics of partition sharding, understand how Kafka guarantees ordering, and learn how to size partitions for high-throughput production clusters.
01The Partition as the Unit of Scalability
When you create a Kafka topic, you specify its partition count. The partition is the fundamental unit of parallelism, replication, and storage in Kafka.
Consider the retail topic orders configured with three partitions and distributed across three broker nodes:
- Broker 101 hosts the physical commit log for
Partition 0. - Broker 102 hosts the physical commit log for
Partition 1. - Broker 103 hosts the physical commit log for
Partition 2.
Because each partition resides on a different physical broker, producer writes and consumer reads execute simultaneously across distinct CPU cores, network cards, and disk controllers without contention.
The Trade-Off: Partition-Level vs Global Ordering
One of the most frequent misconceptions among engineers new to Kafka is expecting total chronological ordering across an entire topic.
Kafka provides a strict ordering guarantee:
If Producer A publishes Event 1 to Partition 0 at 10:00:01, and Producer B publishes Event 2 to Partition 1 at 10:00:02, a consumer application reading both partitions may process Event 2 before Event 1.
If your business domain strictly requires total global ordering across all events, you have two choices:
- Configure the topic with exactly 1 partition: This enforces absolute chronological ordering, but caps throughput to the I/O capacity of a single broker and restricts consumption to a single active consumer thread.
- Partition by entity key: In almost all real-world domains (such as banking or e-commerce), you do not need global ordering between unrelated customers. You only need strict ordering for events belonging to the same customer or the same order. By assigning
customerIdas the message key, Kafka routes all transactions for customer 102 to Partition 1 in strict sequence, while customer 103 events flow independently to Partition 2.
Loading the rest of the lesson…