SET saves a value under a key in Valkey®, and its options decide when it writes, what it replies and how long the key lives. This guide runs each option once on a single key, greeting.
Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.
Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.
Syntax
SET key value [NX | XX | IFEQ comparison-value | IFNE comparison-value] [GET]
[EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
key: the name to save under.value: the text or number to save. Put it in double quotes when it has spaces.NX: write only if the key doesn’t exist.XX: write only if it exists.IFEQ comparison-value: write only if the current value equals it (Valkey 8.1 and later).IFNE comparison-value: write only if it doesn’t (Valkey 9.2 and later).GET: reply with the value that was there before, instead ofOK.EX secondsorPX milliseconds: delete the key after that time.EXATandPXAT: at that Unix time.KEEPTTL: keep the key’s current expiry.- Square brackets mark an option you can leave out, and
|means “one of these”.
From the SET documentation (opens in a new tab).
Prerequisites
- Valkey 9.2 or later for
IFNEin Step 3. Every other option also works on older versions.
Step 1: Write Only When You Mean To with NX and XX
SET greeting "Hello"
OK
SET greeting "Hi" NX
(nil)
(nil) means nothing was written: greeting already exists, so NX left it alone.
SET greeting "Hi" XX
OK
XX wrote, because greeting exists.
Step 2: Get the Old Value Back with GET
SET greeting "Hey" GET
"Hi"
The new value is saved, and the reply is the value it replaced. This does the job of the older GETSET command, which the SET documentation says may be removed in a future version.
Step 3: Compare Before Writing with IFEQ and IFNE
SET greeting "Hello" IFEQ "Hey"
OK
IFEQ wrote, because greeting held Hey.
SET greeting "Hello again" IFNE "Hello"
(nil)
IFNE writes only when the current value differs from the one you give. greeting holds Hello, so nothing changed.
Step 4: Set an Expiry with EX and Keep It with KEEPTTL
SET greeting "Bye" EX 60
OK
TTL greeting
(integer) 59
EX 60 tells Valkey to delete the key after 60 seconds, and TTL shows the seconds left.
SET greeting "Bye now" KEEPTTL
OK
TTL greeting
(integer) 57
A plain SET removes a key’s expiry. With KEEPTTL, the countdown carries on.

Clean Up
DEL greeting
(integer) 1
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running a plain SET on a key that has an expiry | The expiry is removed, so the key stays forever | Add KEEPTTL |
Using NX to update a value | Nothing is written, because the key exists | Use XX or a plain SET |
FAQ
What’s the difference between NX and IFEQ?
NX checks only whether the key exists. IFEQ checks the value it holds.
Can I combine options?
Yes, one from each group in the syntax: for example NX with GET and EX.
Conclusion
NX and XX decide whether SET writes, IFEQ and IFNE compare the value first, GET returns the old value, and EX or KEEPTTL control the expiry.
Related
- Valkey Tutorial — Part 4: Strings and Counters
- Valkey Tutorial — Part 2: Your First Valkey Commands with valkey-cli
Additional Resources
Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.
0 comments