0
0
Redisquery~5 mins

Time-based event queues in Redis

Choose your learning style9 modes available
Introduction

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.

Scheduling reminders or notifications to be sent later
Delaying processing of tasks until a certain time
Running periodic jobs like backups or cleanups
Handling retries of failed operations after waiting
Ordering events that must happen in a timed sequence
Syntax
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.

Examples
Adds an event to send an email at timestamp 1686000000 and fetches all events up to that time.
Redis
ZADD myqueue 1686000000 "send_email:user123"
ZRANGEBYSCORE myqueue -inf 1686000000
Adds a backup event scheduled later and fetches events up to an earlier time, which returns none.
Redis
ZADD myqueue 1686003600 "backup_db"
ZRANGEBYSCORE myqueue -inf 1686000000
Fetches due events and removes a processed event from the queue.
Redis
ZRANGEBYSCORE myqueue -inf 1686000000
ZREM myqueue "send_email:user123"
Sample Program

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.

Redis
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
OutputSuccess
Important Notes

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.

Summary

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.