Redis ReturnBUSYKEY Target key name already exists.
When errors are usually because you are trying to overwrite oneAlready existing keys, but the current operation does not explicitly allow overwrite.
Here are the detailed explanations and solutions:
Cause of error
This error is common in two operations:
-
RESTORE
Order: Try to backup data (throughDUMP
When the generated serialized data) is restored to Redis, if the target key already exists and no overwrite option is specified. -
Other coverage scenarios: Some operations that require overriding keys (such as
COPY
、MIGRATE
) Overwrite permission is not explicitly enabled.
Solution
Select the corresponding method according to the operation type:
1. When using the RESTORE command
-
reason: By default,
RESTORE
Overwriting existing keys is not allowed. -
repair:Add to
REPLACE
Parameter, force overwrite the target key.
# grammarRESTORE key ttl serialized-value [REPLACE] # Example: Overwrite existing key `mykey`RESTORE mykey 0 \"\\x00\\x03foo\\x06\\x00\\x8f\\xd8\\xc4\\x8d\\x54\\x5d\\x2d\\x9b\" REPLACE
2. When using the COPY command
-
reason:default
COPY
The command does not allow overwriting the target key. -
repair:Add to
REPLACE
Options.
# grammarCOPY source destination [DB destination-db] [REPLACE] # Example: Copy `key1` to the current database and overwrite the key of the same nameCOPY key1 key1 REPLACE
3. When using the MIGRATE command
- reason: When migrating data to the target Redis instance, if the target key already exists and overwrite is not enabled.
-
repair:Add to
REPLACE
parameter.
# grammarMIGRATE host port key|\"\" destination-db timeout [COPY | REPLACE] # Example: Migrate the key `mykey` and overwrite the target key with the same nameMIGRATE 127.0.0.1 6379 mykey 0 5000 REPLACE
Other precautions
- Confirm whether coverage is required:
Overwrite operations permanently delete old data of the target key! Make sure this is your expected behavior.
- Check if the key really exists:
useEXISTS key
The command verifies that the key exists.
- Manually delete old keys:
If you don't want to useREPLACE
, you can manually delete the old key and then perform the operation:
DEL mykey # Delete the old keyRESTORE mykey 0 \"\\x00\\x03foo...\" # No need REPLACE
Error example analysis
Suppose that executing the following command throws an error:
# Try to restore data to an existing key `user:100`RESTORE user:100 0 \"\\x00\\x03foo\\x06\\x00\\x8f\\xd8\\xc4\\x8d\\x54\\x5d\\x2d\\x9b\"
-
Cause of error:
user:100
Already exists and not usedREPLACE
。 -
repair: Add at the end of the command
REPLACE
。
Summarize
- Core logic: Redis protects existing keys by default to prevent accidental overwriting.
-
Solution: explicitly add it in operations that require overriding keys
REPLACE
parameter. -
Preventive measures: In scripts or automation processes, check in advance whether the key exists or is enabled uniformly
REPLACE
。
The above is personal experience. I hope you can give you a reference and I hope you can support me more.