Handling Kafka Consumer Processing Timeouts

111 views Asked by At

I have a C++ service that is processing Kafka messages, like this:

while (!shutdown)
{
    auto msg = KafkaConsumer.poll();
    ProcessMessage(msg);    
}

Calling poll does two things. It commits the previously returned message as complete and then returns the next message.

The problem is that if poll returns a message and you take too long to process it before calling poll again (i.e. if the ProcessMessage function above takes too long) then the message is timed out and it becomes available for other consumers to pick it up again.

You can control the timeout using the "session.timeout.ms" setting, but in my case it's impossible to set a timeout for all cases.

What I want to know is whether a Consumer can check if a message it gets from a Poll request has already been given to a consumer before. i.e. I want to check if this is effectively a 2nd attempt at processing this message (A retry).

My concern is that if ProcessMessage times out once then it's likely to time out every time that message is returned from Poll, and therefore we'll get into an endless retry loop, so I want a way of checking to see if the message has been retried X times already so that we can effectively 'give up' after a certain number of retries.

Of course I could handle this myself by tracking retries though some other mechanism (e.g. Redis), but I wanted to know if Kafka has anything built in for this?

Thanks Ben

0

There are 0 answers