What is a key limitation of multi-key operations in a Redis cluster?
Multi-key operations can only be performed on keys that are in the same hash slot. Keys in different slots cannot be used together in a single multi-key command.
Click to reveal answer
beginner
How does Redis cluster determine which keys belong to the same hash slot?
Redis cluster uses a hash function on the key name or a part of it (called a hash tag) to assign keys to one of 16384 hash slots. Keys with the same hash slot number are stored on the same node.
Click to reveal answer
intermediate
What is a hash tag in Redis cluster multi-key operations?
A hash tag is a substring enclosed in curly braces {} within a key name. It forces Redis to use only that substring for hashing, allowing multiple keys with the same hash tag to be in the same slot.
Click to reveal answer
intermediate
Why can't you run a command like MGET on keys in different hash slots in Redis cluster?
Because Redis cluster distributes keys across nodes by hash slots, multi-key commands require all keys to be on the same node. Different slots mean keys are on different nodes, so the command would fail.
Click to reveal answer
intermediate
How can you design keys to enable multi-key operations in Redis cluster?
Use hash tags by including the same substring in curly braces in all related keys. This ensures they hash to the same slot and can be used together in multi-key commands.
Click to reveal answer
In Redis cluster, what must be true for keys used in a multi-key command like MGET?
AKeys must be on different nodes
BKeys can be in any hash slot
CAll keys must be in the same hash slot
DKeys must have different hash tags
✗ Incorrect
Multi-key commands require all keys to be in the same hash slot so they reside on the same node.
What is the purpose of a hash tag in Redis cluster keys?
ATo encrypt the key
BTo force keys to the same hash slot
CTo make keys unique
DTo store keys on different nodes
✗ Incorrect
Hash tags force Redis to hash only the substring inside curly braces, placing keys with the same tag in the same slot.
How many hash slots does a Redis cluster have?
A16384
B8192
C1024
D32768
✗ Incorrect
Redis cluster uses 16384 hash slots to distribute keys across nodes.
What happens if you try to run a multi-key command on keys in different hash slots in Redis cluster?
AThe command succeeds normally
BThe command runs but returns partial results
CThe command merges keys automatically
DThe command fails with a CROSSSLOT error
✗ Incorrect
Redis cluster returns a CROSSSLOT error because multi-key commands require keys in the same slot.
Which of these keys will be in the same hash slot in Redis cluster?
Auser:{1000}:name and user:{1000}:email
Buser:{1000} and user:{1001}
Cuser:1000 and user:{1001}
Duser:1000 and user:1001
✗ Incorrect
Keys with the same hash tag inside curly braces hash to the same slot.
Explain why multi-key operations in Redis cluster require keys to be in the same hash slot and how hash tags help achieve this.
Think about how Redis cluster splits data and how hash tags control key placement.
You got /5 concepts.
Describe how you would design keys to support multi-key commands like MGET in a Redis cluster environment.
Focus on key naming patterns that control hash slot assignment.
You got /5 concepts.
Practice
(1/5)
1. In a Redis cluster, why must keys be in the same hash slot for multi-key operations?
easy
A. Because Redis cluster does not support multi-key commands at all
B. Because keys in different slots cause syntax errors
C. Because keys in different slots are automatically merged
D. Because Redis cluster routes commands to a single node per hash slot
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 D
Quick Check:
Same hash slot = single node routing [OK]
Hint: Keys must share hash slot for multi-key commands [OK]
Common Mistakes:
Thinking keys in different slots can be used together
Assuming multi-key commands work cluster-wide automatically
Confusing syntax errors with routing errors
2. Which of the following is the correct syntax to ensure two keys are in the same hash slot using key tags?
easy
A. SET user:1:name 'Alice'
SET user:1:age 30
B. SET {user:1}:name 'Alice'
SET {user:1}:age 30
C. SET {user1}:1:name 'Alice'
SET {user}:1:age 30
D. SET {user:1:name} 'Alice'
SET {user:1:age} 30
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 B
Quick Check:
Same key tag inside {} means same slot [OK]
Hint: Use identical curly braces content for same slot keys [OK]
Common Mistakes:
Placing braces incorrectly around parts of the key
Using different tags for keys meant to be grouped
Omitting braces and expecting same slot
3. Given these Redis commands in a cluster:
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?
medium
A. ["pending", "250"]
B. ["pending", null]
C. [null, "250"]
D. Error: CROSSSLOT Keys in request don't hash to the same slot
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 A
Quick Check:
Same slot keys return values correctly [OK]
Hint: MGET works if keys share same hash slot [OK]
Common Mistakes:
Expecting CROSSSLOT error despite same key tags
Confusing string and integer values
Assuming null for existing keys
4. You run this Redis cluster command:
MGET user:1:name user:2:name
But get an error: CROSSSLOT Keys in request don't hash to the same slot.
What is the best fix?
medium
A. Use key tags to put both keys in the same hash slot, e.g., {user}:1:name and {user}:2:name
B. Run the command on each key separately
C. Rename keys to have the same prefix without braces
D. Disable cluster mode to allow multi-key commands
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 A
Quick Check:
Key tags fix CROSSSLOT errors [OK]
Hint: Add identical key tags in braces to fix CROSSSLOT [OK]
Common Mistakes:
Ignoring the error and retrying same command
Assuming prefix alone groups keys
Trying to disable cluster mode without reason
5. You want to atomically increment two counters in a Redis cluster using a multi-key command. The counters are counter:{2024}:a and counter:{2024}:b. Which approach correctly increments both counters in one command without errors?
hard
A. Use INCR counter:2024:a and INCR counter:2024:b separately
B. Use INCR counter:{2024}:a counter:{2024}:b in one command
C. Use MULTI with two INCR commands on the keys, then EXEC
D. Use INCR counter:{2024}:a and INCR counter:{2025}:b in a pipeline
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 C
Quick Check:
Atomic multi-key needs MULTI/EXEC and same slot keys [OK]
Hint: Use MULTI/EXEC with same-slot keys for atomic multi-key ops [OK]
Common Mistakes:
Trying to INCR multiple keys in one command
Using keys without same hash tag
Assuming pipeline is atomic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.