What is the difference between SET with NX and a plain SET in Redis?
Understand Redis SET NX versus a plain SET — conditional writes, atomicity, and expiring locks — with clear examples so you avoid race conditions in your code.
Expected Interview Answer
A plain SET always writes the key, overwriting any existing value, while SET with the NX option writes only if the key does not already exist — if the key is present, SET NX makes no change and signals failure.
SET NX (set-if-not-exists) is atomic, so it is the standard building block for locks and one-time initialization: only the first client to run it succeeds, and everyone else is rejected. Combined with an expiry such as SET key val NX EX 30, it creates a self-expiring lock in a single round trip. A plain SET is unconditional and is used when you simply want the latest value to win.
- SET NX guarantees only one writer initializes a key
- Atomic, avoiding check-then-set race conditions
- Pairs with EX/PX for self-expiring locks in one command
- Plain SET is simplest when the newest value should always win
- Returns nil on NX failure so callers can branch cleanly
AI Mentor Explanation
A plain SET is like a captain overwriting the toss decision on the team sheet no matter what was written before — the newest note always wins. SET NX is like the rule that only the first captain to claim the batting slot gets it: if the slot on the sheet is already filled, your pen is refused and the original choice stands untouched.
Step-by-Step Explanation
Step 1
Run plain SET
SET key value writes unconditionally, replacing any current value for the key.
Step 2
Add the NX flag
SET key value NX only writes when the key is absent, returning nil if it already exists.
Step 3
Understand atomicity
The existence check and the write happen as one operation, so no race window exists between clients.
Step 4
Attach an expiry
SET key value NX EX 30 makes a self-releasing lock that auto-expires after 30 seconds.
Step 5
Branch on the reply
OK means you acquired the key; nil means another client already holds it.
What Interviewer Expects
- Knowing NX means set-if-not-exists
- Understanding plain SET overwrites unconditionally
- Recognizing SET NX as atomic
- Using NX EX together for expiring locks
- Handling the nil reply on NX failure
Common Mistakes
- Thinking NX updates the value when the key exists
- Using GET then SET, creating a check-then-set race
- Forgetting an expiry, so a crashed lock holder blocks others forever
- Confusing NX with XX (set-only-if-exists)
- Assuming plain SET fails when the key already exists
Best Answer (HR Friendly)
“A plain SET always saves the new value, replacing whatever was there. SET with NX only saves if nothing is stored under that key yet — it is a way to make sure only the first person to claim something wins, which is handy for locks.”
Code Example
# Plain SET always overwrites
SET user:1 "Alice" # OK
SET user:1 "Bob" # OK, value is now Bob
# SET NX only writes if absent
SET lock:job "held" NX # OK the first time
SET lock:job "held" NX # (nil) because key exists# Acquire a lock that auto-releases after 30s
SET lock:report token123 NX EX 30
# -> OK means acquired; (nil) means someone else holds itFollow-up Questions
- How does SET NX EX help build a distributed lock?
- What is the difference between NX and XX?
- Why is SET NX safer than GET followed by SET?
- How do you safely release a lock you acquired with SET NX?
- What replaced the older SETNX command and why?
MCQ Practice
1. What does SET key value NX do when the key already exists?
NX means set-if-not-exists; if the key is present, SET NX does nothing and returns nil.
2. Which single command creates a lock that auto-releases?
SET k v NX EX 30 atomically sets the key only if absent and attaches a 30-second expiry in one round trip.
3. Why is SET NX preferred over GET-then-SET for locks?
SET NX performs the existence check and write atomically, so two clients cannot both believe they acquired the lock.
Flash Cards
What does NX stand for behaviorally? — Set-if-not-exists: write only when the key is absent.
What does plain SET do to an existing key? — Overwrites it unconditionally with the new value.
What reply does SET NX give on an existing key? — nil, indicating the write did not happen.
How do you make an expiring lock in one command? — SET key value NX EX <seconds>.
What is the opposite of NX? — XX — set only if the key already exists.