0
0
Redisquery~10 mins

Multi-key operations in cluster in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-key operations in cluster
Client sends multi-key command
Cluster identifies keys' hash slots
Are all keys in same slot?
NoError or Redirect
Yes
Command executed on node owning slot
Result returned to client
The cluster checks if all keys belong to the same hash slot. If yes, it executes the command on that node; otherwise, it returns an error or redirect.
Execution Sample
Redis
MSET key1 value1 key2 value2
GET key1
GET key2
Set two keys and get their values in a Redis cluster, ensuring keys are in the same hash slot.
Execution Table
StepActionKeys involvedHash SlotsResultNotes
1Client sends MSET commandkey1, key2slot 1234, slot 1234Command acceptedBoth keys hash to same slot
2Cluster routes MSET to node owning slot 1234key1, key2slot 1234Values setAtomic multi-key set succeeds
3Client sends GET key1key1slot 1234value1Single key get succeeds
4Client sends GET key2key2slot 1234value2Single key get succeeds
5Client sends MGET key1 key3key1, key3slot 1234, slot 5678Error or RedirectKeys in different slots, multi-key command fails
💡 Multi-key commands require all keys in the same hash slot; otherwise, cluster returns error or redirect.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
key1nullpending setvalue1value1value1value1
key2nullpending setvalue2value2value2value2
key3nullnullnullnullnullnull
Key Moments - 2 Insights
Why does the MGET command with keys in different slots fail?
Because Redis cluster requires all keys in multi-key commands to be in the same hash slot. The execution_table row 5 shows the error due to keys in different slots.
How does the cluster know which node to send the command to?
It calculates the hash slot for the keys and routes the command to the node owning that slot, as shown in execution_table rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the MSET command in step 2?
AError due to keys in different slots
BPartial values set
CCommand accepted and values set
DRedirect to another node
💡 Hint
See execution_table row 2 where both keys hash to the same slot and values are set.
At which step does the cluster detect keys in different slots causing an error?
AStep 5
BStep 1
CStep 3
DStep 4
💡 Hint
Check execution_table row 5 where keys key1 and key3 have different slots.
If key2 was in a different slot, what would happen to the MSET command?
AIt would partially set keys
BIt would fail with error or redirect
CIt would succeed normally
DIt would ignore key2
💡 Hint
Refer to the cluster rule in concept_flow and execution_table row 5 about multi-key commands requiring same slot.
Concept Snapshot
Multi-key operations in Redis cluster require all keys to be in the same hash slot.
Cluster routes commands to the node owning that slot.
If keys span multiple slots, commands fail or redirect.
Use hash tags { } to force keys into same slot.
Example: MSET {user1}name "Alice" {user1}age 30
Full Transcript
In Redis cluster, multi-key commands like MSET or MGET only work if all keys belong to the same hash slot. The cluster calculates the hash slot for each key. If all keys share the same slot, the command is sent to the node owning that slot and executes successfully. If keys belong to different slots, the cluster returns an error or a redirect message. This is because Redis cluster distributes keys across nodes by hash slots to scale horizontally. To ensure multi-key commands work, keys can use hash tags (curly braces) to force them into the same slot. For example, keys like {user1}name and {user1}age hash to the same slot. This visual trace showed step-by-step how commands are routed and executed, and when errors occur due to slot mismatches.