Search code examples
redisspring-data-redis

Pop (LPOP, RPOP) multiple keys with one command


I have multiple small queues inside Redis and I want to poll them at a fixed period of time. At the moment I have multiple keys in Redis where each stores a list of strings.

Is there a way to run pop operations (LPOP, RPOP) on multiple keys at the same time?

Let's suppose that have 3 keys:
key1 -> ["a1","b1","c1"]
key2 -> ["a2","b2","c2"]
key3 -> ["a3","b3","c3"]

Can I run an RPOP operation (or something similar) in such a way that I'll pop "c1","c2" and "c3".


If it's possible, can I run the same command without specifying the keys? like to run RPOP for all the keys that are stored in Redis.


Solution

  • Can I run an RPOP operation (or something similar) in such a way that I'll pop "c1","c2" and "c3".

    You can use Lua script to achieve the goal:

    eval 'local items = {}; for i, k in ipairs(KEYS) do items[#items+1] = redis.call("lpop", k) end; return items' 2 key1 key2
    

    In fact, there're builtin commands for popping items from multiple keys, however these commands only return items from the first non-empty list. If that's OK, you can take a look: BLPOP, BRPOP, LMPOP(This is a new command since Redis 7.0.0).

    can I run the same command without specifying the keys? like to run RPOP for all the keys that are stored in Redis.

    You can modify the Lua scripts above to achieve the goal. However, it's not recommended to do that. Since iterating all keys might block Redis. That's a bad idea on Production environment.