无法连接到 redis 服务器;连接超时 [英] It was not possible to connect to the redis server(s); ConnectTimeout

查看:154
本文介绍了无法连接到 redis 服务器;连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Azure Function V1 与 StackExchange.Redis 1.2.6 一起使用.每分钟接收 1000 条消息的功能,对于每条消息,对于每台设备,我正在检查 Redis.我注意到当我们收到更多消息时,我们会收到错误消息.

I'm using Azure Function V1 with StackExchange.Redis 1.2.6. Function receiving 1000s of messages per minutes, For every message, For every device, I'm checking Redis. I noticed When we have more messages at that time we are getting below an error.

执行函数时出现异常:TSFEventRoutingFunction 没有可用连接来服务此操作:HGET GEO_DYNAMIC_hash;无法连接到 redis 服务器;连接超时;IOCP: (Busy=1,Free=999,Min=24,Max=1000), WORKER: (Busy=47,Free=32720,Min=24,Max=32767), Local-CPU: n/a 它不是可以连接到redis服务器;连接超时

Exception while executing function: TSFEventRoutingFunction No connection is available to service this operation: HGET GEO_DYNAMIC_hash; It was not possible to connect to the redis server(s); ConnectTimeout; IOCP: (Busy=1,Free=999,Min=24,Max=1000), WORKER: (Busy=47,Free=32720,Min=24,Max=32767), Local-CPU: n/a It was not possible to connect to the redis server(s); ConnectTimeout

CacheService 由 MS 推荐

public class CacheService : ICacheService
{
    private readonly IDatabase cache;
    private static readonly string connectionString = ConfigurationManager.AppSettings["RedisConnection"];

    public CacheService()
    {
        this.cache = Connection.GetDatabase();
    }

    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect(connectionString);
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }

    public async Task<string> GetAsync(string hashKey, string ruleKey)
    {
        return await this.cache.HashGetAsync(hashKey, ruleKey);
    }
}

我正在 Azure 函数中注入 ICacheService 并在每个请求上调用 GetAsync 方法.

I'm injecting ICacheService in Azure function and calling GetAsync Method on every request.

使用 Azure Redis 实例 C3

Using Azure Redis Instance C3

目前,您可以看到我只有一个连接,创建多个连接是否有助于解决此问题?或任何其他解决/理解此问题的建议.

Currently, you can see I have a single connection, Creating multiple connections will help to solve this issue? or Any other suggestion to solve/understand this issue.

推荐答案

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

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.

这篇关于无法连接到 redis 服务器;连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆