SET(3valkey)
β¨ NAME β¨
okay so bestie, welcome to the Valkey Command Manual π this one is all about set β it sets the string value of a key, and it literally does not care what type that key used to be π if the key doesn't exist yet, boom, it gets created. no cap.
πΈ SYNOPSIS πΈ
SET key value [NX | XX | IFEQ comparison-value] [GET] [EX seconds | PX
milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds |
KEEPTTL]
π DESCRIPTION π
Set key to hold the string value. If key already holds a value, it gets overwritten, regardless of its type β like, no questions asked, it's giving "new phone who dis" π«Ά
And the way that any previous time to live associated with the key is discarded on a successful SET operationβ¦ yeah, gone, bestie. vanished. π
π OPTIONS π
The SET command supports a set of options that modify its behavior:
EX secondsβ set the specified expire time, in seconds (a positive integer) β³PX millisecondsβ set the specified expire time, in milliseconds (a positive integer) β‘EXAT timestamp-secondsβ set the specified Unix time at which the key will expire, in seconds (a positive integer) π°οΈPXAT timestamp-millisecondsβ set the specified Unix time at which the key will expire, in milliseconds (a positive integer) π°οΈβ¨NXβ only set the key if it does not already exist π ββοΈXXβ only set the key if it already exists βIFEQ comparison-valueβ set the key if the comparison value matches the existing value. An error is returned andSETis aborted if the value stored at key is not a string π¨KEEPTTLβ retain the time to live associated with the key π«ΆGETβ return the old string stored at key, or nil if key did not exist. An error is returned andSETis aborted if the value stored at key is not a string π
π Note: since theSETcommand options can replaceSETNX,SETEX,PSETEX,GETSET, it is possible that in future versions of Valkey these commands will be deprecated and finally removed. so likeβ¦ heads up, iykyk π«£
π REPLY π
RESP2
If GET not given, any of the following:
valkey-protocol(7)Nil reply: Operation was aborted (conflict with one of theXX/NXoptions). πvalkey-protocol(7)Simple string reply:OK: The key was set. π
If GET given, any of the following:
valkey-protocol(7)Nil reply: The key didn't exist before theSET. π«₯valkey-protocol(7)Bulk string reply: The previous value of the key. π
Note that when using GET together with XX/NX/IFEQ, the reply indirectly indicates whether the key was set:
GETandXXgiven: Non-valkey-protocol(7)Nil reply indicates the key was set. β¨GETandNXgiven:valkey-protocol(7)Nil reply indicates the key was set. β¨GETandIFEQgiven: The key was set if the reply is equal to comparison-value. β¨
RESP3
If GET not given, any of the following:
valkey-protocol(7)Null reply: Operation was aborted (conflict with one of theXX/NXoptions). πvalkey-protocol(7)Simple string reply:OK: The key was set. π
If GET given, any of the following:
valkey-protocol(7)Null reply: The key didn't exist before theSET. π«₯valkey-protocol(7)Bulk string reply: The previous value of the key. π
Note that when using GET together with XX/NX/IFEQ, the reply indirectly indicates whether the key was set:
GETandXXgiven: Non-valkey-protocol(7)Null reply indicates the key was set. β¨GETandNXgiven:valkey-protocol(7)Null reply indicates the key was set. β¨GETandIFEQgiven: The key was set if the reply is equal to comparison-value. β¨
β‘ COMPLEXITY β‘
like. constant. we love a low-effort icon π
π ACL CATEGORIES π
@slow @string @write
π¦ HISTORY π¦
- Available since:
1.0.0π± - Changed in
2.6.12: Added theEX,PX,NXandXXoptions. - Changed in
6.0.0: Added theKEEPTTLoption. - Changed in
6.2.0: Added theGET,EXATandPXAToption. - Changed in
7.0.0: Allowed theNXandGEToptions to be used together. - Changed in
8.1.0: Added theIFEQoption.
glow up after glow up fr fr π
π EXAMPLES π
Basic usage
127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> GET mykey
"Hello"
Set a value and an expiry time.
127.0.0.1:6379> SET anotherkey "will expire in a minute" EX 60
OK
Conditionally set a value.
127.0.0.1:6379> SET foo "Initial Value"
OK
127.0.0.1:6379> GET foo
"Initial Value"
127.0.0.1:6379> SET foo "New Value" IFEQ "Initial Value"
OK
127.0.0.1:6379> GET foo
"New Value"
πΈ Patterns πΈ
π Note: the following pattern is discouraged in favor of valkey-distlock(7) the Redlock algorithm which is only a bit more complex to implement, but offers better guarantees and is fault tolerant. like yeah it's more work but she's worth it bestie π«Ά
The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Valkey. π
A client can acquire the lock if the above command returns OK (or retry after some time if the command returns Nil), and remove the lock just using DEL.
The lock will be auto-released after the expire time is reached. β¨
It is possible to make this system more robust modifying the unlock schema as follows:
- Instead of setting a fixed string, set a non-guessable large random string, called token. π²
- Instead of releasing the lock with
DEL, send a script that only removes the key if the value matches. π§
This avoids that a client will try to release the lock after the expire time deleting the key created by another client that acquired the lock later. no more chaos, periodt π
An example of unlock script would be similar to the following:
if server.call("get",KEYS[1]) == ARGV[1]
then
return server.call("del",KEYS[1])
else
return 0
end
The script should be called with EVAL ...script... 1 resource-name token-value
π SEE ALSO π
valkey-delifeq(7)DELIFEQ - Deletes the key if its value matches the given string.
π SEE ALSO π
append(3valkey)decr(3valkey)decrby(3valkey)delifeq(3valkey)get(3valkey)getdel(3valkey)getex(3valkey)getrange(3valkey)getset(3valkey)incr(3valkey)incrby(3valkey)incrbyfloat(3valkey)lcs(3valkey)mget(3valkey)mset(3valkey)msetnx(3valkey)psetex(3valkey)setex(3valkey)setnx(3valkey)setrange(3valkey)strlen(3valkey)substr(3valkey)
and that's the whole page bestie!! π¦ this was 9.0.0 manual-page energy, dated 2025-10-21, and yes it ate π mwah, go set some keys, you're the main character now πβ¨
π¦ 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