Kafka topics organize related events, and each event is stored as a record containing a key, value, timestamp, and other metadata.
But what happens when a topic receives millions of events every second?
Storing and processing all those events on a single server would quickly become a bottleneck. We need a way to distribute the workload across multiple machines while still maintaining ordering where it matters.
Kafka solves this problem using partitions.
Partitions are one of the most important concepts in Kafka. They determine how data is stored, how workloads are distributed across brokers, and how many consumers can process records in parallel.
Let's understand how they work.
A partition is an ordered, append-only log of records within a Kafka topic.
Every Kafka topic contains one or more partitions, and every record published to a topic is stored in exactly one of those partitions.
For example, suppose we have an orders topic with three partitions:
Each partition maintains its own independent sequence of records.
When a producer publishes a new record, Kafka appends it to the end of the selected partition.
Existing records are not modified or rearranged. They remain in their original order until Kafka removes them according to the topic's retention or cleanup policy.
This append-only design is one reason Kafka can handle large volumes of data efficiently.
Instead of constantly inserting or updating records throughout a data structure, Kafka primarily writes new records sequentially to its partition logs.
Each record in a partition receives a unique, sequentially increasing number called an offset.
An offset identifies the record's position within that partition.
For example:
When a new record arrives, Kafka appends it to the partition and assigns it the next offset.
Offsets are maintained independently for every partition.
This means Partition 0 and Partition 1 can both have a record at offset 5. The combination of topic, partition, and offset identifies a record's location.
Another important detail is that offsets are logical positions, not necessarily the number of records currently stored. As old records are deleted or compacted, gaps may appear in the available offsets.
Consumers use these offsets to track which records they have read and where they should resume processing.
Partitions solve three important problems: scalability, parallel processing, and ordering.
A Kafka cluster typically contains multiple servers called brokers.
Instead of storing an entire topic on one broker, Kafka can distribute its partitions across different brokers.
Suppose our orders topic has three partitions and the cluster has three brokers.
A possible arrangement is:
Here, each broker handles a portion of the topic's data.
If the Order Service publishes records to all three partitions, Kafka can distribute the workload across the three brokers.
This allows the cluster to use the CPU, memory, disk, and network resources of multiple machines instead of relying on a single server.
For example, if a single partition can sustain 10 MB/s of writes under a particular workload, distributing three partitions across three brokers might allow the topic to handle approximately 30 MB/s.
Actual throughput depends on factors such as hardware, record size, replication, batching, and broker configuration.
The important idea is that partitions allow Kafka to scale a topic horizontally across brokers.
Partitions also determine how consumers can process records in parallel.
Suppose the orders topic has three partitions and we have three consumers in the same consumer group.
Kafka can assign one partition to each consumer:
Each consumer reads records from its assigned partition independently.
This allows three consumers to process records simultaneously, increasing the overall throughput of the application.
Now suppose we add a fourth consumer to the same group.
Since the topic has only three partitions, the fourth consumer will not receive an assignment for this topic.
Within a consumer group, a partition is assigned to at most one consumer at a time.
This means that, for a group consuming only this topic, the number of partitions limits how many consumers can actively read from it in parallel.
A single consumer can handle multiple partitions, but a partition cannot be simultaneously assigned to multiple consumers within the same group.
Different consumer groups can still read the same partitions independently.
For example, an Analytics consumer group and a Notification consumer group can both read all three partitions without interfering with each other.
This partition-based assignment is a key part of Kafka's consumer scalability model.
Kafka also uses partitions to provide ordering guarantees.
Within a partition, records are stored in the order they are appended.
For example:
A consumer reading this partition sequentially encounters these records in offset order.
However, Kafka does not guarantee ordering across different partitions.
Consider the following example:
Kafka maintains the order within each partition, but it doesn't guarantee whether an event from Partition 0 will be consumed before an event from Partition 1.
This distinction matters when designing event-driven applications.
For example, if all events related to a particular order must be processed in sequence, they should be routed to the same partition.
One common approach is to use the order ID as the record key.
With consistent key-based partitioning, records for the same order are routed to the same partition.
So far, we've assumed that each partition is stored on a different broker.
In practice, Kafka uses partition replication to provide fault tolerance.
A topic can be configured with a replication factor, which determines how many copies of each partition Kafka maintains.
For example, consider a topic with three partitions and a replication factor of three.
Kafka maintains three replicas of every partition, typically placing them on different brokers.
For each partition, one replica acts as the leader, while the others act as followers.
A possible arrangement is:
The leader handles writes for its partition, while followers replicate the data from the leader.
If a leader broker fails, Kafka can elect an eligible replica as the new leader, allowing the partition to continue operating.
The availability and durability guarantees depend on the replication configuration, synchronization state, and producer acknowledgment settings.
Replication and partitioning solve different problems:
A topic with three partitions and a replication factor of three has nine partition replicas in total, but it still contains only three logical partitions. Replication does not increase the number of partitions available for consumer parallelism.
Let's create a topic with three partitions using Kafka's command-line tools.
Assuming Kafka is running locally on port 9092, execute:
This creates an orders topic with three partitions and one replica per partition.
A replication factor of one is suitable for a single-broker local environment, but it doesn't provide fault tolerance.
To inspect the topic, run:
For each partition, the output includes information such as:
For a three-partition topic, you'll see entries for partitions 0, 1, and 2.
Kafka allows you to increase the partition count of an existing topic.
For example, to increase our topic from three partitions to six:
Kafka creates three additional partitions without redistributing the records already stored in the original partitions.
Increasing the partition count can change how record keys map to partitions. New records for an existing key might be routed to a different partition, potentially affecting ordering for that key.
This is one reason partition counts should be planned carefully.
Kafka does not support directly decreasing the number of partitions in an existing topic.
There is no single partition count that works for every application.
The right number depends on your expected throughput, consumer parallelism, and infrastructure.
For example, suppose an application needs to process 60,000 events per second, and a single consumer can process approximately 10,000 events per second.
You would need at least six consumers to handle that workload.
Since each consumer needs an assigned partition to read independently, the topic would need at least six partitions.
However, you might choose more partitions to support future growth and distribute the workload more evenly.
Keep in mind that adding partitions is not always the solution to performance problems.
If one partition receives most of the traffic because of a frequently used key, adding more partitions may not resolve the bottleneck.
Creating too many partitions also has a cost. It increases metadata, storage, and operational overhead.
A Kafka topic is divided into one or more partitions, and each partition is an ordered, append-only log of records.
Partitions allow Kafka to distribute data across brokers, process records in parallel, and maintain ordering within individual partitions.
Each partition has its own offsets, and each consumer group assigns a partition to at most one consumer at a time.
Kafka can also replicate partitions across brokers to improve availability and fault tolerance.
The number of partitions influences throughput, consumer parallelism, and operational overhead, making it an important design decision.
10 quizzes