Kafka topics are divided into partitions. You can control how messages are assigned to these partitions using several strategies:
Load Balanced Partitioning
Distributes messages evenly across partitions to ensure balanced consumer load. If no key is provided, the producer assigns a partition randomly.
Semantic Partitioning
Assigns messages to partitions based on a specific property (e.g., a session_id). This ensures that all messages related to a specific entity are processed by the same consumer instance, facilitating local state management.
Custom Partitioning
To ensure compatibility with other Kafka clients (like Java-based ones), you can implement a custom partitioner. You can pass a class that responds to call(partition_count, message) or a Proc to the Kafka.new constructor.