@user779159
为了支持可靠的队列机制,我们采用以下方法:
- two data structures
-- Redis List (the original queue from which items are popped regularly)
-- a Redis z-set, which temporarily stores the popped item.
算法:
-- When an element is popped, we store in z-set
-- If the task that picked the item completed its job, it will delete the entry from z-set.
-- If the task couldn't complete it, the item will be hanging around in z-set. So we know, whether a task was done within expected time or not.
-- Now, another background process periodically scans this z-set, picks up items which are timedout, and then puts them back to queue
它是如何完成的:
- 我们使用 zset 来存储我们弹出的项目(通常使用 lua 脚本)。
- 我们将超时值存储为该项目的排名/分数。
- 另一个扫描程序进程将定期(比如每分钟)运行 z-set 命令 zrangebyscore,以选择(现在和最后 1 分钟)之间的项目。
- 如果上面的命令找到了项目,这意味着弹出项目的进程(通过 brpop)没有及时完成它的任务。
- 因此,第二个进程会将项目放回它最初所属的队列(redis 列表)中。