且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法连接到 redis 服务器;连接超时

更新时间:2021-11-16 22:29:00

出现错误的原因有很多.以下是我能想到的一些(不按任何特定顺序):

There are many different causes of the error you are getting. Here are some I can think of off the top of my head (not in any particular order):

  1. 您的 connectTimeout 太小.我经常看到客户经常设置一个小的连接超时,因为他们认为这将确保在该时间跨度内建立连接.这种方法的问题在于,当出现问题(高客户端 CPU、高服务器 CPU 等)时,连接尝试将失败.这通常会使糟糕的情况变得更糟——它不但没有帮助,反而通过强制系统重新启动尝试重新连接的过程来加剧问题,通常会导致 connect -> fail -> retry 循环.我通常建议您将 connectionTimeout 保留在 15 秒或更高.***让您的连接尝试在 15 或 20 秒后成功,而不是在 5 秒后反复尝试失败,从而导致持续几分钟的中断,直到系统最终恢复.

  1. Your connectTimeout is too small. I often see customers set a small connect timeout often because they think it will ensure that the connection is established within that time span. The problem with this approach is that when something goes wrong (high client CPU, high server CPU, etc), then the connection attempt will fail. This often makes a bad situation worse - instead of helping, it aggravates the problem by forcing the system to restart the process of trying to reconnect, often resulting in a connect -> fail -> retry loop. I generally recommend that you leave your connectionTimeout at 15 seconds or higher. It is better to let your connection attempt succeed after 15 or 20 seconds than it is to have it fail after 5 seconds repeatedly, resulting in an outage lasting several minutes until the system finally recovers.

发生服务器端故障转移.由于某种类型的从主服务器到副本服务器的故障转移,连接被服务器切断.如果服务器端软件在 Redis 层、操作系统层或托管层更新,就会发生这种情况.

A server-side failover occurs. A connection is severed by the server as a result of some type of failover from master to replica. This can happen if the server-side software is updated at the Redis layer, the OS layer or the hosting layer.

某种类型的网络基础设施故障(位于客户端和服务器之间的硬件出现某种类型的问题).

A networking infrastructure failure of some type (hardware sitting between the client and the server sees some type of issue).

您更改了 Redis 实例的访问密码.更改密码将重置与所有客户端的连接,以强制它们重新进行身份验证.

You change the access password for your Redis instance. Changing the password will reset connections to all clients to force them to re-authenticate.

需要调整线程池设置.如果您的线程池设置没有针对您的工作负载正确调整,那么您可能会在启动新线程时遇到延迟,如 在这里解释.

Thread Pool Settings need to be adjusted. If your thread pool settings are not adjusted correctly for your workload, then you can run into delays in spinning up new threads as explained here.

我写了一堆 Redis ***实践,它们也将帮助您避免其他问题.

I have written a bunch of best practices for Redis that will help you avoid other problems as well.