How to Fix Redis CLUSTER (error) MOVED Redirection Not Working: A Deep Dive into Slot Allocation Issues


2 views

The MOVED error in Redis Cluster is a normal part of the redirection mechanism - it tells clients which node is responsible for handling a particular key. However, in your case, there appears to be a fundamental issue with your cluster configuration.

Looking at your cluster configuration:

192.168.0.14:6379 master (slots from 0 to 16383)
192.168.0.15:6379 slave (slots from 0 to 16383)
192.168.0.16:6379 master (without slots)

There are several critical issues:

  1. 192.168.0.14 claims all 16384 slots (0-16383)
  2. 192.168.0.16 has no assigned slots
  3. This creates an unbalanced cluster where one node handles all traffic

When you execute:

192.168.0.16:6379> set myKey myValue
(error) MOVED 16281 192.168.0.14:6379

The cluster is working correctly by redirecting you to the proper node. However, the command appears to fail because:

  1. The client isn't automatically following the redirect
  2. The key might not actually be stored (shown by the nil response)

Here's how to properly set up a 3-node cluster with balanced slots:

# On each node
redis-cli --cluster create \
192.168.0.14:6379 \
192.168.0.15:6379 \
192.168.0.16:6379 \
--cluster-replicas 0

This will automatically distribute slots evenly across all masters.

For production use, you should:

  1. Use a Redis client that supports cluster mode (like redis-py-cluster)
  2. Or handle MOVED/ASK responses manually

Example Python code using redis-py-cluster:

from rediscluster import RedisCluster

startup_nodes = [{"host": "192.168.0.14", "port": "6379"}]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

# This will automatically handle redirections
rc.set("myKey", "myValue")
print(rc.get("myKey"))

Always check your cluster status with:

redis-cli --cluster check 192.168.0.14:6379

This will show slot distribution and any configuration issues.

  • Redis Cluster requires proper slot distribution across masters
  • Clients must either follow redirects or use cluster-aware libraries
  • Never have a master with no slots assigned
  • Regularly check cluster health with cluster check

When working with Redis Cluster, you'll encounter the MOVED error when attempting to access keys that don't belong to the current node's slot range. The error follows this format:

(error) MOVED [slot] [destination-node-ip:port]

In your specific case, the cluster configuration shows:

192.168.0.14:6379 master (slots from 0 to 16383)
192.168.0.15:6379 slave (slots from 0 to 16383)
192.168.0.16:6379 master (without slots)

The key issue here is that node 192.168.0.16 has no assigned slots, making it unable to process any key operations directly.

When you execute:

192.168.0.16:6379> set myKey myValue
(error) MOVED 16281 192.168.0.14:6379

The cluster correctly identifies that the key's slot (16281) belongs to 192.168.0.14, but the command doesn't automatically follow the redirection because:

  • Redis CLI in standalone mode doesn't handle cluster redirections by default
  • The target node (192.168.0.14) returns (nil) because the initial SET operation never reached it

Option 1: Using Redis CLI Cluster Mode

Connect using cluster-aware client mode:

redis-cli -c -h 192.168.0.16 -p 6379

Now operations will automatically follow redirections:

192.168.0.16:6379> set myKey myValue
-> Redirected to slot [16281] located at 192.168.0.14:6379
OK
192.168.0.14:6379> get myKey
"myValue"

Option 2: Proper Cluster Configuration

Your cluster has unbalanced slot distribution. Fix this by:

redis-cli --cluster reshard 192.168.0.14:6379
# Follow prompts to move slots to 192.168.0.16

After resharding, check cluster status:

redis-cli --cluster check 192.168.0.14:6379

Option 3: Programming Client Handling

For application code, use cluster-aware clients. Example in Python:

from rediscluster import RedisCluster

startup_nodes = [{"host": "192.168.0.14", "port": "6379"}]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
rc.set("myKey", "myValue")  # Automatically handles redirections
  • Always use -c flag for cluster-aware CLI connections
  • Ensure proper slot distribution across all master nodes
  • Use cluster-enabled client libraries in production applications
  • The (nil) response indicates the initial operation never reached the correct node