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.
Multi-key operations in cluster in 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.
MGET key1 key2 key3
DEL key1 key2
MSET key1 value1 key2 value2
This sets two keys with the same tag {user} so they are in the same slot, then gets their values together.
MSET {user}:1:name "Alice" {user}:1:age "30"
MGET {user}:1:name {user}:1:ageRedis 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.
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.
Practice
Solution
Step 1: Understand Redis cluster hash slots
Redis cluster divides keys into 16384 hash slots, each handled by one node.Step 2: Multi-key commands require single node routing
Multi-key commands must operate on keys in the same slot to be routed to one node.Final Answer:
Because Redis cluster routes commands to a single node per hash slot -> Option DQuick Check:
Same hash slot = single node routing [OK]
- Thinking keys in different slots can be used together
- Assuming multi-key commands work cluster-wide automatically
- Confusing syntax errors with routing errors
Solution
Step 1: Identify key tag usage
Key tags are enclosed in curly braces {} and define the hash slot.Step 2: Check which keys share the same tag
Keys with the same substring inside {} share the same slot, e.g., {user:1}.Final Answer:
SET {user:1}:name 'Alice' and SET {user:1}:age 30 -> Option BQuick Check:
Same key tag inside {} means same slot [OK]
- Placing braces incorrectly around parts of the key
- Using different tags for keys meant to be grouped
- Omitting braces and expecting same slot
SET {order}:1001:status 'pending'
SET {order}:1001:amount 250
MGET {order}:1001:status {order}:1001:amount
What will be the output of the MGET command?Solution
Step 1: Check keys and their hash slots
Both keys have the same tag {order}, so they share the same slot.Step 2: Verify keys exist and MGET output
Both keys were set, so MGET returns their values in order: 'pending' and '250'.Final Answer:
["pending", "250"] -> Option AQuick Check:
Same slot keys return values correctly [OK]
- Expecting CROSSSLOT error despite same key tags
- Confusing string and integer values
- Assuming null for existing keys
MGET user:1:name user:2:nameBut get an error: CROSSSLOT Keys in request don't hash to the same slot. What is the best fix?
Solution
Step 1: Understand CROSSSLOT error cause
Keys user:1:name and user:2:name hash to different slots, causing the error.Step 2: Use key tags to group keys
Adding the same tag in braces, e.g., {user}, forces keys into the same slot.Final Answer:
Use key tags to put both keys in the same hash slot, e.g., {user}:1:name and {user}:2:name -> Option AQuick Check:
Key tags fix CROSSSLOT errors [OK]
- Ignoring the error and retrying same command
- Assuming prefix alone groups keys
- Trying to disable cluster mode without reason
counter:{2024}:a and counter:{2024}:b. Which approach correctly increments both counters in one command without errors?Solution
Step 1: Confirm keys share the same hash slot
Both keys have the same tag {2024}, so they are in the same slot.Step 2: Use MULTI/EXEC for atomic multi-key operations
Redis supports atomic transactions with MULTI/EXEC on keys in the same slot.Final Answer:
Use MULTI with two INCR commands on the keys, then EXEC -> Option CQuick Check:
Atomic multi-key needs MULTI/EXEC and same slot keys [OK]
- Trying to INCR multiple keys in one command
- Using keys without same hash tag
- Assuming pipeline is atomic
Ready to prove it?
Pass the test and this pattern moves unknown
