Home  >  Article  >  Backend Development  >  Simply handle the problem of sharp increase in database load caused by high concurrent access after K->V cache failure_PHP tutorial

Simply handle the problem of sharp increase in database load caused by high concurrent access after K->V cache failure_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:13:51963browse

In daily programming, for some database operations with a certain amount of concurrency or high data volume, we will add a cache layer on the front end and set the expiration time. Now it is usually mongoDB or memcached. The simple process is as follows:

This mode has basically no problem when the amount of concurrency is not too high or the data operation efficiency is very high.
But maybe you have seen, if (cache failure && happens to encounter high concurrency && long database operation time) then?
1. Cache invalidation
2. The first process goes to the database to obtain new data. If it includes SQL+ program logic, it takes 5S
3. Within these 5 seconds, the second, third...Nth one only obtained the expired cache, so they all connected to the database...
4. The result is obvious , the database locks the table -> The database accumulates a large number of processes -> until the database hangs up!

So, how to solve this problem? In fact, the simplest solution is:
Add a mark, similar to a file lock, to determine whether the program is updating the cache at this time.
If yes, return to the old cache directly (the mark has a set expiration time to avoid the cache not updating problem caused by the mark not being deleted due to program errors)
If not , then set a mark, then perform data acquisition and cache update, and finally delete the mark.

PS. Some users may not be able to see the data when the cache is generated for the first time, but this chance is very small and can be solved through other methods such as manual generation.


The process is as follows:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440329.htmlTechArticleIn daily programming, for some database operations with a certain amount of concurrency or high data volume, we will add A cache layer and set the expiration time. Now it is usually mongoDB or me...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn