1 month ago

avoiding cache slams

Posted 1 month ago by vladyslavstartsev

hey guys! how do I avoid cache slams (https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html#cache-slams) ? this question is not about doctrine but cache in general

I need something like this

//pseudo code
// $cacheKey = 'randomCacheKey'.
if(Cache::has($cacheKey)) {
 return Cache::get($cacheKey);
//do some work
$valueToCache = $this->someComplexTask();
Cache::set($cacheKey, $valueToCache);
return $valueToCache;

the question is, how do I need to do it, to avoid cache slams? For example if I have 200 parallel requests, and all of them will notice that there is no cache, they all will try to write to same, key, with will lead to spike in cpu/memory/db queries and etc. so I need only one of them to write to this cache, and all others should wait for it. How do I do it?

this one is probably has something to do with atomic locks but it's not clear to me how do I use it, doc's for it doesn't me (it's too much "hello world"-like example).

Please sign in or create an account to participate in this conversation.