Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Przemek Sobstel:
Preventing the Dogpile Effect
Aug 11, 2014 @ 16:47:28

Przemek Sobstel has a recent post investigating an interesting theory in caching of any kind of application, not just PHP ones. He looks at the dogpile effect: when a cache expires and the database or host cannot catch up with so many non-cached requests coming in.

Implementing caching in web apps seems to be simple. You check if value is cached. If it is, you fetch cached value from cache and serve it. If it’s not, you generate new value and store in cache for future requests. Simple like that. However, what if value expires and then you get hundreds of requests? It cannot be served from cache anymore, so your databases are hit with numerous processes trying to re-generate the value. And the more requests databases receive, the slower and less responsive they get. Load spikes. Until eventually they likely go down.

He recommends using something called a "semaphore lock" to help prevent this kind of issue from happening. This lock prevents the removal of any stale content until after one process has finished refreshing the requested data. Only once the lock is released are the other processes allows to serve the fresh data. He includes some PHP pseudo-code that illustrates the point: trying to fetch the content, checking for the lock and releasing it when the single process is done with the refresh. He includes a link to a full implementation as well. He's also written up a full library, Metaphore, that integrates this into a full caching system.

tagged: dogpile effect theory cache tutorial metaphore semaphore

Link: http://www.sobstel.org/blog/preventing-dogpile-effect/


Trending Topics: