How to prevent a database from running a DDoS application when the cache expires?

We have a high traffic site similar to StackOverflow with an object cache in memcache. The site is built with PHP (CodeIgniter) and MySQL.

Each time a TTL (lifetime) expires on a cached object that is part of the loading of each page, all page loads at this point lead to a query in the database, effectively performing DDOS on the database.

Is there a way for only one pageload to retrieve data and load another page, waiting for the cache to be updated first?

My first idea is to get the randomizer to work so that some of the pages load data, while others have to wait a second before revising the cache. But, of course, there should be a better way.

+4
source share
3 answers

You can use my algorithm from this code: https://github.com/jamm/Memory/blob/master/lib/Jamm/Memory/MemcacheObject.php#L230

  • Read Key and TTL
  • If the TTL is small (less than 5 seconds, for example), try locking a special key (not what you are reading), for example '_update. {name_of_key} '
  • If the lock is successful, calculate (or read) the new value and update cache
  • release update.

Thus, only 1 process will read the new value from the database.

+2
source

, cronjob, , .

, , 20 ( ), . , .

+2

, - , , ( ...):

if ($this->cacheExpired()){
    if (!$this->isMarkedAsRegenerating())
    {
         $this->markAsRegenerating();              //..ing
         $this->regenerateCache();
         $this->markAsCacheRegenerated();          //..ed
    } else {
         while ( $this->isMarkedAsRegenerating() )
         {
               sleep(1); //sleep 1 second to decrease database-queries, we are preventing a DDOS you know...
         }
    }
}

$this->output(); //at this point, we always have a cached version of the page

, , - , * . , , , .

+1

All Articles