How to send message to a particular partition in Kafka?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka, a popular distributed streaming platform, uses topics for organizing records. These topics are further divided into partitions. Understanding how to send messages to a specific partition in Kafka is mandatory for many scenarios, such as ensuring order within partitions or customizing the partition logic for load balancing.
Understanding Kafka Partitions:
In Kafka, a topic is split into one or more partitions. This facilitates distributing the data across multiple brokers for fault tolerance and increased throughput. Normally, Kafka determines the partition for a record based on the key of the message using a default partitioner. Essentially, if the key is not null, Kafka will hash the key to consistently map it to a specific partition.
Sending a Message to a Specific Partition:
To control which partition a record is sent to explicitly, you must provide a partition number when sending the record. There are three primary ways to achieve this in Kafka:
- Explicit Partition Specification: Directly specify the partition when sending a message.
- Custom Partitioner: Implement a custom partitioner that determines the partition based on logic you define.
- Key Based Partitioning: Use keys strategically and rely on Kafka’s native partitioning logic.
Example using Kafka Producers:
Let's delve into the details and examples using Kafka producers for above methods:
1. Explicit Partition Specification
You can explicitly specify the partition when producing a message. This approach ignores any key that might be part of the message.
Here, partitionNumber is the specific partition you want to send the message to.
2. Custom Partitioner
For scenarios where messages need to be distributed to partitions following a specific logic not covered by Kafka’s default partitioner, you can create a custom partitioner:
Register this partitioner in your producer configuration:
3. Key Based Partitioning
When a key is provided, and you rely on Kafka's default partitioning, the partition is determined by hashing the key and using modulo operation on the number of partitions:
This will use the default partitioner if no custom partitioner has been specified.
Summary Table:
| Method | Description | Use Case |
| Explicit Partition | Directly specify the target partition. | Full control over message placement. |
| Custom Partitioner | Implement custom logic to determine partitions. | Complex distribution logic needed. |
| Key Based Partitioning | Utilize Kafka's default mechanism which hashes the key to assign partitions. | Simple, scalable partitioning. |
Additional Considerations:
- Performance Impact: Be cautious with partition logic as it can affect both producer and consumer performance.
- Scalability: Custom partitioning logic should handle changes in the number of partitions.
- Fault Tolerance: Misbehaving partitioners or explicit partitioning that does not account for partition availability can cause data loss or downtime.
Conclusion:
Sending messages to a specific partition in Kafka allows for advanced message distribution strategies, crucial for applications requiring ordered consumption or special processing guarantees. By understanding these methods and their applications, developers can better exploit Kafka's capabilities in data streaming scenarios.
Related reading
- How to send messages synchronously in kafka?
- How to send output of two different Spout to the same Bolt?
- How to set cleanup.policy 'delete' AND 'compact' for a Kafka topic?
- How to set consumer.id for new Kafka consumer
- How to set group name when consuming messages in kafka using command line?
- How to set group.id for consumer group in kafka data source in Structured Streaming?
- How to set kafka-connect connector and task's JVM heap size?
- how to set kafka consumer concurrency using spring boot

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.