Senior
Kuidas tagada, et kõik tellimuse sündmused jõuavad ühte Kafka partitsiooni?
sobes.tech AI
Vastus AI-lt
To ensure that all events related to a single order go into the same Kafka partition, you need to use a key when sending messages, which uniquely identifies the order.
Kafka uses the key to compute the partition via a hash function. If the same key (e.g., order_id) is used for all events of an order, they will go into the same partition.
Example in Go with sarama (a popular Kafka client):
producer.Input() <- &sarama.ProducerMessage{
Topic: "orders",
Key: sarama.StringEncoder(orderID), // orderID — order identifier
Value: sarama.StringEncoder(eventData),
}
This guarantees that all events with the same key will be in the same partition, which is important for ordered processing and consistency.