0
0
Redisquery~10 mins

RENAME and RENAMENX in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RENAME and RENAMENX
Start
Check if source key exists
Yes
For RENAME:
Rename key, overwrite if target exists
Return OK
End
For RENAMENX:
Check if target key exists
No
Rename key
Return 1
End
If target exists in RENAMENX
Do not rename
Return 0
End
The flow checks if the source key exists, then for RENAME it renames and overwrites target if exists, while RENAMENX renames only if target does not exist.
Execution Sample
Redis
SET key1 "Hello"
SET key2 "World"
RENAME key1 key2
RENAMENX key2 key3
This sequence sets two keys, renames key1 to key2 (overwriting key2), then tries to rename key2 to key3 only if key3 does not exist.
Execution Table
StepCommandSource Key Exists?Target Key Exists?ActionResultState Change
1SET key1 "Hello"N/AN/ASet key1OKkey1 = "Hello"
2SET key2 "World"N/AN/ASet key2OKkey2 = "World"
3RENAME key1 key2YesYesRename key1 to key2, overwrite key2OKkey2 = "Hello", key1 deleted
4RENAMENX key2 key3YesNoRename key2 to key31key3 = "Hello", key2 deleted
💡 Commands complete; RENAMENX skips rename if target exists, RENAME always renames and overwrites.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
key1null"Hello""Hello"deleteddeleted
key2nullnull"World""Hello"deleted
key3nullnullnullnull"Hello"
Key Moments - 2 Insights
Why does RENAME overwrite the target key but RENAMENX does not?
RENAME always renames the source key to the target key, deleting the target if it exists (see step 3 in execution_table). RENAMENX only renames if the target key does not exist, otherwise it does nothing and returns 0 (see step 4).
What happens to the source key after a successful rename?
After renaming, the source key is deleted and no longer exists in the database (see variable_tracker after step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of key2 after RENAME key1 key2?
A"World"
B"Hello"
Cnull
D"key1"
💡 Hint
Check the 'State Change' column at step 3 in execution_table.
At which step does the source key get deleted?
AAfter step 3
BAfter step 1
CAfter step 2
DAfter step 4
💡 Hint
Look at variable_tracker for key1 after step 3.
If key3 already existed before step 4, what would RENAMENX return?
A1
BOK
C0
DError
💡 Hint
Refer to the concept_flow and execution_table logic for RENAMENX behavior.
Concept Snapshot
RENAME sourceKey targetKey
- Renames sourceKey to targetKey
- Overwrites targetKey if exists
- Returns OK

RENAMENX sourceKey targetKey
- Renames only if targetKey does NOT exist
- Returns 1 if renamed, 0 if not
- Source key deleted after rename
Full Transcript
This visual execution trace shows how Redis commands RENAME and RENAMENX work. First, keys are set with values. RENAME renames a source key to a target key, overwriting the target if it exists, and deletes the source key. RENAMENX renames only if the target key does not exist, otherwise it does nothing. The variable tracker shows key values after each step. Key moments clarify why RENAME overwrites but RENAMENX does not, and what happens to the source key after renaming. The quiz tests understanding of key states and command results.