Junior — Middle
Is it possible to ensure sequential message output from Kafka so that they are in the same order as they were written to the Topic?
sobes.tech AI
Answer from AI
Kafka guarantees message order only within a single partition of a topic. If you want to ensure sequential output of messages in the same order they were written, you need to:
- Use one partition for all messages to preserve order.
- Consume messages from this partition sequentially with a single consumer.
If the topic has multiple partitions, message order between them is not guaranteed, as each partition is processed independently.
Example of producer configuration for sending to a single partition:
ProducerRecord<String, String> record = new ProducerRecord<>("topic", 0, key, value);
producer.send(record);
Here, partition 0 is explicitly specified so that all messages go to one partition and the order is maintained.