RPOPLPUSH(3valkey)
π RPOPLPUSH β the Valkey glow-up for your lists
okay bestie, welcome to the Valkey Command Manual entry for rpoplpush, and honestly? she's iconic. she pops one thing, she pushes another, she does it atomically, and she does it all in O(1). no cap.
β¨ NAME β¨
rpoplpush β Returns the last element of a list after removing and pushing it to another list. Deletes the list if the last element was popped.
like... she's a mover, a shaker, a little list-to-list relocation fairy π§
π¦ SYNOPSIS π¦
RPOPLPUSH source destination
two args, zero drama. source and destination, that's the whole vibe.
πΈ DESCRIPTION πΈ
Atomically returns and removes the last element (the tail) of the list stored at source, and pushes that element to be the first element (the head) of the list stored at destination.
okay so picture this: source is holding the list a,b,c and destination is holding the list x,y,z. you run RPOPLPUSH, and now source is holding a,b and destination is holding c,x,y,z. the c literally traveled. she moved cities. she's thriving in her new list π
If source doesn't exist, you get the value nil back and no operation is performed β nothing happens, nobody gets hurt, we just move on.
If source and destination are the same, the operation is equivalent to removing the last element from the list and pushing it as the first element of the list. so basically it's a list rotation command and she ate that.
π« Alternative
LMOVE with the RIGHT and LEFT arguments. same energy, different name, iykyk.
π¬ REPLY π¬
RESP2
one of the following, bestie:
valkey-protocol(7)Bulk string reply: the element being popped and pushed. πvalkey-protocol(7)Nil reply: if the source list is empty. π
RESP3
one of the following:
valkey-protocol(7)Bulk string reply: the element being popped and pushed. πvalkey-protocol(7)Null reply: if the source list is empty. π
(yeah they're the same vibe, RESP3 just says Null instead of Nil. glow-up behavior only.)
β‘ COMPLEXITY β‘
constant time. she's fast. she doesn't even break a sweat. π
π ACL CATEGORIES π
@list @slow @write
π HISTORY π
- Available since:
1.2.0β she's been around, she's a legacy icon π
π EXAMPLES π
127.0.0.1:6379> RPUSH mylist "one"
(integer) 1
127.0.0.1:6379> RPUSH mylist "two"
(integer) 2
127.0.0.1:6379> RPUSH mylist "three"
(integer) 3
127.0.0.1:6379> RPOPLPUSH mylist myotherlist
"three"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "one"
2) "two"
127.0.0.1:6379> LRANGE myotherlist 0 -1
1) "three"
see how "three" just β¨teleportedβ¨ into myotherlist? main character energy fr fr.
π Pattern: Reliable queue
Valkey is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks. A simple form of queue is often obtained pushing values into a list on the producer side, and waiting for these values on the consumer side using RPOP (using polling), or BRPOP if the client is better served by a blocking operation.
but bestie... in this context the obtained queue is not reliable π messages can be lost. for example if there's a network problem, or if the consumer crashes right after the message is received but before it can be processed. gone. poof. bye.
RPOPLPUSH (or BRPOPLPUSH for the blocking variant) offers a way to avoid this problem: the consumer fetches the message and at the same time pushes it into a processing list. It will use the LREM command in order to remove the message from the processing list once the message has been processed. π«Ά
An additional client may monitor the processing list for items that remain there for too much time, pushing timed out items back into the queue again if needed. so nothing ever gets abandoned. we don't leave items on read. π
π Pattern: Circular list
Using RPOPLPUSH with the same source and destination key, a client can visit all the elements of an N-elements list, one after the other, in O(N) β without transferring the full list from the server to the client using a single LRANGE operation. efficient queen behavior. π
The above pattern works even if one or both of the following conditions occur:
- There are multiple clients rotating the list: they'll fetch different elements, until all the elements of the list are visited, and the process restarts. π
- Other clients are actively pushing new items at the end of the list.
The above makes it very simple to implement a system where a set of items must be processed by N workers continuously as fast as possible. An example is a monitoring system that must check that a set of web sites are reachable, with the smallest delay possible, using a number of parallel workers. β
Note that this implementation of workers is trivially scalable and reliable, because even if a message is lost the item is still in the queue and will be processed at the next iteration. no message left behind, periodt. π
π SEE ALSO π
blmove(3valkey), blmpop(3valkey), blpop(3valkey), brpop(3valkey), brpoplpush(3valkey), lindex(3valkey), linsert(3valkey), llen(3valkey), lmove(3valkey), lmpop(3valkey), lpop(3valkey), lpos(3valkey), lpush(3valkey), lpushx(3valkey), lrange(3valkey), lrem(3valkey), lset(3valkey), ltrim(3valkey), rpop(3valkey), rpush(3valkey), rpushx(3valkey)
the whole list squad, basically. go say hi. πΈ
okay that's the whole page bestie, ily, go rotate some lists π
version 9.0.0 Β· dated 2025-10-21 Β· RPOPLPUSH(3valkey) β mwah, bye!! πβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms