These commands change the name of a key in the database. It helps you organize or update your data by giving keys new names.
RENAME and RENAMENX in Redis
RENAME oldkey newkey RENAMENX oldkey newkey
RENAME changes the key name and overwrites the new key if it exists.
RENAMENX changes the key name only if the new key does not exist; otherwise, it does nothing.
user:1 to user:100. If user:100 exists, it will be overwritten.RENAME user:1 user:100
user:1 to user:100 only if user:100 does not exist. If it exists, the command does nothing.RENAMENX user:1 user:100
This example shows how RENAME overwrites the key user:100 with user:1's value. Then it resets user:1 and tries RENAMENX, which does not rename because user:100 exists.
SET user:1 "Alice" SET user:100 "Bob" RENAME user:1 user:100 GET user:100 SET user:1 "Alice" RENAMENX user:1 user:100 GET user:100 GET user:1
If the old key does not exist, both commands return an error.
RENAME can overwrite existing keys, so use it carefully.
RENAMENX returns 1 if the rename was successful, 0 if not.
RENAME changes a key's name and overwrites if needed.
RENAMENX changes a key's name only if the new name is free.
Use these commands to organize or update your Redis keys safely.
Practice
RENAME do?Solution
Step 1: Understand the purpose of RENAME
TheRENAMEcommand changes the name of an existing key to a new name.Step 2: Check behavior when new key exists
If the new key already exists,RENAMEoverwrites it without error.Final Answer:
Changes the name of a key and overwrites the new key if it exists -> Option AQuick Check:
RENAME overwrites existing keys = B [OK]
- Thinking RENAME only renames if new key is free
- Confusing RENAME with RENAMENX
- Assuming RENAME copies instead of renaming
oldkey to newkey using RENAMENX?Solution
Step 1: Recall RENAMENX syntax
The correct syntax isRENAMENX oldkey newkey, where the first argument is the current key and the second is the new key.Step 2: Identify incorrect options
A reverses arguments, B uses RENAME not RENAMENX, D has a typo in command name.Final Answer:
RENAMENX oldkey newkey -> Option CQuick Check:
RENAMENX oldkey newkey syntax = C [OK]
- Swapping oldkey and newkey arguments
- Using RENAME instead of RENAMENX
- Typo in command name
SET key1 "value1" SET key2 "value2" RENAMENX key1 key2 GET key1 GET key2
What will be the output of the two GET commands?
Solution
Step 1: Analyze RENAMENX behavior
RENAMENX renames key1 to key2 only if key2 does not exist. Here, key2 exists, so RENAMENX does nothing.Step 2: Check GET results
Since RENAMENX failed, key1 still holds "value1" and key2 holds "value2".Final Answer:
GET key1: "value1", GET key2: "value2" -> Option AQuick Check:
RENAMENX fails if new key exists = A [OK]
- Assuming RENAMENX overwrites existing keys
- Expecting key1 to be deleted after RENAMENX
- Confusing RENAMENX with RENAME
RENAMENX keyA keyB but get an error saying ERR no such key. What is the most likely cause?Solution
Step 1: Understand error meaning
The errorERR no such keymeans the source key to rename does not exist.Step 2: Identify which key is source
InRENAMENX keyA keyB,keyAis the source key. If it does not exist, the error occurs.Final Answer:
keyA does not exist -> Option DQuick Check:
ERR no such key means source key missing = D [OK]
- Thinking error means destination key missing
- Assuming RENAMENX overwrites keys
- Ignoring command spelling
session123 to user_session only if user_session does not exist, and get a confirmation if rename succeeded. Which Redis command and response should you use?Solution
Step 1: Choose command that renames only if new key is free
RENAMENXrenames only if the new key does not exist, perfect for this case.Step 2: Understand RENAMENX response
RENAMENX returns 1 if rename succeeded, 0 if new key exists and rename skipped.Final Answer:
Use RENAMENX session123 user_session and expect 1 if renamed, 0 if not -> Option BQuick Check:
RENAMENX returns 1 or 0 for success/failure = A [OK]
- Using RENAME which always overwrites
- Swapping source and destination keys
- Expecting OK response from RENAMENX
Ready to prove it?
Pass the test and this pattern moves unknown
