はじめに
先日からAzure Cache for Redisのベストプラクティスを検証しています。
Azure Cache for Redisのドキュメントを見ていると、「接続の回復力に関するベストプラクティス」というドキュメントを見つけました。
以前、Spring BootアプリからAzure Cache for Redisへの接続をするためのブログ記事を載せたのですが、それをもう少し発展させてより有益な情報としていきたいと思います。
これまでのエントリーは以下のものです。
訂正
さっそくの訂正です。前回のエントリーでは、Azure Cache for Redisに接続する時に以下のようなコードを書く必要があると書きました。
package com.example.demo; // import文は省略 @Configuration @EnableRedisHttpSession public class SessionConfig extends AbstractHttpSessionApplicationInitializer { @Value("${spring.redis.host}") private String redis_host; @Value("${spring.redis.password}") private String redis_pass; @Value("${spring.redis.port}") private String redis_port; @Bean public RedisSerializer<Object> springSessionDefaultRedisSerializer() { return new GenericJackson2JsonRedisSerializer(); } @Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } @Bean public LettuceConnectionFactory connectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(this.redis_host); redisStandaloneConfiguration.setPassword(this.redis_pass); redisStandaloneConfiguration.setPort(Integer.parseInt(this.redis_port)); LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().useSsl().build(); return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration); } }
このうち、connectionFactory
は必要なく、application.properties
にspring.redis.ssl=true
を書けば良かったです。ということで、SessonConfig
クラスは以下のような実装になります。
package com.example.demo; // import文は省略 @Configuration @EnableRedisHttpSession public class SessionConfig extends AbstractHttpSessionApplicationInitializer { @Bean public RedisSerializer<Object> springSessionDefaultRedisSerializer() { return new GenericJackson2JsonRedisSerializer(); } @Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } }
Linux でホストされるクライアント アプリケーションのための TCP の設定
さて、次に書かれているのが「Linux でホストされるクライアント アプリケーションのための TCP の設定」というものですが、正直どのように実装すれば良いかわかりませんでした。
lettuceの6.1からIntroduce extended Keep-Alive options · Issue #1437 · lettuce-io/lettuce-core · GitHubでそんなに細かく実装しなくても良いのでは?と思っているのですが、Spring Data RedisやSpring Sessionとの組み合わせ方があまりピンときてなくて、とりあえず保留。
適切なタイムアウトを構成する
次は「適切なタイムアウトを構成する」です。ドキュメントにあるようにConnect timeoutとコマンドタイムアウトの二種類の設定をするのが良さそうです。Spring Bootには、以下のプロパティ一覧を見るとそれぞれspring.redis.connect-timeout
とspring.redis.timeout
に該当するようです。
デフォルト値が書いてなく、単位もよくわからない...
Lettuce 6.1.5.RELEASEのAPIドキュメントを見るとDEFAULT_CONNECT_TIMEOUT
は10L
でDEFAULT_TIMEOUT
は60L
みたい。
Constant Field Values (Lettuce 6.1.5.RELEASE API)
Spring BootのAutoconfigureのテストでも正しそうです。
ただ、単位があまりよくわからず。秒なのかミリ秒なのか...
だいぶ中途半端な感じですが、今日はここまでということで。