Architecting Real Time Data with Apache Kafka
Scaling via Partitions
The Unit of Scale
In Kafka, a topic isn't a single, monolithic log. Instead, it's divided into smaller, independent logs called partitions. Think of a single topic as a large book, and each partition as a separate chapter. This division is the fundamental mechanism that allows Kafka to scale horizontally.
Each partition is an ordered, immutable sequence of records. When a new record is written to a topic, it's appended to one of its partitions. This structure allows work to be split across multiple machines, both for writing data and for reading it.
Leadership and Replication
Partitions are distributed across the different servers, or brokers, in a Kafka cluster. For any given partition, one broker is designated as the 'leader.' This leader is responsible for handling all read and write requests for that specific partition. It's the single source of truth.
Other brokers that hold a copy of the partition are called 'followers.' Their only job is to replicate the data from the leader. They passively copy the leader's log to stay in sync. This replication provides fault tolerance. If a leader broker fails, one of the in-sync followers is automatically elected as the new leader, ensuring data remains available without interruption.
This design distributes the load across the cluster. A topic with many partitions will have its leadership spread out, preventing any single broker from becoming a bottleneck.
Scaling with Consumers
The number of partitions in a topic directly dictates the maximum level of parallelism for your consumers. Within a single consumer group, each partition can only be assigned to one consumer. Therefore, if a topic has 10 partitions, you can run up to 10 consumer instances in the same group, each processing a unique subset of the data in parallel.
If you have more consumers than partitions in a group, the extra consumers will sit idle. If you have fewer consumers than partitions, some consumers will read from multiple partitions. This relationship makes the partition count a critical decision for application throughput.
The number of partitions is the hard limit for your consumer parallelism within a single consumer group.
Choosing the right number of partitions involves a trade-off. Too few, and you cap your potential throughput, creating a bottleneck if your processing needs grow. Too many, and you can introduce other problems. Each partition requires file handles and memory on the brokers. A very high number of partitions (thousands per broker) can increase end-to-end latency, as it puts more stress on the controller for metadata management and leader elections.
Distributing Data
When a producer sends a record, how does it decide which partition to use? There are two primary strategies.
If the record has no key, the producer will distribute records in a round-robin fashion across all available partitions of the topic. This ensures a roughly even spread of data, which is great for stateless processing where message order doesn't matter.
If the record does have a key (like a userID or orderID), Kafka uses a deterministic hashing function. The producer hashes the key and maps the result to a specific partition. This guarantees that all records with the same key will always land in the same partition, preserving the order of events for that key. This is essential for stateful applications that need to process events for a specific entity in the sequence they occurred.
Understanding partitions is key to designing a Kafka architecture that is both fast and resilient. It's the mechanism that allows a system to start small and grow to handle massive streams of data by simply adding more brokers and consumers.
In the context of Kafka, what is a partition?
What is the primary responsibility of a 'leader' broker for a given partition?