BZMOVE
BZMOVE source destination <MIN | MAX> timeout [COUNT count]
- Available since:
- Redis Open Source 8.12.0
- Time complexity:
- O(K*(log(N)+log(M))) where K is the number of moved members and N and M are the source and destination sizes.
- ACL categories:
-
@write,@sortedset,@slow,@blocking, - Compatibility:
- Redis Software and Redis Cloud compatibility
BZMOVE is the blocking variant of ZMOVE for
moving lowest-scoring or highest-scoring members between sorted sets.
When source contains members, it behaves like ZMOVE with the same MIN or
MAX selector and optional COUNT.
When source does not exist, the connection waits until at least one member
becomes available or the timeout expires. When members become available, it
moves up to count members without waiting for a full batch. If the timeout
expires first, it returns null. Changes to destination do not unblock the
connection. BYSCORE is not supported.
Moved members retain their source scores. If a member already exists in
destination, that score is overwritten. Other destination members are
retained. Redis creates destination if needed and deletes source when its
last member is moved. Existing expiration times are preserved; a newly created
destination has no expiration.
If both arguments name the same sorted set or the same nonexistent key, the
command returns null without moving members or blocking. Inside a
MULTI/EXEC
transaction or a script, the command does not block. If no member is available,
it returns null.
A source key of the wrong type causes a WRONGTYPE error immediately.
The destination type is checked when members are available to move. If a
blocked client wakes and destination holds another type, the command returns
WRONGTYPE and leaves the source members unchanged.
In Redis Open Source Cluster, both keys must be in the same hash slot. See multi-key operations.
Required arguments
source
The key of the sorted set to move members from. This is the only key the command blocks on.
destination
The key of the sorted set to move members to.
MIN | MAX
Move the members with the lowest scores (MIN) or highest scores (MAX).
MIN returns members in ascending score order, and MAX in descending score
order. Equal scores follow the corresponding lexicographical member order.
timeout
The maximum time to block, in seconds. This is a non-negative double, so
fractional seconds are allowed. A timeout of 0 blocks indefinitely.
Optional arguments
COUNT count
Move at most count available members. The default is 1. count must be a
positive integer. Zero and negative counts return
ERR count should be greater than 0 before moving members or blocking.
Examples
Move the available members without waiting for a full batch:
redis> ZADD ready:{example} 1 first 2 second
(integer) 2
redis> BZMOVE ready:{example} processing:{example} MIN 1 COUNT 3
1) 1) "first"
2) "1"
2) 1) "second"
2) "2"
redis> BZMOVE ready:{example} processing:{example} MIN 0 COUNT 0
(error) ERR count should be greater than 0To wait for a producer, run the following command when ready:{queue} does
not exist:
BZMOVE ready:{queue} processing:{queue} MIN 0 COUNT 10
In a second connection, run ZADD ready:{queue} 1 job:1. The blocked command
returns one nested member/score pair for job:1; it does not wait for nine
more members.
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ❌ Standard |
❌ Standard |
Return information
One of the following:
- Array reply: an array of two-element member/score arrays, nested even when only one member is moved. Each score is a bulk string. Pairs follow the selected order.
- Nil reply: the timeout expired, both keys name the same sorted set, or blocking is disallowed and no member is available.