ZMOVE
ZMOVE source destination <MIN | MAX | BYSCORE score-min score-max> [COUNT count]
- Available since:
- Redis Open Source 8.12.0
- Time complexity:
- O(log(N) + K*(log(N)+log(M))) where N and M are the source and destination sizes and K is the number of moved members.
- ACL categories:
-
@write,@sortedset,@slow, - Compatibility:
- Redis Software and Redis Cloud compatibility
Atomically moves members from the sorted set at source to the sorted set at
destination, preserving their scores. Select members by lowest score (MIN),
highest score (MAX), or a score range (BYSCORE).
If a moved member already exists in destination, its score is replaced with
the source score. Other destination members are retained. Redis creates
destination if needed and deletes source when its last member is moved.
Existing key expiration times are preserved; a newly created destination has no
expiration.
When both keys name the same sorted set, the command returns null without
iterating over members. If source exists and either key holds another type,
the command returns a WRONGTYPE error before moving any members.
In Redis Open Source Cluster, both keys must be in the same hash slot.
Use a shared hash tag, such as bookmarks:{user:42} and
batch:{user:42}:001. See multi-key operations.
Use BZMOVE to wait for members to become
available, or ZMOVEMEMBER to move named
members, optionally conditioned on their presence in the destination.
Required arguments
source
The key of the sorted set to move members from.
destination
The key of the sorted set to move members to.
MIN | MAX | BYSCORE score-min score-max
MINmoves the members with the lowest scores, in ascending score order. Equal scores use ascending lexicographical member order.MAXmoves the members with the highest scores, in descending score order. Equal scores use descending lexicographical member order.BYSCORE score-min score-maxmoves members whose scores fall within the specified range. Members are moved and returned in ascending score order, with equal scores ordered lexicographically.
Without COUNT, MIN and MAX move one member, and BYSCORE moves all
matching members.
Score bounds are inclusive by default. Prefix a bound with ( to exclude it.
For example, BYSCORE -inf (1700000000 selects scores strictly less than
1700000000. Use -inf and +inf for unbounded ends of the range.
Optional arguments
COUNT count
Move at most count selected members. count must be a positive integer.
Zero and negative counts return ERR count should be greater than 0 before
any members are moved. There is no offset argument.
Examples
Move one member while preserving unrelated destination members:
redis> ZADD scores:{example} 1 one 2 two 3 three
(integer) 3
redis> ZADD moved:{example} 99 one 100 retained
(integer) 2
redis> ZMOVE scores:{example} moved:{example} MIN
1) 1) "one"
2) "1"
redis> ZMOVE scores:{example} moved:{example} MAX
1) 1) "three"
2) "3"
redis> ZRANGE moved:{example} 0 -1 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
5) "retained"
6) "100"
redis> ZMOVE scores:{example} moved:{example} MIN COUNT 0
(error) ERR count should be greater than 0
redis> ZRANGE scores:{example} 0 -1 WITHSCORES
1) "two"
2) "2"Claim a bounded batch with scores strictly less than a cutoff:
redis> ZADD bookmarks:{user:42} 1699999998 bookmark:a 1699999999 bookmark:b 1700000000 bookmark:c
(integer) 3
redis> ZMOVE bookmarks:{user:42} batch:{user:42}:001 BYSCORE -inf (1700000000 COUNT 500
1) 1) "bookmark:a"
2) "1699999998"
2) 1) "bookmark:b"
2) "1699999999"
redis> ZRANGE bookmarks:{user:42} 0 -1 WITHSCORES
1) "bookmark:c"
2) "1700000000"
redis> ZMOVE bookmarks:{user:42} batch:{user:42}:002 BYSCORE -inf (1700000000 COUNT 500
(nil)Claim a batch for processing
When scores represent update times, use BYSCORE with an exclusive upper bound
to claim older members. Selection, destination insertion, and source removal
happen in one atomic command. Other clients cannot change a selected member's
score between the range check and its removal.
Give each consumer batch a separate destination key. Persist the batch, then
delete that destination after successful processing. Writers can continue to
add or update members in source; deleting the batch does not delete those
later source updates.
ZMOVE does not acknowledge processing or automatically retry failed batches.
Your application must retain and recover unprocessed destinations. It moves
only sorted-set members and scores; values stored under other keys are not
copied or snapshotted.
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: no members
were moved because
sourcedoes not exist, no scores match, or both keys name the same sorted set.