MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.35k stars 456 forks source link

very slow writing/reading into redis from asp.net app #199

Closed bigerock closed 6 years ago

bigerock commented 7 years ago

i've got redis running in our internal network. using cachemanager is causing the round trip of the page to exceed 100+ seconds vs the normal 2-5 seconds.

what should i be looking at to help debug the issue?

i'm using Ninject for DI and instantiating the ICacheManager object in a singleton scope, so i don't think it's having to re-open the connection over and over - which has been an issue with other third party systems (like Elasticsearch).

here's how i'm configuring the cache...

cacheConfig = ConfigurationBuilder.BuildConfiguration("memandrediscache", settings => { settings .WithSystemWebCacheHandle("inProcessCache") //.EnableStatistics() .And .WithRedisConfiguration(redisKey, config => config .WithAllowAdmin() .WithConnectionTimeout(1000) .WithEndpoint(endPointUrl, endPointPort) .WithPassword(password) //.WithSsl() ) .WithGzJsonSerializer() .WithJsonSerializer(jsonSer, jsonDeSer) .WithMaxRetries(10) //from 1000 .WithRetryTimeout(100) .WithRedisBackplane(redisKey) .WithRedisCacheHandle(redisKey, true); });

MichaCo commented 7 years ago

Little bit hard to tell without a sample app to reproduce it. There must be something fishy going on in your app which slows it down.

The configuration looks ok from what I can tell, except the usage of GzJson and Json serializer. I think it will use the second one, you cannot use both together.

You'd have to trace your app to figure out what takes long per request, and maybe double check in the place where you inject the instance, if it is really a singleton or not, maybe you messed up something with the DI setup, can happen ;)

bigerock commented 7 years ago

Thank you. I’ll see if I can debug it a lower-level or try to get a sample project up. Thank you for your quick response.

bigerock commented 7 years ago

sorry, i left out the code that does the actual binding ...

kernel.Bind(typeof(ICacheManager<>)).ToMethod(context => CacheFactory.FromConfiguration(context.GenericArguments[0], cacheConfig)).InSingletonScope();

ericnewton76 commented 7 years ago

Is the call into getting the cache item taking forever, or the initial connection attempt? Can you post a subset around of the lines of code that is actually freezing?

bigerock commented 7 years ago

Thanks for replying. I'm trying to get an example project together. I'm off work this week and will get it posted next week. It seems just the sheer number of calls to redis is causing the slowness. Which is why I thought it is something in the connection (not a Singleton perhaps). A few calls is fine but like a hundred and it's crazy slow.

MichaCo commented 7 years ago

It it is really just a few hundred calls then there must be something wrong in the code. You should be able to do 1000s of calls easily. Or you try to store like insane large data structures? An example would be really great to see what you are trying to do ;)

bigerock commented 7 years ago

Not very large data structures. I was able to get something reproduced with just using a date time data structure.

satinderpals commented 7 years ago

We ran into similar issue and had to rollback at the moment. Will try to contribute and create a PR.

bigerock commented 6 years ago

ok i've finally got a working example. it saves and retrieves 500 datetime values. locally its taking several seconds (closer to a full minute) and i would think that it would be much faster than that. if you could take a look and see what kind of values you're getting i'd appreciate it.

just replace your redis connection settings in the web.config

https://github.com/bigerock/cacheManTest

MichaCo commented 6 years ago

Hey @bigerock, I checked your repository and ran the website a few times and the site loads in <1sec for me, all 500 dates being the same all the time, meaning it takes less than 1 second to create those 500 keys, although you do 2 gets and some strange things on every insert. Redis server was running locally.

So, in short, I cannot reproduce the slow behavior.

What kind of redis server are you using? Which version? Is it running on your local box or somewhere else?

jzabroski commented 6 years ago

@bigerock can you try running a performance profiler like Redgate ANTS on it?

On Tue, Nov 14, 2017, 7:12 PM MichaC notifications@github.com wrote:

Hey @bigerock https://github.com/bigerock, I checked your repository and ran the website a few times and the site loads in <1sec for me, all 500 dates being the same all the time, meaning it takes less than 1 second to create those 500 keys, although you do 2 gets and some strange things on every insert. Redis server was running locally.

So, in short, I cannot reproduce the slow behavior.

What kind of redis server are you using? Which version? Is it running on your local box or somewhere else?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/MichaCo/CacheManager/issues/199#issuecomment-344443456, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbT_Wf8ik-hUUIky7jSrHIO8nSwk49dks5s2ix3gaJpZM4QBJbR .

bigerock commented 6 years ago

michaco is right. running locally it runs quickly, and yes i do see the two get operations, which was in there as a relic from the real code.

so it looks like the only issue is network latency. obviously one can't expect to run redis on the same webservers so we will make efforts to get it as close to them as possible.

thanks for looking.

MichaCo commented 6 years ago

Ok, there seem to be no problem with CacheManager. /closing