Reputation: 490
Could you help me with my issue. I have some slow mysql queries, so I'm caching its results into memcache.
I do something like this:
$data = get_from_cache();
if (! $data) {
$data = get_from_mysql();
set_cache($data);
}
Problem. Sometimes I have about 10 requests per second. So, when my cache is expiring, I have 5-10 get-requests which are initiating this slow mysql query at the same time.
Could you recommend a pattern for me to make a mutex in php, or something like this, to do only one slow request to mysql.
Upvotes: 3
Views: 1215
Reputation: 781716
This is known as the dog pile problem. Strategy: Break Up The Memcache Dog Pile describes two approaches:
Solution 1:
Solution 2:
Upvotes: 4