Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching & avoiding Cache Stampedes - multiple simultaneous calculations
    text
    copied!<p>We have a very expensive calculation that we'd like to cache. So we do something similar to:</p> <pre><code>my $result = $cache-&gt;get( $key ); unless ($result) { $result = calculate( $key ); $cache-&gt;set( $key, $result, '10 minutes' ); } return $result; </code></pre> <p>Now, during <code>calculate($key)</code>, before we store the result in the cache, several other requests come in, that also start running <code>calculate($key)</code>, and system performance suffers because many processes are all calculating the same thing.</p> <p>Idea: Lets put a flag in the cache that a value is being calculated, so the other requests just wait for that one calculation to finish, so they all use it. Something like:</p> <pre><code>my $result = $cache-&gt;get( $key ); if ($result) { while ($result =~ /Wait, \d+ is running calculate../) { sleep 0.5; $result = $cache-&gt;get( $key ); } } else { $cache-&gt;set( $key, "Wait, $$ is running calculate()", '10 minutes' ); $result = calculate( $key ); $cache-&gt;set( $key, $result, '10 minutes' ); } return $result; </code></pre> <p>Now that opens up a whole new can of worms. What if $$ dies before it sets the cache. What if, what if... All of them solvable, but since there is nothing in <a href="http://search.cpan.org/search?m=all&amp;q=cache" rel="nofollow noreferrer">CPAN</a> that does this (there is something in CPAN for <em>everything</em>), I start wondering:</p> <p>Is there a better approach? Is there a particular reason e.g. Perl's <code>Cache</code> and <code>Cache::Cache</code> classes don't provide some mechanism like this? Is there a tried and true pattern I could use instead?</p> <p>Ideal would be a CPAN module with a debian package already in squeeze or a eureka moment, where I see the error of my ways... :-)</p> <p>EDIT: I have since learned that this is called a <a href="https://en.wikipedia.org/wiki/Cache_stampede" rel="nofollow noreferrer">Cache stampede</a> and have updated the question's title.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload