Time-based event queues help you manage tasks or events that need to happen at specific times. They keep events in order by when they should run.
Time-based event queues in Redis
ZADD queue_name timestamp event_data ZRANGEBYSCORE queue_name -inf current_time ZREM queue_name event_data
ZADD adds an event with a score (timestamp) to a sorted set.
ZRANGEBYSCORE fetches events with scores up to the current time.
ZADD myqueue 1686000000 "send_email:user123" ZRANGEBYSCORE myqueue -inf 1686000000
ZADD myqueue 1686003600 "backup_db" ZRANGEBYSCORE myqueue -inf 1686000000
ZRANGEBYSCORE myqueue -inf 1686000000 ZREM myqueue "send_email:user123"
This example adds two events with different times. It fetches events due at the first timestamp, removes the processed event, then fetches events due at the later timestamp.
ZADD event_queue 1686000000 "notify_user:42" ZADD event_queue 1686003600 "cleanup_temp_files" ZRANGEBYSCORE event_queue -inf 1686000000 ZREM event_queue "notify_user:42" ZRANGEBYSCORE event_queue -inf 1686003600
Time complexity for ZADD, ZREM, and ZRANGEBYSCORE is O(log N) where N is the number of events.
Space complexity depends on the number of events stored in the sorted set.
Common mistake: forgetting to remove processed events, causing repeated processing.
Use time-based queues when you need ordered, timed execution; use simple lists if order or timing is not important.
Time-based event queues use Redis sorted sets with timestamps as scores.
They let you schedule and fetch events that should happen at or before a certain time.
Always remove events after processing to avoid duplicates.