StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.84k stars 1.5k forks source link

4 connections per multiplexer created when connecting to Azure Cache For Redis Server #2685

Open kcormanmsft opened 3 months ago

kcormanmsft commented 3 months ago

Not sure if this is specifically a library issue or more of an azure redis server issue, but azure redis team suggested I file an issue here.

We noticed when we switched our apps from Microsoft.Caching.Redis (so essentially an old fork of SE.Redis) to SE.Redis that our connection count as reported by our azure redis instances increased by 33%. An important point here is that we're connecting to Azure Cache for Redis instances that have clustering enabled, but only one shard. My understanding is that this should mean each multiplexer our app creates should yield 3 connections: one discovery connection, one interactive connection, and one pubsub connection. But when I run the app below to connect to an ACR instance I actually see 4 connections from my machine when I run client list in the server console.

Configuration of the Azure Cache for Redis server is a P1 SKU with Clustering enabled and shard count of 1.

Here's the app (all code is just in this one file, and then it has a nuget dependency on Stackexchange.Redis

using StackExchange.Redis;

namespace RedisTest
{
    internal class Program
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            return ConnectionMultiplexer.Connect("<connection string redacted>");
        });

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

        public async Task WriteToCache(string key, string value)
        {
            IDatabase cache = Connection.GetDatabase();
            await cache.StringSetAsync(key, value);
        }

        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var prog = new Program();
            await prog.WriteToCache("testkey1", "val1");
            await Task.Delay(TimeSpan.FromMinutes(60));
        }
    }
}
philon-msft commented 3 months ago

Does CLIENT LIST show all four connections coming from your client machine's IP address? In a clustered cache there will be additional inter-node connections for data replication and coordination. Those will show up with a different IP from your client.