[ACCEPTED]-Delayed execution / scheduling with Redis?-delayed-execution

Accepted answer
Score: 17

If you want to do scheduling with redis, i 7 would suggest using sorted set (the z*) commands:


what 6 you can do is something like this:

ZADD jobs <unix timestamp of when you want the job to run> <job identifier>


ZADD jobs 1291348355

Then, every 5 so often (up to every second) you can pull 4 scheduled jobs that should run (or should 3 have run by now):

ZRANGEBYSCORE jobs -inf, <current unix timestamp>

Boom, you got your jobs 2 to run. Of course, make sure to delete done 1 jobs from the sorted set.

Score: 1

You can work with a ring of multiple LISTs 10 that have a time component in their name. As 9 time component you can take the current 8 second (0-59).

You always add tasks to the 7 list for the current second. To get the 6 jobs you do a BLPOP (with low timeout) only 5 on those lists where it is guaranteed, that 4 the content is older than the given number 3 of seconds.

If you work from multiple hosts 2 you have to take care, that the clocks are 1 in sync (NTP).

Score: 1

While the @efalcao's answer is a very good 6 one, your question might indicate that redis 5 does not perfectly suit your application 4 needs. if your application has the nature 3 of a message box, please consider using 2 rabbitMQ, which features delayed messages or akka if 1 you feel bold

More Related questions