【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时

问题描述

在AKS环境中,应用使用Lettuce客户端连接Azure Redis,遇见了长达15分钟的超时情况

 

问题解答

在AKS中的节点操作系统,通常情况都是使用Linux,所以在连接Redis服务时候,TCP的连接需要基于系统级(Linux)的配置。 因为Azure Redis服务端对已经建立的连接最长空闲时间为10分钟,如果超时,Azure Redis会主动发起RST来断开TCP连接,而非正常的断开操作(四次挥手: FIN/ACK指令)。因此,因为服务器停止响应而未正常关闭连接时,Linux客户端 TCP 将继续重新传输数据包 15 分钟,然后才认为这个TCP连接死掉,直到这时,系统才会让Reids客户端重新建立新的连接。

 

Lettuce客户端 6.3.0 版本之前,并没有在底层适配TCP Keepalive 功能或是keepalive的发包间隔时间超过了10分钟,并且在Command Timeout 状态下无法直接放弃一个连接并马上重连,而是选择进入TCP重传状态等待连接恢复。

现在,Lettuce 已经在6.3.0版本中修复这个问题。通过配置TCP_USER_TIMEOUT来减少TCP重传的时间。

 

注:由于Azure Redis的升级机制或是其他意外问题(Failover),导致连接异常断开是预期的。所以必须要从Lettuce客户端入手,加强Lettuce的保活以及断开重连机制,从而将连接异常断开的影响降到最低。

 

附录一:关于Lettuce的Issue细节

请见:https://github.com/lettuce-io/lettuce-core/issues/2082#issuecomment-1702782618

 

@mp911de I verified 6.3.0.BUILD-SNAPSHOT and it succeeded. The details are as follows. I am not sure if we need to update a document to provide examples and reference it during official release. If necessary, please let me know where to add or modify it.

1. DO NOT configure TCP_USER_TIMEOUT will continue to be retransmitted.

2. Configure TCP_ USER_ TIMEOUT is 30 seconds, and a reconnection will be initiated using a new port 30 seconds after the first retransmission occurs!

 

my pom.xml is (notice: Linux need use native-epoll)

        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.3.0.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
            <version>4.1.65.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>

in debug, will see this log:

2023-09-01 21:30:20 [main] DEBUG i.l.core.resource.EpollProvider - Starting with epoll library
2023-09-01 21:30:20 [main] DEBUG i.l.c.r.DefaultEventLoopGroupProvider - Allocating executor io.netty.channel.e

参考资料

Linux 托管客户端应用程序的 TCP 设置: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-best-practices-connection#tcp-settings-for-linux-hosted-client-applications
Lettuce TCP_USER_TIMEOUT : https://github.com/lettuce-io/lettuce-core/issues/2082#issuecomment-1702782618

 

posted @ 2024-03-14 20:59  路边两盏灯  阅读(104)  评论(0编辑  收藏  举报