Video in progress
When you add a new node to Redis Cluster and move slots, what do clients see, and how do you carry out this operation safely?
While a slot is moving, keys migrate to the new node one by one; keys that haven't moved yet are on the old node, and for the ones that have moved the client gets an ASK redirect, then MOVED once it's done. The risks: big keys blocking the migration, clients not refreshing their topology, and latency growing under load.
📖 Answer
The mechanics (inside redis-cli --cluster reshard or rebalance):
- The slot is marked
MIGRATINGon the source node andIMPORTINGon the target node. - The
MIGRATEcommand moves keys batch by batch. Moving each key is an atomic and blocking operation on the source node, so if the key is big, the node doesn't respond for that time. - If a client goes to the old node and the key has already moved, it gets
ASK: "go there this time, but don't change your map". The client sendsASKING+ the command to the new node. - Once the slot has fully moved, its owner is updated and
MOVEDis returned from then on, and the client updates its map.
If a multi-key command hits a slot in the middle of a migration (some keys there, some here), you can get a TRYAGAIN error.
What I do to make it safe:
- find big keys beforehand and split them or deal with them before the migration: a 500 MB key times out in
MIGRATEand leaves the slot half-moved (then you needredis-cli --cluster fix); - check that clients handle
MOVED/ASKcorrectly and that adaptive topology refresh is on in Lettuce; - at low-traffic times, move slowly with a small
--cluster-pipelinevalue and watch p99 latency; - the
cluster-allow-reads-when-downandcluster-require-full-coveragesettings determine what happens when something fails during the migration, so understand them in advance.
On managed services (like ElastiCache online resharding) this process is automated, but the physics is the same: big keys and a misconfigured client will still cause trouble.
An alternative strategy is to bring up a new, larger cluster alongside, make the application write to both (dual write), and then switch reads over. For a cache there's something even simpler: start the new cluster empty and shift traffic gradually with cold start protection.
Sign up to mark studied questions and save your progress.