Migrate a key name
Change a cache key after a namespace or identifier migration without rewriting its stored value.
Redis · Command
Renames a key to a new name, replacing the destination if it already exists. In Redis Cluster, both names must belong to the same hash slot.
RENAME moves an existing key from key to newkey and returns success. If the destination already exists, Redis replaces it; in clustered deployments, both names must map to the same hash slot.
RENAME key newkey| Name | Type | Description |
|---|---|---|
key | key | The key to rename. |
newkey | key | The new key name. An existing key with this name is overwritten. |
Simple string: OK on success; an error when the source key does not exist.
SET user:42 "Ada"
RENAME user:42 user:99
GET user:99Stores a value, moves the key to a new name, then reads it through the new name.
Change a cache key after a namespace or identifier migration without rewriting its stored value.
Publish a completed temporary value under its final key name.
Move a key within a cluster when both names share a hash tag and slot.
RENAME changes the name of an existing key and returns a simple string success response. If the source key does not exist, Redis returns an error.
Follow-up: What happens when the destination exists? Redis overwrites it through an implicit DEL, which can add latency for a very large value.
The command has O(1) time complexity in the usual case. However, replacing an existing destination can incur deletion work that is not represented by the nominal rename complexity.
Follow-up: How would you avoid an unconditional overwrite? Use RENAMENX, which renames only when the target name does not already exist.
In Redis Cluster, the source and destination must be assigned to the same hash slot. Keys using the same hash tag can be designed to satisfy that requirement.
Follow-up: Why does this matter? Multi-key operations cannot reliably coordinate keys placed in different cluster slots.
DELRENAMENXCOPYCOMMAND GETKEYS