CLUSTER-SETSLOT(3valkey)
β¨ CLUSTER-SETSLOT π
okay so this is CLUSTER-SETSLOT, from the Valkey Command Manual (the 3valkey girlies know) β and bestie, she's the one who binds a hash slot to a node. that's the whole job. let's get into it π«Ά
π SYNOPSIS
CLUSTER SETSLOT slot <IMPORTING node-id | MIGRATING node-id | NODE
node-id | STABLE> [TIMEOUT timeout]
likeβ¦ yes, that's a lot of angle brackets. breathe. we'll go one by one π
πΈ DESCRIPTION
CLUSTER SETSLOT is the one responsible for changing the state of a hash slot in the receiving node, in a bunch of different ways. it's part of the legacy mechanism for cluster resharding. so like, iconic but also, respectfully, old π
Note: for live resharding, the newervalkey-atomic-slot-migration(7)mechanism usingCLUSTER MIGRATESLOTSis recommended β it's faster, more reliable, and has less impact on client applications. no cap. π§
depending on which subcommand you use, CLUSTER SETSLOT can:
MIGRATINGsubcommand: set a hash slot in migrating state.IMPORTINGsubcommand: set a hash slot in importing state.STABLEsubcommand: clear any importing / migrating state from the hash slot.NODEsubcommand: bind the hash slot to a different node.
the command + its whole little subcommand family is useful for starting and ending cluster live resharding operations β which is accomplished by setting a hash slot in migrating state in the source node, and importing state in the destination node. two nodes, one slot, a whole journey π
each subcommand gets its own section below β¬οΈ and at the end you'll find how live resharding is actually performed using this command and other related commands. sit down, get comfy, hydrate π
π¦ CLUSTER SETSLOT <slot> MIGRATING <destination-node-id>
this one sets a slot to migrating state. but to set a slot in this state, the node receiving the command must be the hash slot owner β otherwise an error is returned. periodt, no exceptions.
when a slot is in migrating state, the node changes behavior like this:
- if a command is received about an existing key, the command is processed as usual.
- if a command is received about a key that does not exist, an
ASKredirection is emitted by the node, asking the client to retry only that specific query intodestination-node. in this case the client should not update its hash slot to node mapping. - if the command contains multiple keys: if none exist, behavior is the same as point 2; if all exist, it's the same as point 1. but if only a partial number of keys exist, the command emits a
TRYAGAINerror, so that the keys in question can finish being migrated to the target node and the multi-key command can actually run. it's giving "wait your turn bestie" β
π CLUSTER SETSLOT <slot> IMPORTING <source-node-id>
this is the reverse of MIGRATING β it prepares the destination node to import keys from the specified source node. the command only works if the node is not already the owner of the specified hash slot. π
when a slot is in importing state, the node changes behavior like this:
- commands about this hash slot are refused and a
MOVEDredirection is generated as usual β except if the command follows anASKINGcommand, in which case the command is executed. π«‘
so when a node in migrating state generates an ASK redirection, the client contacts the target node, sends ASKING, and immediately after sends the command. this way, commands about non-existing keys in the old node (or keys already migrated to the target node) get executed in the target node, so that:
- new keys are always created in the target node. during a hash slot migration we only have to move old keys, not new ones. efficiency slay π
- commands about keys already migrated are correctly processed in the context of the node which is the target of the migration β the new hash slot owner β in order to guarantee consistency.
- without
ASKINGthe behavior is the same as usual. this guarantees that clients with a broken hash slots mapping will not write by mistake in the target node, creating a new version of a key that has yet to be migrated. protecting you from yourself, respectfully π‘οΈ
π CLUSTER SETSLOT <slot> STABLE
this subcommand just clears migrating / importing state from the slot. it's mainly used to fix a cluster stuck in a wrong state via valkey-cli --cluster fix. normally though, the two states get cleared automatically at the end of the migration using the SETSLOT ... NODE ... subcommand β as explained in the next section. so this is the "emergency reset button" vibe β
π CLUSTER SETSLOT <slot> NODE <node-id>
okay this one has the most complex semantics. it associates the hash slot with the specified node β but the command only works in specific situations and has different side effects depending on the slot state. here's the full set of pre-conditions and side effects:
- if the current hash slot owner is the node receiving the command, but because of the command the slot would be assigned to a different node, the command will return an error if there are still keys for that hash slot in the node receiving the command. π¨
- if the slot is in migrating state, that state gets cleared when the slot is assigned to another node.
- if the slot was in importing state in the node receiving the command, and the command assigns the slot to this node (which is what happens in the target node at the end of the resharding of a hash slot from one node to another), the command has these side effects:
- A) the importing state is cleared.
- B) if the node config epoch is not already the greatest of the cluster, it generates a new one and assigns the new config epoch to itself. this way its new hash slot ownership will win over any past configuration created by previous failovers or slot migrations. main character energy, honestly π
important: step 3 is the only time when a Valkey Cluster node will create a new config epoch without agreement from other nodes. this only happens when a manual configuration is operated. however, it is impossible for this to create a non-transient setup where two nodes have the same config epoch, since Valkey Cluster uses a config epoch collision resolution algorithm. so likeβ¦ chaotic but it's handled π
π§ Valkey Cluster live resharding explained
CLUSTER SETSLOT is an important piece used by Valkey Cluster in order to migrate all the keys contained in one hash slot from one node to another. here's how the migration is orchestrated, with the help of other commands too. we'll call the node that has the current ownership of the hash slot the source node, and the node where we want to migrate the destination node.
- set the destination node slot to importing state using
CLUSTER SETSLOT <slot> IMPORTING <source-node-id>. - set the source node slot to migrating state using
CLUSTER SETSLOT <slot> MIGRATING <destination-node-id>. - get keys from the source node with the
valkey-cluster-getkeysinslot(7)CLUSTER GETKEYSINSLOTcommand and move them into the destination node using thevalkey-migrate(7)MIGRATEcommand. - send
CLUSTER SETSLOT <slot> NODE <destination-node-id>to the destination node. - send
CLUSTER SETSLOT <slot> NODE <destination-node-id>to the source node. - send
CLUSTER SETSLOT <slot> NODE <destination-node-id>to the other primary nodes (optional).
Notes:
- the order of step 1 and 2 is important. we want the destination node to be ready to accept
ASKredirections when the source node is configured to redirect. π - the order of step 4 and 5 is important. the destination node is responsible for propagating the change to the rest of the cluster. if the source node is informed before the destination node and the destination node crashes before it's set as new slot owner, the slot is left with no owner, even after a successful failover. π do not do this.
- step 6 β sending
SETSLOTto the nodes not involved in the resharding β is not technically necessary since the configuration will eventually propagate itself. however, it's a good idea to do it anyway, in order to stop nodes from pointing to the wrong node for the hash slot that moved as soon as possible, resulting in fewer redirections to find the right node. be nice to your clients π₯Ί - starting from Valkey 8.0,
CLUSTER SETSLOTis synchronously replicated to all healthy replicas running Valkey version 8.0+. by default, this synchronous replication must complete within 2 seconds. if the replication fails, the primary does not execute the command, and the client receives aNOREPLICAS Not enough good replicas to writeerror. operators can retry the command or customize the timeout using theTIMEOUTparameter to further increase the reliability of live reconfiguration:
CLUSTER SETSLOT slot [MIGRATING|IMPORTING|NODE] node-id [TIMEOUT timeout]
here, timeout is measured in seconds, with 0 meaning to wait indefinitely. β¨
π REPLY
valkey-protocol(7) Simple string reply: all the sub-commands return OK if the command was successful. otherwise an error is returned.
β‘ COMPLEXITY
O(1) β constant time, we love a fast queen πβοΈπ¨
π ACL CATEGORIES
@admin @dangerous @slow
(yes, @dangerous. respect the warning bestie, this one rearranges your whole cluster's sense of self π₯)
π°οΈ HISTORY
- Available since: 3.0.0
- Changed in 8.0.0: Added the
TIMEOUToption.
π SEE ALSO
asking(3valkey), cluster(3valkey), cluster-addslots(3valkey), cluster-addslotsrange(3valkey), cluster-bumpepoch(3valkey), cluster-cancelslotmigrations(3valkey), cluster-count-failure-reports(3valkey), cluster-countkeysinslot(3valkey), cluster-delslots(3valkey), cluster-delslotsrange(3valkey), cluster-failover(3valkey), cluster-flushslots(3valkey), cluster-forget(3valkey), cluster-getkeysinslot(3valkey), cluster-getslotmigrations(3valkey), cluster-help(3valkey), cluster-info(3valkey), cluster-keyslot(3valkey), cluster-links(3valkey), cluster-meet(3valkey), cluster-migrateslots(3valkey), cluster-myid(3valkey), cluster-myshardid(3valkey), cluster-nodes(3valkey), cluster-replicas(3valkey), cluster-replicate(3valkey), cluster-reset(3valkey), cluster-saveconfig(3valkey), cluster-set-config-epoch(3valkey), cluster-shards(3valkey), cluster-slot-stats(3valkey), cluster-slots(3valkey), cluster-syncslots(3valkey), readonly(3valkey), readwrite(3valkey)
that's the whole page bestie, you ate it ππ
ok and for the little sign-off moment: this was from Valkey 9.0.0, dated 2025-10-21, and yes β the whole thing is the CLUSTER-SETSLOT(3valkey) manual. go forth and reshard responsibly ππβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms