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
SET key value IFEQ comparison-value
SET key value IFNE comparison-value
keyandvalue: 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 forIFNE.
Step 1: Save the Starting Value
SET status "draft"
OK
Step 2: Change It Only If It Matches
SET status "review" IFEQ "draft"
OK
OK: status held draft, so it’s now review. Someone who still thinks it’s a draft tries to publish it:
SET status "published" IFEQ "draft"
(nil)
(nil): the value isn’t draft any more, so nothing changed. With the value Valkey really holds, it works:
SET status "published" IFEQ "review"
OK
Step 3: Refuse a Change with IFNE
SET status "draft" IFNE "published"
(nil)
IFNE writes only when the current value isn’t the one you give. status is published, so it stays that way:
GET status
"published"

Clean Up
DEL status
(integer) 1
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading the value with GET and then saving with SET | Another client can change it between the two commands, and its change is lost | Use SET … IFEQ with the value you read |
Mixing up IFEQ and IFNE | The write happens in the opposite case | IFEQ: only if equal. IFNE: only if not equal |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
(error) ERR syntax error on a SET … IFNE | Your Valkey is older than 9.2 | Upgrade 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.
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