Kafka
Message Routing
Data Streaming
Distributed Systems
Big Data Architecture

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

  1. No key: the default partitioner distributes keyless records across the topic's partitions itself.
  2. 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.
  3. Custom partitioner: producers can implement their own partitioning logic.

Sending a keyed record

java
ProducerRecord<String, String> record = new ProducerRecord<>(topicName, key, value);
producer.send(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.

FeatureDescriptionUse-Case
PartitioningDistributes topic messages across partitionsLoad balancing, Parallel processing
Key-Based RoutingRoutes same key messages to the same partitionData locality, Stateful operations
Custom PartitionerAllows custom-defined partitioning logicSpecial routing needs
ScalabilityTopics can be scaled by adding more partitionsGrowing 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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design