0

We've recently switched out in-memory cache for Azure Redis cache. It was simple enough since the API is the same. All good and well, except for a much bigger latency. I suspect it has something to do with improper caching techniques on our behalf. But since that was not a problem when we were using in-memory cache, I'm hoping this can be fixed with a Redis configuration.

Our data structure mirrors the database schema. One key for each record. For example to fetch 100 users, we query the cache 100 times. Since Redis likes to work with smaller chunks, that seems to be the way to go.

I've read about pipelining and multiplexing. Is that something that might fix this problem?

Example of how we do it:

services.AddDistributedRedisCache(action =>
{
    action.Configuration = "...";
    action.InstanceName = "...";
});

This gets called in each loop iteration.

byte[] cacheData = await Cache.GetAsync(key);

Note: The Azure Redis cache is located in the same region as our web server.

judehall
  • 884
  • 12
  • 27
  • Are you waiting for one response for one of the users to come back before you fire off the request for the next user? – James Thorpe Nov 21 '19 at 13:17
  • @JamesThorpe In this particular example, I'm fetching a list of users to display. – judehall Nov 21 '19 at 13:18
  • But how are you fetching them - you say each request is for one user, but are you waiting for that request to complete before getting the next one from the list? – James Thorpe Nov 21 '19 at 13:19
  • @JamesThorpe I'm looping through a list of keys which need to be fetched individually. I call "await Cache.GetAsync(key)" in each loop iteration. – judehall Nov 21 '19 at 13:20
  • @judehall Redis is *remote*, so every single call has to go over the network. It's always going to be a lot slower than accessing a local dictionary. Even if it's installed locally, you'll still be calling from one process into another. Don't loop like this. Find a way to retrieve multiple items in a single call – Panagiotis Kanavos Nov 21 '19 at 17:29
  • @judehall that said, Redis and any in-memory cache will be faster than reading from a remote disk or database *for lookup data*. If you use it to just dump the contents of a table, it may be *slower* than a database with proper indexes. Databases cache data in-memory too – Panagiotis Kanavos Nov 21 '19 at 17:32
  • 2
    `that was not a problem when we were using in-memory cache` in that case you were using a local collection to hold all data in the same process. Looping over the cache was bad for performance back then as well, but you wouldn't notice unless there was a lot of traffic. With Redis, you're calling a server on a different machine, so inefficient loops will cost a lot more. – Panagiotis Kanavos Nov 21 '19 at 17:35
  • @PanagiotisKanavos totally makes sense. I’m looking into multiplexing and fetching by multiple keys. Thank you sir – judehall Nov 21 '19 at 18:02

0 Answers0