Kafka
Topic Partitions
Data Distribution
Message Brokers
Kafka Architecture

How Kafka distributes the topic partitions among the brokers

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform that has the capability to publish and subscribe to streams of records, similar to a message queue or enterprise messaging system. One of the key components of Kafka’s high throughput and scalable architecture is how it manages and distributes topic partitions across various brokers in a Kafka cluster. Let's delve deeper into understanding this distribution process, including its mechanisms, the role of the partition leader, and examples for clarity.

Partitioning in Kafka Topics

When a topic is created in Kafka, it can be divided into multiple partitions. This partitioning allows the topic's data to be split across multiple brokers in the cluster, facilitating load balancing, failover, and increased throughput. Each partition of a topic is an ordered, immutable sequence of records that is continually appended to—a structured commit log.

Distribution of Partitions

Kafka’s approach to distributing partitions across brokers is designed to balance the load and ensure data availability and resilience. Here’s how the distribution works:

  1. Uniform Distribution: When you create a topic with a specified number of partitions, Kafka attempts to distribute these partitions as uniformly as possible across the available brokers in the cluster. This distribution is important for load balancing, ensuring no single broker becomes a bottleneck.
  2. Replication for Fault Tolerance: Each partition can be replicated across multiple brokers. This means that copies of each partition exist on several brokers. The number of replicas is configured per topic. Each partition has one broker that acts as the leader and zero or more brokers acting as followers. The leader handles all read and write requests for the partition, while the followers replicate the leader.
  3. Leader Election: In the event of a broker failure that hosts the leader partition, one of the follower partitions will be promoted to the new leader. This ensures high availability and fault tolerance. Leader election is handled by ZooKeeper in Kafka versions preceding 2.8, and by Kafka itself in newer versions through the use of KRaft (Kafka Raft Metadata mode).
  4. Manual or Automatic Rebalancing: Kafka Cluster allows for rebalancing of partitions either manually using tools like the Kafka-reassign-partitions tool or automatically in the case of broker failure, addition or removal. Rebalancing helps in evenly distributing the load particularly when the cluster’s composition changes.

Example of Partition Distribution

Consider a Kafka cluster with three brokers and a topic "TopicA" that has 3 partitions with a replication factor of 2. The possible distribution could look like something below:

  • Partition 0
    • Leader: Broker 1
    • Follower: Broker 2
  • Partition 1
    • Leader: Broker 2
    • Follower: Broker 3
  • Partition 2
    • Leader: Broker 3
    • Follower: Broker 1

This setup ensures that each broker is a leader for one partition and a follower for another, thus balancing the load.

Table: Key Features of Kafka Partition Distribution

FeatureDescription
ScalabilityPartitions are distributed across different brokers to handle more data and more consumers.
Fault ToleranceReplication of partitions across multiple brokers allows for failure of a broker without losing data.
Load BalancingUniform distribution of partitions and leaders across the brokers prevents bottlenecks.
High AvailabilityLeader election ensures partitions are always available for writing and reading despite broker failures.

Additional Considerations

In production environments, care must be taken to monitor the distribution and status of topic partitions and their replicas. Tools integrated into Kafka, like Kafka Manager or Confluent Control Center, can provide valuable insights and operational capabilities. Moreover, understanding the implications of partition count and replication factors on performance and durability is crucial for effective Kafka deployment.

Partition distribution in Kafka is a fundamental aspect of its design, enabling Kafka to deliver high-throughput, resilient, and distributed data streaming capabilities. Knowledge of this topic is essential for anyone involved in designing, deploying, or managing Kafka clusters.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.