Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:47:32+00:00 2026-06-04T11:47:32+00:00

We have a very expensive calculation that we’d like to cache. So we do

  • 0

We have a very expensive calculation that we’d like to cache. So we do something similar to:

my $result = $cache->get( $key );

unless ($result) {
    $result = calculate( $key );
    $cache->set( $key, $result, '10 minutes' );
}

return $result;

Now, during calculate($key), before we store the result in the cache, several other requests come in, that also start running calculate($key), and system performance suffers because many processes are all calculating the same thing.

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:

my $result = $cache->get( $key );

if ($result) {
    while ($result =~ /Wait, \d+ is running calculate../) {
        sleep 0.5;
        $result = $cache->get( $key );
    }
} else {
    $cache->set( $key, "Wait, $$ is running calculate()", '10 minutes' );
    $result = calculate( $key );
    $cache->set( $key, $result, '10 minutes' );
}


return $result;

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 CPAN that does this (there is something in CPAN for everything), I start wondering:

Is there a better approach? Is there a particular reason e.g. Perl’s Cache and Cache::Cache classes don’t provide some mechanism like this? Is there a tried and true pattern I could use instead?

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… 🙂

EDIT: I have since learned that this is called a Cache stampede and have updated the question’s title.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:47:34+00:00Added an answer on June 4, 2026 at 11:47 am

    flock() it.

    Since your worker processes are all on the same system, you can probably use good, old-fashioned file locking to serialize the expensive calculate()ions. As a bonus, this technique appears in several of the core docs.

    use Fcntl qw(:DEFAULT :flock);    # warning:  this code not tested
    
    use constant LOCKFILE => 'you/customize/this/please';
    
    my $result = $cache->get( $key );
    
    unless ($result) {
        # Get an exclusive lock
        my $lock;
        sysopen($lock, LOCKFILE, O_WRONLY|O_CREAT) or die;
        flock($lock, LOCK_EX) or die;
    
        # Did someone update the cache while we were waiting?
        $result = $cache->get( $key );
    
        unless ($result) {
            $result = calculate( $key );
            $cache->set( $key, $result, '10 minutes' );
        }
    
        # Exclusive lock released here as $lock goes out of scope
    }
    
    return $result;
    

    Benefit: worker death will instantly release the $lock.

    Risk: LOCK_EX can block forever, and that is a long time. Avoid SIGSTOPs, perhaps get comfortable with alarm().

    Extension: if you don’t want to serialize all calculate() calls, but merely all calls for the same $key or some set of keys, your workers can flock() /some/lockfile.$key_or_a_hash_of_the_key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard that DateTime.Now is very expensive call (from here ) Is GETDATE()
I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
Let's say I have a business object that is very expensive to instantiate, and
I have a several very expensive queries which seem to hog resources that seems
I have a set of operations that are very expensive but are all pretty
I have a function that performs a very expensive computational task on a click
I have a very expensive query which gets executed from php and it can
I want to get number of images from Server? It is very expensive to
I have a small application that manages several git repositories similar to Github/Gitorious. Github
In postgis, is the ST_GeomFromText call very expensive? I ask mostly because I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.