1

I am caching result of a method(obviously with its signature) so that it don't make complex query on my data-store every time. My caching is working perfectly.

My question is:

How should I find the optimal value of timeout for an entry in cache?

What should be the optimal number of entry in the cache?

Are their any other variables that I can change to improve performance of my application?

Assume the various factors effecting the performance of caching as variables and get me a formula to help understand how can I optimize my cache?

Anshul Sharma
  • 327
  • 4
  • 14

1 Answers1

1

There are two hard problems in computer science: cache invalidation and naming things. First off, I'd be sure that you need a cache. It depends on what sort of datastore you're using (redis, apparently). If it were a traditional RDBMS then you'd be better off making sure that your indexing strategy was tight first. The trouble with introducing caching is that at some point, sooner rather than later, and many times thereafter, you're going to get an inconsistent cache. The cache invalidation isn't atomic with updates to your datastore, so something's going to fire an invalidate message but fail to reach it's destination, and your cache will be out of date. So be dead sure you need that caching before you introduce it. In terms of cache timeout - the sooner the better. An hour is great, a day less so. If something gets out of sync then it'll fix itself rather than causing ongoing issues. Also, if you're setting cache timeouts of a week or more then your cache is going to start operating like a datastore all of it's own; if it goes down and you have to rebuild it then you're going to take a large performance hit. So in this case less is more. Finally, make sure that you make sure and do actually set a cache timeout for everything that goes into your cache. It's all too easy with memcache to just have no expiry date by default, and in that case your cache really is going to start acting like a datastore. Don't let that happen; I've been there and waiting a week for your site to recover is not fun.

Dunk
  • 1,336
  • 12
  • 16