I have a requirement in which I have to do delayed scheduling in Redis. The delay can be in hours. As discussed in this answer one way of achieving this as below:-
- Add jobs to a sorted set
- Dispatcher thread which checks sorted sets every N seconds e.g -
ZRANGEBYSCORE jobs -inf, <current unix timestamp>
- Whenever some jobs are available move it to main job list where main job workers are blpop'ing.
- Delete the jobs from sorted set.
Can some one suggest some ways how can I modify the above approach to handle the below case:-
I can't afford to lose message or give duplicate jobs to main worker thread. In the above approach if the dispatcher thread dies after reading the message from sorted set, writing to main job list but before deleting the message from sorted set. Then the message will be delivered twice to the main job list?
One way I can think of is the dispatcher thread will write to an intermediate list rather than to main list. One more thread will keep on checking the intermediate list in every N seconds. Whenever some job is available move it to the main list by doing BRPOPLPUSH.
Can some one suggest some other better and optimal way?