extensions icon indicating copy to clipboard operation
extensions copied to clipboard

Performance spikes when using Microsoft.Extensions.Caching.StackExchangeRedis

Open aleprisecaru opened this issue 6 years ago • 7 comments

Hello, We have a cloud solution composed of multiple microservices (developed using .Net Core 3.1). We are encountering some performance spikes that occur in every service that uses the redis caching mechanism.

We have investigated this issue and it seems like the spikes are caused by the action of reading data from the AWS ElastiCache Redis cluster. We are consuming Redis through the “Microsoft.Extensions.Caching.StackExchangeRedis” nuget package, version 3.1.8 and the faulty method is RedisCache.GetAsync(string key). This method sometimes gives response times that take ~1s, generally, we have response times of ~5ms. These spikes appear pretty often, and they don’t seem to follow a particular pattern.

We used redis-cli to check the cluster to see if we have latency problems on it and all the benchmarks have shown good results. Then we were thinking it is an infrastructure problem and the DevOps department and the AWS support assured us that our infrastructure is behaving well, we don't have heavy load and there are no issues on that part.

Now, we don’t have any more ideas how to further investigate and how to solve this issue. Therefore, we are approaching you to tell us if this is a known code issue or if it is something you have heard of before? Do you think if it is something Redis specific? Does the NuGet client need some special settings we are not aware of? Do you have any suggestions on how to check if it is an infrastructure problem or not?

Steps to reproduce the behavior

  1. Using version 3.1.8 of package Microsoft.Extensions.Caching.StackExchangeRedis
  2. Initialize Redis Cache: var cacheOptions = new RedisCacheOptions { Configuration = "host:port" }; var redisCache = new RedisCache(new RedisOptions(cacheOptions));
  3. Read from redis: var result = await redisCache.GetAsync(key);
  4. Create a .net core 3.1 web service and include the logic to read from redis as part of every http request.
  5. Make several web requests (delays can be added between the requests)
  6. Check the response times of the requests

Expected behavior

Get good response times at all times, with no major performance spikes. Note: We are seeing performance spikes even when there is no heavy load.

Screenshots

This is an example of a request in which we see a performance spike. It took ~2.55 seconds to read from redis. Usually this wouldn't take more than 5ms. performanceSpikeReadFromRedisCrop

Additional context

We have also tested with different configurations, e.g. abortFail=false and different values for keepAlive in order to preserve connectivity, but the problems persist.

aleprisecaru avatar Oct 16 '20 08:10 aleprisecaru

I would also like to add that our AWS support let us known that when a performance spike occurred, there were ~5k eval based commands and eval based command latency.

According to them, the EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis, meaning that the time complexity for those commands depends on the script that is executed. High time-complexity Lua scripts are a common source of latency.

We didn't have a large number of requests in that timeframe and redis was accessed only through this nuget. As far as I know, this nuget makes the mentioned scripts, possibly through the StackExchange.Redis package.

aleprisecaru avatar Oct 16 '20 08:10 aleprisecaru

Redis monitor can show you what commands are running. Could you try that out and see what's happening during a spike?

We haven't seen this sort of behavior before, and we think we're only running EVAL when you set cache values.

BrennanConroy avatar Oct 16 '20 21:10 BrennanConroy

Hello, thanks for the suggestion, it was very helpful!

I set up monitoring and I noticed that for the reading action, an EVALSHA script is used and the HMGET command is called. This is a sample of the monitoring: RedisMonitoringResults_EvaluateScript_HMGET

This happens because for reading, a script is evaluated in the RedisExtensions class: image

Looking at the code it seems like this script is used only to get data for a key along with expiration information. I updated the code to use a method from the StackExchange.Redis nuget package directly: image

The end result is the same, but we don't use EVALSHA anymore. This is monitoring sample after this change: image

So far we didn't have as many spikes as we did before, but I will continue monitoring. From what I read EVALSHA has a greater impact on performance compared to native commands. Is there a reason we should opt for using EVAL scripts for reading instead of calling the HashGet methods instead?

If this change turns out to be effective for our use case I would like to either create an internal tool for our purposes or if you consider it appropriate, contribute to this code with the changes I've mentioned.

aleprisecaru avatar Oct 20 '20 12:10 aleprisecaru

That looks like a good change, we'd welcome a contribution. We wrote this 6 years ago and probably didn't notice that you could use the native method here.

BrennanConroy avatar Oct 20 '20 16:10 BrennanConroy

Great! I created a pull request, I hope it's alright and made the right steps. If not, I will update.

aleprisecaru avatar Oct 21 '20 13:10 aleprisecaru

We just switched back to StackExchange.redis cause the library was increasing the CPU usage by 4 times too. I am wondering what was the reason for using Lua script instead of the native calls from the library?

ZeinSleiman avatar Oct 28 '20 23:10 ZeinSleiman

Done via https://github.com/dotnet/extensions/pull/3643

BrennanConroy avatar Nov 10 '20 23:11 BrennanConroy