Search code examples
apache-kafkakafka-consumer-api

Apache kafka - consumer delay option


I want to start a consumer in Kafka for a particular topic in a small delay. In detail, I want the consumer to start consuming the messages from the topic after a particular time delay from the time of producing the messages.

Is there any property or option in Kafka to enable it?


Solution

  • We did the same stuff for spark-streaming. I hope, the approach can suits you also.

    The idea is very simple - use Thread.sleep. When you receive new message from kafka, you can calculate how long do you need to sleep before process it.

    pseudocode for idea:

    message = getNextMessageFromKafka()
    sleepMs = Math.max(0, currentTime - message.timestamp)
    Thread.sleep(speepMs)
    do processing