Skip to main content
0 reactions

Valkey SET Command Options Explained: NX, XX, GET, IFEQ and EX

Featured image for Valkey SET Command Options Explained, showing SET greeting with NX replying nil, with GET replying Hi, and with EX 60 replying OK.
Valkey SET command options.

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

SyntaxPlain text
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 of OK.
  • EX seconds or PX milliseconds: delete the key after that time. EXAT and PXAT: 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 IFNE in Step 3. Every other option also works on older versions.

Step 1: Write Only When You Mean To with NX and XX

127.0.0.1:6379>Plain text
SET greeting "Hello"
OutputPlain text
OK
127.0.0.1:6379>Plain text
SET greeting "Hi" NX
OutputPlain text
(nil)

(nil) means nothing was written: greeting already exists, so NX left it alone.

127.0.0.1:6379>Plain text
SET greeting "Hi" XX
OutputPlain text
OK

XX wrote, because greeting exists.

Step 2: Get the Old Value Back with GET

127.0.0.1:6379>Plain text
SET greeting "Hey" GET
OutputPlain text
"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

127.0.0.1:6379>Plain text
SET greeting "Hello" IFEQ "Hey"
OutputPlain text
OK

IFEQ wrote, because greeting held Hey.

127.0.0.1:6379>Plain text
SET greeting "Hello again" IFNE "Hello"
OutputPlain text
(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

127.0.0.1:6379>Plain text
SET greeting "Bye" EX 60
OutputPlain text
OK
127.0.0.1:6379>Plain text
TTL greeting
OutputPlain text
(integer) 59

EX 60 tells Valkey to delete the key after 60 seconds, and TTL shows the seconds left.

127.0.0.1:6379>Plain text
SET greeting "Bye now" KEEPTTL
OutputPlain text
OK
127.0.0.1:6379>Plain text
TTL greeting
OutputPlain text
(integer) 57

A plain SET removes a key’s expiry. With KEEPTTL, the countdown carries on.

valkey-cli session running SET greeting with NX, XX, GET, IFEQ, IFNE, EX and KEEPTTL, with each reply and the seconds left shown by TTL.
Every SET option, run on the key greeting.

Clean Up

127.0.0.1:6379>Plain text
DEL greeting
OutputPlain text
(integer) 1

Common Mistakes

MistakeWhat happensDo this instead
Running a plain SET on a key that has an expiryThe expiry is removed, so the key stays foreverAdd KEEPTTL
Using NX to update a valueNothing is written, because the key existsUse 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.

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