Message routing in Kafka: partitions, keys and custom partitioners
Codemia Knowledge Hub ·
Kafka routing happens at the partition level, and the producer makes the decision. A record with a key goes to hash(key) % number_of_partitions, so the same key always lands in the same partition. Records without a key are spread across partitions by the producer's default partitioner, and you can plug in a custom partitioner for anything else.
Topics and partitions
A topic is a named feed that records are published to. Topics are multi-subscriber: they can have zero, one or many consumers.
Partitions parallelize a topic by splitting its data across brokers. Each partition can live on a different broker, so multiple consumers can read the topic in parallel.
How the producer chooses a partition
- No key: the default partitioner distributes keyless records across the topic's partitions itself.
- Key-based partitioning: when the message has a key, the partition is
hash(key) % number_of_partitions. Messages with the same key always go to the same partition, which groups them by key. - Custom partitioner: producers can implement their own partitioning logic.
Sending a keyed record
Every message with the same key is routed to the same partition, which preserves order within that key.
What brokers do
Brokers are the servers that store data and serve clients. They receive messages from producers, store them in the right partitions, and answer consumer fetch requests for those partitions.
Consumer groups split the partitions
Consumers can join consumer groups. Within a group, each consumer reads from its own exclusive set of partitions, which spreads the load. If a consumer fails, Kafka rebalances the partitions among the remaining members so processing continues.
| Feature | Description | Use-Case |
| Partitioning | Distributes topic messages across partitions | Load balancing, Parallel processing |
| Key-Based Routing | Routes same key messages to the same partition | Data locality, Stateful operations |
| Custom Partitioner | Allows custom-defined partitioning logic | Special routing needs |
| Scalability | Topics can be scaled by adding more partitions | Growing data needs |
Partition reassignment and throttling
Partition reassignment moves partitions between brokers. It is useful for:
- Spreading partitions onto brokers when the cluster grows or shrinks.
- Balancing partitions across the cluster for even load.
- Recovering from broker failures to keep data available and durable.
Throttling limits the data transfer rate during rebalancing or when adding brokers, so the cluster keeps performing well during maintenance.
Related reading
- Message sequence acting unexpectedly in RabbitMQ
- Message timestamp and commit_timestamp in Kafka's __consumer_offsets
- Messages lost if queue does not exist
- Messages lost when Kafka nodes are restarted
- Messaging vs RPC in a distributed system (Openstack vs K8s/Swarm)
- Metadata requests in Kafka producer
- Moving data from Snowflake to Kafka
- Multiple windows of different durations in Spark Streaming application
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.