====== Lettuce - Redis Java Client ====== * [[nosql:redis|Redis]] Java Client * https://lettuce.io/ * non-blocking 으로 성능이 매우 좋다. ===== Replica ===== * [[https://blog.leocat.kr/notes/2021/08/10/redis-read-from-replica|[Redis] Master 이외의 Replica(slave)로 부터 읽기]] * spring-data-redis 를 사용하고, AWS [[aws:elasticcache|AWS ElasticCache]] 의 replica 를 사용하는 경우에는 ''org.springframework.data.redis.connection.RedisStaticMasterReplicaConfiguration'' 를 서버 설정으로 사용해서 모든 replica 를 지정해줘야 한다. * AWS ElasticCache 가 아니고 ''INFO'' 명령으로 제대로 replica 정보를 알 수 있는 경우에는 그냥 master 주소만 지정해서 ''RedisStandaloneConfiguration'' 를 사용해도 되는 듯. * ''ReadFrom'' 의 설정값이 ''ANY'' 를 제외하고는 특정 순서를 따르게 돼 있는데, 이로 인해 오히려 부하 분산이 제대로 안된다. 다음과 같은 형태로 별도의 ''ReadFrom'' 구현체를 사용해야 random 부하 분산이 된다(Lettuce 5.2 이상?). public class ReadFromReplicaUnOrderSensitive extends ReadFrom { @Override public List select(Nodes nodes) { return ReadFrom.REPLICA.select(nodes); // 그냥 REPLICA_PREFERRED 가 나아보이기도 하고? } /** * default 값이 기본 {@code false} 이긴 하지만 혹시나 모를 코드 변경에 대비하여 명시적 override. * @see ReadFrom#isOrderSensitive() */ @Override protected boolean isOrderSensitive() { return false; } } * AWS Elastic Cache 의 경우 Read Only endpoint 를 따로 지원해주지만, 이 Read Only Endpoint 가 제대로 분산되어 클라이언트들에게 배분되지 않는 경우도 많이 발생함. 제일 분산률을 높게 하려면 replica 서버를 각자 따로 지정해야함. 다만, 애플리케이션 서버 댓수가 많다면 굳이 이렇게 까지 하지 않아도 분산률이 상당히 높게 됨. ===== Redis Cluster ===== * [[nosql:redis:redis_cluster|Redis Cluster]] 참조. * [[https://lettuce.io/core/5.3.7.RELEASE/reference/index.html#clientoptions.cluster-specific-options|Cluster Specific options]] * ''readFrom'' 설정을 할 것. 그래야 replica 에서 데이터를 읽는다. * https://lettuce.io/core/release/api/io/lettuce/core/ReadFrom.html * ''enablePeriodicRefresh'' 설정. 10초? * ''enableAllAdaptiveRefreshTriggers'' : on, 이 경우 ''MOVED'', ''ACK'' 등 지나치게 이벤트가 많이 발생하는 경우 성능저하가 있을 수 있음. ===== Client Side Caching ===== * [[https://lettuce.io/core/release/api/io/lettuce/core/support/caching/ClientSideCaching.html|ClientSideCaching]] 참조 * [[https://github.com/lettuce-io/lettuce-core/issues/1281|Add support for Client-side caching · Issue #1281 · lettuce-io/lettuce-core]] * [[https://github.com/lettuce-io/lettuce-core/issues/2100|Client-side caching clarifications · Issue #2100 · lettuce-io/lettuce-core]] ===== Bean 종료 안되는 문제 ===== * ''6.06'' 버전 이전의 경우 ''LettuceConnectionFactory'' Spring Bean 이 올바로 종료 안되는 문제 있음. * 이로 인해서 Spring Batch 등의 애플리케이션이 종료가 안돼서 무한 대기에 빠짐. * 라이브러리 버전업 해야함. * [[https://github.com/lettuce-io/lettuce-core/issues/1768|AbstractRedisClient#shutdown() never ends when Redis is unstable · Issue #1768 · lettuce-io/lettuce-core · GitHub]] ===== 참조 ===== * [[https://lettuce.io/core/release/reference/|Lettuce 문서]] * [[https://www.baeldung.com/java-redis-lettuce|Introduction to Lettuce - the Java Redis Client | Baeldung]] * [[https://gist.github.com/warrenzhu25/1beb02a09b6afd41dff2c27c53918ce7|Azure java redis best practice]] * [[https://jojoldu.tistory.com/418|Jedis 보다 Lettuce 를 쓰자]]