Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to maintain message ordering between partitions of a kafka topic with a single consumer?

We are developing a kafka based streaming system in which the producer would produce to multiple partitions within its topic and a single consumer would consume from the topic. I know that kafka maintains message order within partitions, but can we maintain a global message order between partitions within a topic?

like image 231
Pranay Avatar asked Apr 21 '16 06:04

Pranay


People also ask

Does Kafka maintain order across partitions?

First of all, Kafka only guarantees message ordering within a partition, not across partitions. This places a burden on the producers and consumers to follow certain Kafka design patterns to ensure ordering. For example, the ability to partition data by key and one consumer per partition.

Can a single Kafka consumer read from multiple partitions?

When the number of consumers is lower than partitions, same consumers are going to read messages from more than one partition. In your scenario, a single consumer is going to read from all your partitions. This type of consumer is known as exclusive consumer. This happens when consumer groups have only one consumer.

How do you ensure the order of events consuming from Kafka is maintained?

As per Kafka guarantees: Messages sent by a producer to a particular topic partition will be appended in the order they are sent. To ensure strict ordering across all events, the topic should be single partitioned.


1 Answers

Short answer: no, Kafka does not provide any ordering guarantees between partitions.

Long answer: I don't quite understand your problem. If you are saying you have only one consumer consuming your topic, why would you have more than 1 partition in that topic and reinvent the wheel trying to maintain order between partitions? If you want to leave some space for future growth, e.g. adding another consumer to consume a part of partitions, then you'll have to rethink your "global message order" idea.

Do you really need ALL messages to be processed in order? Or maybe you could partition by client/application/whatever and maintain order per partition? In most cases you don't really need that global message order, but just have to partition your data properly.

Maintaining order between multiple consumers is a really tough problem to solve, and even if solved correctly you'll just neglect all Kafka benefits.

like image 71
serejja Avatar answered Oct 03 '22 04:10

serejja