Multi-key operations in a Redis cluster let you work with several keys at once. This helps when you want to get or set many values quickly.
0
0
Multi-key operations in cluster in Redis
Introduction
You want to get values of multiple keys in one go to save time.
You need to set or update several keys together.
You want to delete many keys at once.
You want to check if multiple keys exist.
You want to perform operations on keys that are stored in the same part of the cluster.
Syntax
Redis
COMMAND key1 key2 ... keyN
All keys must be in the same hash slot for multi-key commands to work in a Redis cluster.
Use key tags (like {tag}) to force keys into the same slot.
Examples
Gets the values of key1, key2, and key3 if they are in the same hash slot.
Redis
MGET key1 key2 key3
Deletes key1 and key2 if they share the same hash slot.
Redis
DEL key1 key2
Sets key1 to value1 and key2 to value2 together, keys must be in the same slot.
Redis
MSET key1 value1 key2 value2
Sample Program
This sets two keys with the same tag {user} so they are in the same slot, then gets their values together.
Redis
MSET {user}:1:name "Alice" {user}:1:age "30"
MGET {user}:1:name {user}:1:ageOutputSuccess
Important Notes
Redis cluster splits keys into slots. Multi-key commands only work if keys share the same slot.
Use curly braces {} around part of the key to create a key tag that forces keys into the same slot.
If keys are in different slots, multi-key commands will fail with an error.
Summary
Multi-key operations let you handle many keys at once in Redis cluster.
Keys must be in the same hash slot to use multi-key commands.
Use key tags with curly braces to group keys into the same slot.