Kafka topic partitions
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform used for building real-time data pipelines and streaming applications. Kafka enables you to publish and subscribe to streams of records, store records in a fault-tolerant way, and process those records as they occur. One of the core components of Kafka's scalability and performance is its use of topic partitions.
What Are Partitions?
In Kafka, a topic is a category or feed name to which records are published. Topics in Kafka are divided into one or more partitions. Partitions allow the log data of a topic to be spread across multiple servers. This partitioning provides several benefits:
- Parallelism: Partitions allow multiple consumers to read from a topic in parallel, enhancing the system's throughput.
- Scalability: Data is split across multiple brokers (servers), letting the system grow by adding more brokers.
- Fault Tolerance: Partitions can be replicated across multiple brokers to ensure that data is not lost if a broker fails.
Each partition is an ordered, immutable sequence of records that is continually appended to—a structured commit log. Records in the partitions are each assigned a sequential ID number known as the offset that uniquely identifies each record within the partition.
How Partitions Work
When a producer publishes data to a topic, Kafka does not send all data to a single partition. The assignment of a record to a partition is typically handled in one of two ways:
- Round-robin: This is the default partition assignment strategy. It distributes data evenly across all partitions ensuring balanced load.
- Key-based partitioning: In this method, a key is specified along with the message, which influences which partition the record will be sent to. Records with the same key always go to the same partition. This ensures that records with the same key maintain their order relative to each other.
Replication
Kafka can replicate partitions across multiple brokers. This replication provides redundancy and higher availability. Each partition has one ‘leader’ and zero or more ‘followers’. The leader handles all read and write requests for the partition, and the followers replicate the leader. If the leader fails, one of the followers will automatically become the new leader.
Example: Create a Topic with Partitions
To create a topic with multiple partitions in Kafka, you would use the Kafka command-line tools provided within a Kafka distribution:
This command creates a new topic named example-topic with 3 partitions and a replication factor of 2.
Best Practices for Partitioning
While configuring partitions, it’s essential to consider the following:
- Number of partitions: This affects scalability, performance, and fault tolerance. More partitions allow greater parallelism but can increase the overhead on Kafka’s coordination layer.
- Replication factor: This should be set based on the criticality of the data. More replicas increase fault tolerance.
- Partition count and broker capacity: Balancing the number of partitions with broker capacity and performance is crucial. Too many partitions per broker can degrade performance.
Summary Table
| Feature | Description | Benefits |
| Parallelism | Multiple partitions allow multiple consumers to read in parallel. | Increased throughput |
| Fault Tolerance | Partitions can be replicated across brokers to prevent data loss. | High availability |
| Scalability | Spreads data across multiple brokers, facilitating more scalable patterns. | Facilitates system growth |
| Ordered messages | Records within a partition maintain a specific order. | Guarantees order within partitions |
Understanding and utilizing partitions effectively is key to harnessing the full potential of Apache Kafka. By strategically configuring partitions, businesses can ensure that their Kafka deployment is scalable, resilient, and optimized for high performance.
Related reading
- Kafka topic partitions to Spark streaming
- Kafka topic per producer
- Kafka Topic vs Partition topic
- Kafka transaction failed but commits offset anyway
- Kafka unable to connect to Zookeeper
- Kafka uncommitted messages
- Kafka transactionLog fails with NotEnoughReplicasException, despite correct config
- Kafka unable to start Kafka - process can not access file 00000000000000000000.timeindex

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.