-1

I have a method which takes input as list and store each item in Azure Redis cache.

public async Task<bool> StoreAsBatch<T>(T data)
        {
            var storeData = new List<Task<bool>>();
            try
            {
                foreach (var each in data as List<EmpUser>)
                {
                    storeData.Add(Store(each.UserId, each));
                }

                await Task.WhenAll(storeData).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError($"StoreAsBatch failed with an exception - {ex.Message} ");
                return false;
            }
            
            return true;
        }

Here is the Store method

public async Task<bool> Store<T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentException(nameof(key));
            key = $"emp_user_{key}";
            string val = JsonConvert.SerializeObject(value);
            return await _database.StringSetAsync(key, JsonConvert.SerializeObject(value), new TimeSpan(30, 0, 0, 0,0));
        }

When I am passing the list(list size: 1k records) to the above StoreAsBatch method. I am getting exception like this Error

Timeout awaiting response (outbound=0KiB, inbound=0KiB, 7625ms elapsed, timeout is 5000ms), command=SETEX, next: SETEX emp_user_00mb1, inst: 0, qu: 0, qs: 1, aw: True, rs: DequeueResult, ws: Writing, in: 0, serverEndpoint: mrcooper-originations-boss-dev.redis.cache.windows.net:6380, mc: 1/1/0, mgr: 9 of 10 available, clientName: WIN10H-DLPIH45D, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=2,Free=32765,Min=8,Max=32767), v: 2.1.58.34321 (Please take a look at this article for some common client-side issues that can cause timeout) 

I am new to Azure Redis cache. Please help me to resolve the error.

Thanks in advance.

akhil
  • 1,649
  • 3
  • 19
  • 31
  • 1
    The error tells you what's going on. You are trying to write 1k items, it takes your application 7.6 seconds to do so, and you configured Redis to timeout after 5 seconds. Either increase the timeout threshold or don't store a JSON with 1k items in it – Camilo Terevinto Oct 17 '20 at 09:08
  • @CamiloTerevinto, where can I increase timeout. Can you please help me. is it should be done from code side? – akhil Oct 17 '20 at 09:10
  • 1
    The documentation is your friend: https://stackexchange.github.io/StackExchange.Redis/Configuration – Camilo Terevinto Oct 17 '20 at 09:16

1 Answers1

0

Based on @CamiloTervinto suggestions and refering this Redis Configuration Documentation.

I am able to solve my error by increasing threshold time for async calls while settingup redis cache.

Here is the Reference code.

Method 1

public void SetUpCacheManager()
        {
            try
            {
                _lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
                { 
                    string connectionString = _config.AzureRedis.HostName + _config.AzureRedis.Password + _config.AzureRedis.Ssl;
                    var redisConfig = ConfigurationOptions.Parse(connectionString);
                    redisConfig.AsyncTimeout = 15000;
                    return ConnectionMultiplexer.Connect(redisConfig);
                });

                _database = Connection.GetDatabase();
            }
            catch(Exception e)
            {
                _logger.LogError($"(SetUpCacheManager): Exception in Connecting to Cache, Message: {e.Message}");
            }  
        }

Method 2

        public async Task<bool> StoreAsBatch(List<EncompassUser> data)
        {
            Parallel.ForEach(data, new ParallelOptions{MaxDegreeOfParallelism =  10 }, (obj) =>
            {
                    try
                    {
                        Store(obj.UserId, obj);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"StoreAsBatch failed with an exception - {ex.Message} ");
                    }
            });
            return true;
        }
akhil
  • 1,649
  • 3
  • 19
  • 31