Senior
How to ensure that all events for a single order go into one Kafka partition?
sobes.tech AI
Answer from AI
To ensure that all events for a single order go to the same Kafka partition, you need to use a key when sending messages that uniquely identifies the order.
Kafka uses the key to determine the partition via a hash function. If all events of the same order use the same key (for example, order_id), they will go to the same partition.
Example in Go using sarama (a popular Kafka client):
producer.Input() <- &sarama.ProducerMessage{
Topic: "orders",
Key: sarama.StringEncoder(orderID), // orderID — order identifier
Value: sarama.StringEncoder(eventData),
}
Thus, it is guaranteed that all events with the same key will be in one partition, which is important for ordered processing and consistency.