Kafka - Delayed Queue implementation using high level consumer
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a potent distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since its inception, it has developed into a full-fledged event streaming platform.
Understanding Delayed Queue
In many real-world applications, there is a need to delay the processing of messages or tasks. These scenarios can include scheduling tasks to run after a certain period, delaying retries after a failed operation, or simply spacing out the processing of events.
Kafka itself does not support delayed queues natively in the way that some other message brokers like RabbitMQ do. However, you can implement such a feature using Kafka's primitives, specifically through the use of high-level consumers and topic design.
Implementation of Delayed Queues in Apache Kafka
Implementing a delayed queue using Kafka involves a few systematic steps. Here’s how you can achieve it with high-level consumers:
Step 1: Topic Design
You’ll need to create one or multiple topics where the messages will be initially sent. This topic can act as a "waiting room" for messages before they are ready to be processed.
Step 2: Message Stamping
When a producer sends a message to the topic, it can add a timestamp indicating when the message should be available for consumption. This is typically done by inserting a delay value into the message itself, often in the headers.
Step 3: Scheduler Consumer
You create a high-level consumer whose job is to poll messages from the initial topic continuously. This consumer will check the timestamp of each message. If the current time is less than the target time of the message (current time < target time + delay), the message is not ready to be processed. Instead, the consumer sends this message to a secondary topic, often called a "delay topic."
Step 4: Delay Topic and Reprocessing
Messages in the delay topic are then consumed by another consumer, which checks if the delay has elapsed. If not, the message is sent back to the delay topic. If the delay has elapsed, it goes to a "ready" topic or is processed immediately.
Step 5: Final Consumer
A final consumer reads from the "ready" topic where all the conditions are met for message processing.
Key Challenges and Considerations:
- Multiple Consumers: Managing multiple consumers and ensuring they are in sync can be challenging.
- Resource Utilization: Continuously polling and re-writing messages can be resource-intensive.
- At-Least-Once Delivery: Ensuring that messages are not lost but are also not overly duplicated.
Summary Table of Steps and Components
| Step | Component | Description |
| 1 | Initial Topic | The topic where messages are first sent. |
| 2 | Message Stamping | Adding delay information in the message itself, typically in the headers. |
| 3 | Scheduler Consumer | High-level consumer that re-routes messages to the delay topic when not ready. |
| 4 | Delay Topic | Temporary storage for messages not yet ready to be processed. |
| 5 | Final Consumer | Consumes messages that are ready for processing. |
Additional Enhancements
- Efficient Polling: Implement smart polling mechanics to reduce CPU cycles, such as increasing the poll interval dynamically.
- Retry Logic: Incorporate logic to handle messages that fail to process even after the delay period.
- Scalability: Enhance the system to handle more topics and partitions as the load increases.
Implementing a delayed queue in Kafka, while not straightforward, can be highly effective and scalable using the right architectural approaches and Kafka’s robust streaming capabilities.
Related reading
- Kafka - Deserializing the object in Consumer
- Kafka - difference between Log end offset(LEO) vs High Watermark(HW)
- Kafka - Docker - Error when sending message from Host to Container (Batch Expired)
- KAFKA - ERROR Failed to write meta.properties due to (kafka.server.BrokerMetadataCheckpoint)
- Kafka - fetch.max.wait.ms - how does it behave with multiple partitions?
- Kafka - Message versus Record versus offset
- KAFKA and SSL java.lang.OutOfMemoryError Java heap space when using kafka-topics command on KAFKA SSL cluster
- Kafka as a message queue for long running tasks

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.