Search code examples
servicestackazure-redis-cacheservicestack.redis

Use of RedisConfig DefaultPoolSizeMultiplier property in RedisPoolManager


DefaultPoolSizeMultiplier & DefaultMaxPoolSize property of RedisConfig in ServiceStack.Redis. I didn't find any detailed documentation of RedisConfig properties. I think DefaultMaxPoolSize, determines the no. of redisClient a RedisPoolManager can have. But not sure about the use of DefaultPoolSizeMultiplier

Default Max Pool Size for Pooled Redis Client Managers (default none)
public static int? DefaultMaxPoolSize;

The default pool size multiplier if no pool size is specified (default 50)
public static int DefaultPoolSizeMultiplier = 50;
public class RedisClientManager
{
    private readonly List<RedisManagerPool> pooledClientmanagers = new List<RedisManagerPool>();
    private const int clientPoolSize = 50;
    public RedisClientManager()
    {
        RedisConfig.DefaultRetryTimeout = 5000;
        RedisConfig.BackOffMultiplier = 2;
        RedisConfig.DefaultIdleTimeOutSecs = 600;
        RedisConfig.DefaultMaxPoolSize = clientPoolSize;
        try
        {
            for (int i = 0; i < 20; ++i)
            {
                var redisManagerPool = new RedisManagerPool
                    (RedisConnectionString);
                this.pooledClientmanagers.Add(redisManagerPool);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public IRedisClient GetRedisClient()
    {
        IRedisClient client = null;
        foreach (var poolManager in pooledClientmanagers)
        {
            if ((poolManager.GetClientPoolActiveStates().Where(x => x == 0).Any()) ||
                (poolManager.GetStats().ContainsKey("clientsInUse") && Convert.ToInt32(poolManager.GetStats().GetValueOrDefault("clientsInUse")) < clientPoolSize))
            {
                client = poolManager.GetClient();
                var clientName = $"{AppDomain.CurrentDomain.FriendlyName}_{poolManager.GetId()}_{AppDomain.GetCurrentThreadId()}";
                client.SetClient(clientName);
                break;
            }
        }
        return client;
    }
}

If anyone can help me out with their usage. If I want to need 1000 active redisClient all the time which one is recommended approach:
1. One Singleton registered RedisPoolManager with DefaultMaxPoolSize as 1000(for maintainig 1000 RedisClient) (Is there any limit of DefaultMaxPoolSize?)
2. Static List of 20 RedisPoolManager with DefaultMaxPoolSize as 50(for maintainig 50 RedisClient)

mythz Could you please provide your insight..


Solution

  • You should only ever have a singleton instance of a Redis Client Manager to ensure your App is sharing from the same pooled redis client connections.

    Multiplier just means how big the pool size is relative to the configured redis hosts, e.g. for master server there's only a single instance so the multiplier just determines the pool size for redis clients connected to the master instance.

    Instead of using a multiplier you can use the Redis Configuration class to set an exact pool size for both read/write and read only client pools:

    Configure Pool Size of Redis Client Managers

    RedisConfig.DefaultMaxPoolSize = 100;