Site icon JD Bots

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

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]

From the SET documentation.

Prerequisites

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

127.0.0.1:6379>

SET greeting "Hello"

Output

OK

127.0.0.1:6379>

SET greeting "Hi" NX

Output

(nil)

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

127.0.0.1:6379>

SET greeting "Hi" XX

Output

OK

XX wrote, because greeting exists.

Step 2: Get the Old Value Back with GET

127.0.0.1:6379>

SET greeting "Hey" GET

Output

"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>

SET greeting "Hello" IFEQ "Hey"

Output

OK

IFEQ wrote, because greeting held Hey.

127.0.0.1:6379>

SET greeting "Hello again" IFNE "Hello"

Output

(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>

SET greeting "Bye" EX 60

Output

OK

127.0.0.1:6379>

TTL greeting

Output

(integer) 59

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

127.0.0.1:6379>

SET greeting "Bye now" KEEPTTL

Output

OK

127.0.0.1:6379>

TTL greeting

Output

(integer) 57

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

Every SET option, run on the key greeting.

Clean Up

127.0.0.1:6379>

DEL greeting

Output

(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.

Related

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.

Exit mobile version