-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.

3
  • 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 Commented Oct 17, 2020 at 9:08
  • @CamiloTerevinto, where can I increase timeout. Can you please help me. is it should be done from code side? Commented Oct 17, 2020 at 9:10
  • 1
    The documentation is your friend: stackexchange.github.io/StackExchange.Redis/Configuration Commented Oct 17, 2020 at 9:16

1 Answer 1

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.