How to build a delayed queue with Apache Kafka
Codemia Knowledge Hub ·
Apache Kafka does not natively support delayed messages, where a message is held for a set time before delivery. It is built for real-time streaming. You can still build a delay queue on top of it with dedicated delay topics, a Kafka Streams application with a state store, or an external system that holds messages until they are due. Delays come up often in task scheduling, rate limiting and business workflows.
1. Topic-based delay
Create one topic per delay length, for example topic-delay-5m, topic-delay-10m and topic-delay-1h.
- Produce: when a message needs a delay, the application sends it to the matching delay topic.
- Poll: a separate consumer polls the delay topics and checks each message's timestamp.
- Forward: once the current time is past the intended delivery time (timestamp plus delay), the consumer produces the message to the real topic, where normal consumers process it.
The costs are managing many topics for different delays and the overhead of monitoring and checking timestamps.
2. Kafka Streams with a state store
Kafka Streams is a client library for applications whose input and output live in Kafka. It allows finer control over delays:
- Read from the source topic in a Kafka Streams application.
- Use a transformer that puts each message, which carries its delivery timestamp, into a state store.
- Periodically (by wall-clock or stream time), pull the messages that are due out of the store and send them to the target topic.
The value is expected as payload,deliveryTimestampMillis. transform() is deprecated by KIP-820 and removed in Kafka 4.0, where the same logic goes in process() with an api.Processor.
3. External systems
For complex requirements or very long delays (days or weeks), an external system can be more efficient:
- Database or key-value store: save delayed messages with a timestamp and have a scheduled job or notifier push due messages to Kafka.
- A queue with built-in delay, such as RabbitMQ or AWS SQS: use it only for the delay, then publish the messages back into Kafka for processing.
Trade-offs to watch
- Accuracy vs resources: checking more often gives more accurate timing but uses more resources.
- Error handling: handle failures carefully, especially when messages flow from an external system back into Kafka.
- Monitoring: track delays, processing times and backlogs in every part of the delay pipeline.
Which approach fits depends on delay length, the volume of delayed messages and how precise the timing must be: a Kafka-only design for simple cases, an external service for the rest.
Related reading
- How to achieve high availability in a Kafka Streams app during deployment?
- How to achieve multi-tenancy in the context of Kafka and storm?
- How to achieve strong consistency in Kafka?
- How to acknowledge consume message in kafka using php-rdkafka?
- How to achieve strong consistency in MongoDB Replica Sets?
- How to adapt Fenwick tree to answer range minimum queries
- How to acknowledge current offset in spring kafka for manual commit
- How to add a header keyvalue pair when publishing a message with pika
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.