The BUSYKEY error occurs when trying to use the RESTORE command to restore a key that already exists in your Redis database without using the REPLACE option. This prevents accidental data overwrites.
Redis raises the BUSYKEY error as a safety mechanism when you attempt to restore a serialized value to a key name that already exists in the database. By default, the RESTORE command does not overwrite existing keys to prevent unintended data loss. This error is distinct from other Redis key-related errors in that it specifically guards against attempting to write to an occupied key.
Use the EXISTS command to verify whether the key is already in Redis:
EXISTS mykeyIf it returns 1, the key exists. If it returns 0, the key does not exist.
To understand what data is stored in the conflicting key:
TYPE mykey
TTL mykey
GET mykeyUse GET for strings, HGETALL for hashes, LRANGE for lists, etc., based on the TYPE output. Check TTL to see if the key will expire naturally.
If you no longer need the data in the existing key, delete it before restoring:
DEL mykeyThen proceed with your RESTORE command:
RESTORE mykey 0 "<serialized-value>"The recommended solution is to use the REPLACE option (available in Redis 3.0+) to allow RESTORE to overwrite the existing key:
RESTORE mykey 0 "<serialized-value>" REPLACEThis safely overwrites the target key without additional cleanup steps.
When importing data into a Redis cluster and encountering BUSYKEY errors, use the --cluster-replace flag:
redis-cli --cluster import 127.0.0.1:6379 --cluster-from 127.0.0.1:6380 --cluster-replaceThis allows existing keys to be overwritten during the import process.
If BUSYKEY errors occur frequently in your application:
1. Check if multiple processes are writing to the same keys
2. Use RENAMENX instead of RENAME to avoid overwriting keys unintentionally
3. Add existence checks before RESTORE operations
4. Implement proper key naming schemes to avoid collisions
5. Use Lua scripts with atomic operations to ensure key safety
The BUSYKEY error is fundamentally different from ERR key exists—BUSYKEY specifically applies to the RESTORE command and cluster operations. The REPLACE option in RESTORE (Redis 3.0+) executes an implicit DEL of the existing key before restoring, which is atomic and safe. However, if the existing key contains a large value, this operation may cause brief latency spikes. For high-throughput systems, consider using RENAME or RENAMENX as alternatives when appropriate. In Lua scripts and transactions, ensure you check for key existence before attempting RESTORE operations to prevent race conditions in concurrent environments.
ERR Unbalanced XREAD list of streams
How to fix "ERR Unbalanced XREAD list" in Redis
ERR syntax error
How to fix "ERR syntax error" in Redis
ConnectionError: Error while reading from socket
ConnectionError: Error while reading from socket in redis-py
ERR unknown command
How to fix ERR unknown command in Redis
Command timed out
How to fix 'Command timed out' in ioredis