That is, Bypass cache
, yes More commonly used caching strategies.
Read requestCommon process The application will first determine whether the cache has the data. If the cache hits, the data will be returned directly. If the cache misses, the cache will penetrate to the database and retrieve the data from the database. Query the data and then write it back to the cache, and finally return the data to the client. (2) First update the database and then delete the data from the cache. After looking at the picture of the write request, some students may ask: Why do we need to delete the cache? Can't we just update it directly? There are several pitfalls involved here, let’s step through them step by step. If the Cache aside strategy is used incorrectly, you will encounter deep pits. Let’s step into them one by one. Pitfall 1: Update the database first, then update the cache If there are two The execution process as shown above: (1) (2) (3) (4) The expected result after execution is that the database age is 20, the cache age is 20, and the result cache age is 18. This causes the cache data to be not the latest and dirty data appears. Trap 2: Delete the cache first, then update the database If The execution process as shown above: (1) (2) (3) After the whole process, it was found that the age in Trap Three: Update the database first, then delete the cache In the actual system, for The execution process as shown above: (1) (2) (3) After the entire process, it was found that But if we think about it carefully, the probability of the above problem occurring is actually very low, because database update operations usually take several orders of magnitude more time than memory operations. The last step in the figure above is write-back caching (set age 18) It's very fast and usually completes before updating the database. What if this extreme scenario occurs? We have to think of a solution: In the Cache Aside update mode, the application code needs to maintain two data sources: one is the cache and the other is the database. Under the As shown above, the application only needs to interact with When performing a large number of reads, The cache remains consistent with the data source, and writes always reach the data source through the As shown above, when the application updates two data, the Cache Provider will write it to the cache immediately, but it will be written to the database in batches after a period of time. middle. This method has advantages and disadvantages: After learning so much, I believe everyone has a clear understanding of the cache update strategy. Finally, a little summary. There are three main strategies for cache update: Cache aside Usually the database is updated first, and then the cache is deleted. To protect the data, the cache time is usually set. Read/Write through generally provides read and write operations by a Cache Provider, and the application does not need to know whether the cache or the database is being operated. Write behind simply understands that it is delayed writing. Cache Provider will batch input the database every once in a while. The advantage is that the application writes very quickly. Okay, I’m here today. Have you learned it? Write request
Common process
Cache aside pitfalls
write requests at the same time
Data needs to be updated, each write request The database is updated first and then the cache is updated. Data inconsistency may occur in concurrent scenarios. Write request 1
Update the database, update the age field to 18; Write request 2
Update the database, update the age field to 20;Write request 2
Update cache, cache age is set to 20;Write request 1
Update cache, cache age is set to 18 ;write request
The processing flow isDelete the cache first and then update Database
, in a read request
and a write request
concurrent scenario, data inconsistency may occur. Write request
Delete cached data; Read request
Query cache miss (Hit Miss), then query the database and write the returned data back to the cache;Write request
Update database. database
was 20, and the age in cache
was 18. The cache and database data were inconsistent, and dirty data appeared in the cache. write requests
it is still recommended to update first The database then deletes the cache
, but there are still problems in theory, as shown in the following example. Read request
Query the cache first, if the cache is not hit, query the database to return data; Write request
Update the database and delete the cache; Read request
Write back cache; database age is 20
, cache age is 18
, that is, the database and cache are inconsistent, resulting in application The data read by the program from the cache is old data. Cache data setting expiration time
. Usually in the system, a small amount of data can be allowed to be inconsistent for a short period of time.
Read through
Read-Through
strategy, the application does not need to manage the cache and database, and only needs to entrust the synchronization of the database to the cache provider Cache Provider
. All data interactions are completed through the Abstract Cache Layer
. Cache Provider
, and does not need to care whether it is fetched from the cache or database. Read-Through
can reduce the load on the data source and is also resilient to cache service failures. If the cache service goes down, the cache provider can still operate by going directly to the data source. Read-Through is suitable for scenarios where the same data is requested multiple times
, which is very similar to the Cache-Aside strategy, but there are still some differences between the two, which are emphasized again:
Write through
Write-Through
strategy, when a data update (Write) occurs, the cache provider Cache Provider
Responsible for updating the underlying data source and cache. Abstract Cache Layer
. Cache Provider
It acts like a proxy.
Write behind
Write behind
In some places Also called Write back
, the simple understanding is: when the application updates data, it only updates the cache, Cache Provider
refreshes the data into the database at regular intervals. To put it bluntly, it is Delayed writing
.
Advantage
is that the data writing speed is very fast and is suitable for frequent writing scenarios. . Disadvantage
is that the cache and database are not strongly consistent, so use it with caution in systems with high consistency requirements.
To summarize
The above is the detailed content of In a high-concurrency scenario, should the cache or the database be updated first?. For more information, please follow other related articles on the PHP Chinese website!