Search code examples
spring-dataamazon-elasticachespring-data-redislettuce

Redis Spring data with Lettuce: com.lambdaworks.redis.RedisCommandExecutionException: MOVED error


I'm using AWS ElastiCache (Redis) in cluster mode. I'm having two implementations to connect to ElastiCache. One of the implementations is directly using the native Lettuce driver and other using Spring data with Lettuce as underneath driver. AWS ElastiCache has Cluster configuration endpoint. I want to use this endpoint to connect to ElastiCache. I'm able to successfully connect to ElastiCache using cluster endpoint with native Lettuce driver implementation but getting below error when using spring data with cluster endpoint

Spring data: 1.8.9-RELEASE (Using a higher version of Spring is not an option)

Lettuce: 4.5.0-FINAL

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
    lettuceConnectionFactory.setHostName(<cluster_endpoint>);
    lettuceConnectionFactory.setPort();
    lettuceConnectionFactory.setUseSsl(Boolean.valueOf(useSsl));
    //lettuceConnectionFactory.setPassword(password);
    return lettuceConnectionFactory;
}

Error:

Caused by: org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is com.lambdaworks.redis.RedisCommandExecutionException: MOVED 12894 cache---.usw2.cache.amazonaws.com:6379
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:50)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:48)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)

It works fine in below two scenarios -
1) I use cluster node endpoints and inject in LettuceConnectionFactory via RedisClusterConfiguration.
2) If I use Lettuce as direct implementation (not through Spring data) using StatefulRedisClusterConnection.

What could be the reason for the above two errors? I would prefer to use Lettuce with Spring data by using cluster configuration endpoint.


Solution

  • Below is the solution for connecting to Elasticache Redis cluster using Spring Data -

    Using ElastiCache Cluster Configuration Endpoint:

    @Bean
    public RedisClusterConfiguration redisClusterConfiguration() {
      RedisClusterConfiguration clusterConfiguration = new 
      RedisClusterConfiguration();
      clusterConfiguration.clusterNode("host", port);
      new LettuceConnectionFactory(clusterConfiguration);
    }
    

    Using ElastiCache Node endpoints:

    @Bean
    public RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration()
                .clusterNode("redis-cluster----0001-001.redis-cluster---.usw2.cache.amazonaws.com",6379)
                .clusterNode("redis-cluster----0001-002.redis-cluster---.usw2.cache.amazonaws.com",6379)
                .clusterNode("redis-cluster----0001-003.redis-cluster---.usw2.cache.amazonaws.com",6379)
                .clusterNode("redis-cluster----0001-004.redis-cluster---.usw2.cache.amazonaws.com",6379);
        return clusterConfiguration;
    }
    

    Thanks to Mark Paluch who responded to my issue in Spring Data forum. Here is the detail - https://jira.spring.io/browse/DATAREDIS-898