Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What ways can a Consumer consume message in Kafka?

Tags:

apache-kafka

If there is a Kafka server "over there" somewhere across the network I would assume that there might be two ways that a Consumer could consume the messages:

  1. By first of all 'subscribing' to the Topic and in effect telling the Kafka server where it is listening so that when a new message is Produced, Kafka proactively sends the message to the Consumer, across the network.

  2. The Consumer has to poll the Kafka server asking for any new messages, using the offset of the messages it has currently taken.

Is this how Kafka works, and is the option configurable for which one to use?

like image 423
Zuriar Avatar asked Oct 19 '22 14:10

Zuriar


1 Answers

I'm expanding my comment into an answer.

Reading through the consumer documentation, Kafka only supports option 2 as you've described it. It is the consumers responsibility to get messages from the Kafka server. In the 0.9.x.x Consumer this is accomplished by the poll() method. The Consumer polls the Kafka Server and returns messages if there are any. There are several reasons I believe they've chosen to avoid supporting option 1.

  1. It limits the complexity needed in the Kafka Server. It's not the Server's responsibility to push messages to a consumer, it just holds the messages and waits till a consumer fetches them.
  2. If the Kafka Server was pushing all messages to the consumers, it could overwhelm a consumer. Say a Producer was pushing messaging into a Kafka Server 10 msg/sec, but a certain Consumer could only process 2 msg/sec. If the Kafka Server attempted to push every message it received to that Consumer, the Consumer would quickly be overwhelmed by the number of messages it receives.

There's probably other reasons, but at the moment those were the two I thought about.

like image 133
Morgan Kenyon Avatar answered Oct 21 '22 04:10

Morgan Kenyon