× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

In Apache Kafka why can't there be more consumer instances than partitions?

Feb 16, 2015
<p>Ok, to understand it, one needs to understand several parts.</p> <ol> <li>In order to provide ordering total order, the message can be sent only to one consumer. Otherwise it would be extremely inefficient, because it would need to wait for all consumers to recieve the message before sending the next one:</li> </ol> <blockquote> <p>However, although the server hands out messages in order, the messages are delivered asynchronously to consumers, so they may arrive out of order on different consumers. This effectively means the ordering of the messages is lost in the presence of parallel consumption. Messaging systems often work around this by having a notion of "exclusive consumer" that allows only one process to consume from a queue, but of course this means that there is no parallelism in processing.</p> <p>Kafka does it better. By having a notion of parallelism—the partition—within the topics, Kafka is able to provide both ordering guarantees and load balancing over a pool of consumer processes. This is achieved by assigning the partitions in the topic to the consumers in the consumer group so that each partition is consumed by exactly one consumer in the group. By doing this we ensure that the consumer is the only reader of that partition and consumes the data in order. Since there are many partitions this still balances the load over many consumer instances. Note however that there cannot be more consumer instances than partitions.</p> <p><strong>Kafka only provides a total order over messages within a partition, not between different partitions in a topic.</strong></p> </blockquote> <p>Also what you think is a performance penalty (multiple partitions) is actually a performance gain, as Kafka can perform actions of different partitions completely in parallel, while waiting for other partitions to finish.</p> <ol start="2"> <li>The picture show different consumer groups, but the limitation of maximum one consumer per partition is only within a group. You still can have multiple consumer groups.</li> </ol> <p>In the beginning the two scenarios are described:</p> <blockquote> <p>If all the consumer instances have the same consumer group, then this works just like a traditional queue balancing load over the consumers.</p> <p>If all the consumer instances have different consumer groups, then this works like publish-subscribe and all messages are broadcast to all consumers.</p> </blockquote> <p>So, the more subscriber groups you have, the lower the performance is, as kafka needs to replicate the messages to all those groups and guarantee the total order. </p> <p>On the other hand, the less group, and more partitions you have the more you gain from parallizing the message processing.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25896109/In%20Apache%20Kafka%20why%20can't%20there%20be%20more%20consumer%20instances%20than%20partitions?/25943742">Stack Overflow</a>.</p>
comments powered by Disqus