为什么天青Redis的缓存连接如此之高? [英] Why are connections to Azure Redis Cache so high?

查看:619
本文介绍了为什么天青Redis的缓存连接如此之高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Azure的Redis的缓存在高负载的场景为一台机器查询缓存。本机大致获取和设置每秒约20个项目。白天这增加,在夜间,这是少。

I am using the Azure Redis Cache in a scenario of high load for a single machine querying the cache. This machine roughly gets and sets about 20 items per second. During daytime this increases, during nighttime this is less.

至此,事情已经正常工作。今天,我意识到,连接的客户端的指标是非常高的,虽然我只有1个客户端,只是不断地获取和设置项目。这里是我的意思是度量的截图:

So far, things have been working fine. Today I realized that the metric of "Connected Clients" is extremely high, although I only have 1 client that just constantly Gets and Sets items. Here is a screenshot of the metric I mean:

我的code是这样的:

My code looks like this:

public class RedisCache<TValue> : ICache<TValue>
{
    private IDatabase cache;
    private ConnectionMultiplexer connectionMultiplexer;

    public RedisCache()
    {
        ConfigurationOptions config = new ConfigurationOptions();
        config.EndPoints.Add(GlobalConfig.Instance.GetConfig("RedisCacheUrl"));
        config.Password = GlobalConfig.Instance.GetConfig("RedisCachePassword");
        config.ConnectRetry = int.MaxValue; // retry connection if broken
        config.KeepAlive = 60; // keep connection alive (ping every minute)
        config.Ssl = true;
        config.SyncTimeout = 8000; // 8 seconds timeout for each get/set/remove operation
        config.ConnectTimeout = 20000; // 20 seconds to connect to the cache

        connectionMultiplexer = ConnectionMultiplexer.Connect(config);
        cache = connectionMultiplexer.GetDatabase();
    }

    public virtual bool Add(string key, TValue item)
    {
        return cache.StringSet(key, RawSerializationHelper.Serialize(item));
    }

我不创建该类的多个实例,因此这不是问题。也许我missunderstand连接公制和他们真正的意思是次我访问缓存的数量,但是,它也没有真正意义在我看来。任何想法,或任何具有类似的问题?

I am not creating more than one instance of this class, so this is not the problem. Maybe I missunderstand the connections metric and what they really mean is the number of times I access the cache, however, it would not really make sense in my opinion. Any ideas, or anyone with a similar problem?

推荐答案

StackExchange.Redis了比赛状态,并会导致一些情况下泄漏的连接。此问题已修复在建1.0.333或更高版本。

StackExchange.Redis had a race condition that could lead to leaked connections under some conditions. This has been fixed in build 1.0.333 or newer.

如果要确认这是你打的问题,让您的客户端应用程序的崩溃转储,看看在调试器堆中的对象。寻找大量StackExchange.Redis.ServerEndPoint对象。

If you want to confirm this is the issue you are hitting, get a crash dump of your client application and look at the objects on the heap in a debugger. Look for a large number of StackExchange.Redis.ServerEndPoint objects.

此外,一些用户不得不在自己的code一个错误,导致泄漏的连接对象。这往往是因为他们的code尝试,如果他们看到故障或断开状态,重新创建ConnectionMultiplexer对象。实在是没有必要重建ConnectionMultiplexer因为它内部的逻辑重新连接是必要的。只要确保设定的 abortConnect 的假在连接字符串中...

Also, several users have had a bugs in their code that resulted in leaked connection objects. This is often because their code is trying to re-create the ConnectionMultiplexer object if they see failures or disconnected state. There is really no need to recreate the ConnectionMultiplexer as it has logic internally to recreate the connection as necessary. Just make sure to set abortConnect to false in your connection string...

下面是我们推荐的方式:

The following is the pattern we are recommending:


        private static Lazy lazyConnection = new Lazy(() => {
            return ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
        });

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

这篇关于为什么天青Redis的缓存连接如此之高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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