I think a static object is shared across multiple threads. However, I got a high CPU issue on one of my site, so I took a windbg dump and very surprised, I see this:
We can see that there are 10 instances of a class called ConnectionMultiplexer. But my code creates ConnectionMultiplexer as a static object. This should mean that only one instance be created for all the threads. So how come windbg is showing multiple instances?
This is my code to create a redis connection
public static class CacheConnection
{
private static StackExchangeRedisCacheClient _newconnectionDb;
public static StackExchangeRedisCacheClient NewConnectionDb
=> _newconnectionDb ?? (_newconnectionDb = NewRedisConnection());
private static IDatabase _connectionDb;
public static IDatabase ConnectionDb => _connectionDb ?? (_connectionDb = RedisConnection());
private static StackExchangeRedisCacheClient NewRedisConnection()
{
var serializer = new NewtonsoftSerializer();
return new StackExchangeRedisCacheClient(Connection, serializer);
}
private static IDatabase RedisConnection()
{
var cacheDatabase = Connection.GetDatabase();
return cacheDatabase;
}
public static ConnectionMultiplexer Connection => LazyConnection.Value;
private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(
System.Configuration.ConfigurationManager.AppSettings["CacheConnectionString"]), LazyThreadSafetyMode.PublicationOnly);
}