You can delay messages in Kafka by delaying their production or delaying their processing after they are consumed.
In itself, Kafka doesn’t support delayed messages.
Delayed messages are when a message broker keeps messages for a set amount of time (the delay) after the messages have been produced to the broker.
The broker will send the messages to the consumers only after the delay has elapsed.
To delay messages in Kafka at their production, use a job scheduler to produce the messages after the delay.
Using a job scheduler is safer than delaying inside your codebase.
Your code could stop running because of an error.
But a scheduler is perfect for its job of executing jobs at its timing.
After the scheduler produces to Kafka, consumers will poll from Kafka immediately.
The net effect is the delay was respected.
To delay messages in Kafka at their consumption, introduce timestamps in the message body.
Add the delay to the current time and save the sum (the future timestamp) inside the message.
Produce the message and the consumer will poll it from Kakfa immediately.
However, the trick is in the consumer using the message.
Configure the consumer code to only use up the message contents after the timestamp that was set inside the message.
In summary, while Kafka doesn’t natively delay messages, you can delay messages while using it.
Produce the message after the delay or process a consumed message after the delay has elapsed.
Either way, you would have delayed messages in Kafka.