Tuesday, August 12, 2025

Redis ClusterRedirectException Explanation : ClusterRedirectException: Redirect: slot 14865 to 10.170.15.31:6379

Redis ClusterRedirectException Explanation
Understanding Redis ClusterRedirectException in Spring & AWS

❓ The Exception

org.springframework.data.redis.ClusterRedirectException: Redirect: slot 14865 to 10.170.15.31

This error occurs when you're using a Redis Cluster (e.g. AWS ElastiCache in cluster mode), and the client tries to access a Redis slot that is managed by a different node.

🔍 What Does It Mean?

  • Redis Cluster splits the keyspace into 16,384 hash slots.
  • Each Redis node handles a subset of these slots.
  • If your client accesses a key from the wrong node, Redis returns a MOVED redirect.
  • Spring Data Redis receives this and throws a ClusterRedirectException.

📦 Example Scenario

  • You try to access a key that hashes to slot 14865.
  • Your client contacts a node that does not own that slot.
  • Redis replies with:
    MOVED 14865 10.170.15.31:6379

🚨 Why This Happens

  • Your client may not be fully cluster-aware.
  • You’re using Jedis or Spring Redis without proper cluster configuration.
  • Only a single node was configured, so slot ownership is unknown to the client.

✅ Proper Configuration (Lettuce)

Use LettuceConnectionFactory with a RedisClusterConfiguration:

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
    clusterConfig.clusterNode("10.170.15.31", 6379);
    clusterConfig.clusterNode("10.170.15.32", 6379);
    return new LettuceConnectionFactory(clusterConfig);
}
    

✅ For Jedis Users

@Bean
public JedisConnectionFactory jedisConnectionFactory() {
    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
    clusterConfig.clusterNode("10.170.15.31", 6379);
    clusterConfig.clusterNode("10.170.15.32", 6379);
    return new JedisConnectionFactory(clusterConfig);
}
    
⚠️ Do not use spring.redis.host and spring.redis.port for Redis Cluster. This connects as a standalone instance and does not support slot redirection.

🧠 Best Practices

  • Use cluster-aware clients like Lettuce or JedisCluster.
  • Configure multiple cluster nodes for failover and topology discovery.
  • Use spring.redis.cluster.nodes instead of host/port in application.properties.
spring.redis.cluster.nodes=10.170.15.31:6379,10.170.15.32:6379
spring.redis.timeout=60000
    
✅ A properly configured client will automatically follow Redis redirects and update its slot cache.
Redis Cluster Mode + Spring Data Redis · Explanation by ChatGPT · August 2025

No comments:

AWS Global vs Regional Services AWS Global vs Regional Services Category Global Services...