Sobes.tech
Middle

We have a Topic with 4 partitions. How to make the producer write messages from the same user only to one partition?

sobes.tech AI

Answer from AI

To ensure that a Kafka producer writes messages from the same user to only one partition, you need to use a key when sending messages. Kafka uses the key to determine the partition via a hash function, so the same key will always go to the same partition.

Example in Python using the kafka-python library:

from kafka import KafkaProducer
import json

producer = KafkaProducer(bootstrap_servers='localhost:9092',
                         value_serializer=lambda v: json.dumps(v).encode('utf-8'))

user_id = 'user123'
message = {'event': 'login', 'user': user_id}

# Send message with key user_id so all messages from this user go to the same partition
producer.send('topic_name', key=user_id.encode('utf-8'), value=message)
producer.flush()

Thus, all messages with the same key user_id will go to the same partition.

We have a Topic with 4 partitions. How to make the… - sobes.tech