Difficulty: Intermediate
How do you atomically remove and return the member with the highest score from a Sorted Set, and how do you wait for one if the set is empty?
Use ZPOPMAX key [count]. It removes and returns the member (and its score) with the highest score in one atomic step; ZPOPMIN does the same for the lowest score. Both were added in Redis 5.0. To wait for an element when the set is empty, use the blocking versions BZPOPMAX / BZPOPMIN with a timeout, and in Redis 7.0+ ZMPOP / BZMPOP can pop from the first non-empty of several sets.
Reading the top member with ZRANGE key 0 0 REV and then deleting it with ZREM is two commands. Two workers can read the same top member before either removes it, and both process the same job. ZPOPMAX does read-and-remove as one command, so each member goes to exactly one caller.
| Command | Behaviour |
|---|---|
ZPOPMAX key [count] | Pop up to count highest-scored members |
ZPOPMIN key [count] | Pop up to count lowest-scored members |
BZPOPMAX key [key ...] timeout | Block until one of the keys has a member, then pop the highest |
BZPOPMIN key [key ...] timeout | Same for the lowest |
ZMPOP numkeys key ... MIN or MAX [COUNT n] | Pop from the first non-empty key (7.0+) |
BZMPOP timeout numkeys key ... MIN or MAX [COUNT n] | Blocking version (7.0+) |
The timeout is in seconds and can be fractional since Redis 6.0; 0 means wait forever. Non-blocking pops on an empty or missing key return an empty array. Complexity is O(log N) per popped member.
BZPOPMAX jobs:priority 5 to always take the most urgent job first, for example premium-customer refunds before regular ones.ZPOPMAX leaderboard:daily 3 when awarding the day's top three and resetting.ZPOPMIN gives the job that is due soonest.ZMPOP.ZPOPMIN pops the smallest timestamp even if it is in the future. For "due now" semantics, check the score and put it back, or use a Lua script that pops only when score <= now.ZPOPMAX takes the lexicographically greatest), so add a tiebreaker into the score if order matters.BZPOPMAX and ZMPOP need all keys in the same slot.ZADD jobs:priority 40 refund:402 90 refund:771 10 email:55
ZPOPMAX jobs:priority # highest
ZPOPMIN jobs:priority # lowest
ZPOPMAX jobs:priority 5 # up to 5 more
# Block up to 2.5 seconds on two queues (checked in order)
BZPOPMAX jobs:urgent jobs:priority 2.5
# Redis 7.0+: pop 2 from the first non-empty queue
ZMPOP 2 jobs:urgent jobs:priority MAX COUNT 2
After three pops the set is empty, so the blocking call times out with nil and ZMPOP also returns nil.
import Redis from 'ioredis';
// Use a dedicated connection: blocking commands hold it
const redis = new Redis();
async function workLoop() {
for (;;) {
const res = await redis.bzpopmax('jobs:priority', 5);
if (!res) continue; // timed out, loop again
const [, jobId, score] = res;
console.log(`Processing ${jobId} (priority ${score})`);
// await handle(jobId);
}
}
workLoop();
BZPOPMAX returns [key, member, score]. A short timeout lets the loop check for shutdown signals instead of blocking forever.
ZPOPMAX, ZPOPMIN, BZPOPMAX, ZMPOP, Priority queue