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):

  1. The slot is marked MIGRATING on the source node and IMPORTING on the target node.
  2. The MIGRATE command 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.
  3. 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 sends ASKING + the command to the new node.
  4. Once the slot has fully moved, its owner is updated and MOVED is 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 MIGRATE and leaves the slot half-moved (then you need redis-cli --cluster fix);
  • check that clients handle MOVED/ASK correctly and that adaptive topology refresh is on in Lettuce;
  • at low-traffic times, move slowly with a small --cluster-pipeline value and watch p99 latency;
  • the cluster-allow-reads-when-down and cluster-require-full-coverage settings 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.