Skip to main content
0 reactions

How to Update a Key Only If Its Value Matches in Valkey (SET IFEQ)

Featured image for How to Update a Key Only If Its Value Matches in Valkey, showing SET status with IFEQ replying OK and nil, and SET with IFNE replying nil.
Valkey SET IFEQ and IFNE.

SET with IFEQ changes a key only if it still holds the value you expect, so two people can’t overwrite each other’s change. Its opposite, IFNE, writes only when the value is different. This guide moves a document’s status from draft to review to published in Valkey®.

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 IFEQ comparison-value
SET key value IFNE comparison-value
  • key and value: the key to change and its new value.
  • IFEQ comparison-value: write only if the key holds exactly this value.
  • IFNE comparison-value: write only if the key holds anything else. A key that doesn’t exist counts as different.

From the SET documentation (opens in a new tab).

Prerequisites

  • Valkey 8.1 or later for IFEQ, and 9.2 or later for IFNE.

Step 1: Save the Starting Value

127.0.0.1:6379>Plain text
SET status "draft"
OutputPlain text
OK

Step 2: Change It Only If It Matches

127.0.0.1:6379>Plain text
SET status "review" IFEQ "draft"
OutputPlain text
OK

OK: status held draft, so it’s now review. Someone who still thinks it’s a draft tries to publish it:

127.0.0.1:6379>Plain text
SET status "published" IFEQ "draft"
OutputPlain text
(nil)

(nil): the value isn’t draft any more, so nothing changed. With the value Valkey really holds, it works:

127.0.0.1:6379>Plain text
SET status "published" IFEQ "review"
OutputPlain text
OK

Step 3: Refuse a Change with IFNE

127.0.0.1:6379>Plain text
SET status "draft" IFNE "published"
OutputPlain text
(nil)

IFNE writes only when the current value isn’t the one you give. status is published, so it stays that way:

127.0.0.1:6379>Plain text
GET status
OutputPlain text
"published"
valkey-cli session moving status from draft to review with SET IFEQ, a refused IFEQ, the change to published, and a refused SET IFNE, with GET returning published.
status moves forward only from the value it really holds.

Clean Up

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

Common Mistakes

MistakeWhat happensDo this instead
Reading the value with GET and then saving with SETAnother client can change it between the two commands, and its change is lostUse SET … IFEQ with the value you read
Mixing up IFEQ and IFNEThe write happens in the opposite caseIFEQ: only if equal. IFNE: only if not equal

Troubleshooting

ProblemCauseFix
(error) ERR syntax error on a SET … IFNEYour Valkey is older than 9.2Upgrade to Valkey 9.2 or later

FAQ

What happens if the key doesn’t exist?

With IFNE, a missing key counts as different, so Valkey creates it, as the SET documentation (opens in a new tab) says.

Can I get the old value at the same time?

Yes: add GET. The reply is the old value, and the key was changed if that value equals the one you gave to IFEQ.

Conclusion

IFEQ updates a key only when it holds the value you expect, and IFNE only when it holds something else, so changes never overwrite each other by accident.

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