Talk about cache mode

Time:2022-6-17

introduction

In the system design, cache is used everywhere in order to improve the data processing speed. For example, in order to make up for the gap between CPU processing speed and data IO, there are different storage media of register, CPU cache, memory and hard disk pyramid mode. In the upper business applications, in order to improve the request processing speed, a layer of cache is usually added to store hot data. There are several modes for processing cached data.

Bypass cache aside

Bypass cache mode is the most commonly used cache mode in development. Its core idea is to add an object to the cache only when it is requested. The process of reading and writing cached data in this mode is as follows:

  • data fetch
  1. The service side initiates a data query read request
  2. The processing service first attempts to read load data from the cache
  3. Judge whether the data in the cache exists. If it exists, directly return the cached data
  4. Otherwise, load data from main memory such as the database and write the data to the cache
  5. Return the final query result
  • Data update
  1. The service side initiates a data write / update request
  2. The processing service first updates and writes to the main memory such as the database
  3. Delete the corresponding cache data after completion
  • pattern analysis

The design mode of bypass cache is mainly applicable to the scenario where the data reading frequency is much higher than the write update frequency, that is, once the data is written, the data will not change or change little, and the hot data will be cached by adopting data elimination strategies such as LRU.

However, the data update operation in this mode needs to process data and cache, and is not an atomic operation, so data inconsistency is easy to occur. Data consistency needs to be handled according to actual business requirements. For details, please refer to the article on cache and database consistency [link]

Read through / write through

The read through write mode encapsulates the data cache processing operations, adds a cache layer, and masks the details of the underlying data processing for applications. In the bypass cache mode, the application needs to handle the cache hit or miss processing logic by itself, which increases the complexity. By adding the cache layer, business services only deal with the cache and do not care about the source of specific data. The data reading and writing process is as follows:

=======================================

  • data fetch

The data reading process is similar to the bypass cache, except that the cache logic is handled uniformly by the cache layer.

  • Data update

When data is updated, the cache layer updates the main memory and cache data simultaneously as a transaction.

  • pattern analysis

The read through / write through mode is applicable to the situation where the same data is frequently read and written, and there are strong consistency requirements for the main memory and cache data, such as the bank’s business system.

Cache writeback write behind

Cache write back is similar to write through, except that in the write through mode, the main memory and cached data will be processed at the same time when processing requests, while in the write back mode, only the cached data will be updated, and then the cached data will be written back to the main memory in an asynchronous manner. The data writing and updating process is as follows:

The asynchronous write back of cached data can be done in a variety of ways, such as regularly writing back at a fixed time and frequency, or counting the number (or fixed size) of data updates and writing back when a certain number (size) is reached. You can also combine the two: when either the time or the number of updates reaches the specified value, a write back operation is triggered.

In mysql, a similar pattern is adopted for data updating. MySQL reads the stored data into the memory in the form of pages. When updating the data, only the data loaded in the memory will be updated. At this time, the cache page of the data is inconsistent with the data in the disk. The changed data page is called “dirty page”. When a dirty page is eliminated, or the database is idle or closed, data write back to the dirty page is triggered.

  • pattern analysis

On the one hand, the cache write back mode greatly reduces the data writing time and improves the processing efficiency because the business request only needs to update the rough data; On the other hand, the inconsistency between the cached data and the master data will lead to the reading of old data. In addition, because there is a delay in writing to the primary storage, data loss may occur under abnormal circumstances. Therefore, this mode needs to be able to tolerate certain data inconsistencies, and can accept or compensate for possible data loss.

summary

No mode is perfect. The specific cache strategy needs to be combined with the actual business situation, and additional compensation should be made for the disadvantages or defects of the selected mode.

In addition, the cache design and many other design patterns, the hardware level and the software level will have the same solution when solving some problems. Therefore, understanding and being familiar with the excellent design of the underlying system or hardware is of great reference significance for the upper application and the solution design in the current microservice situation.

Recommended Today

Records about the common problems of Microsoft office 2021 home and student versions _ the shadow of excel in the cell selection is stuck and delayed during the process of pulling down the data area and is out of sync with the mouse pointer!

The problem has been tested on the home and student versions of office 2021 on 2 computers, and the same problem occurs The mouse operation is to pull down at a constant speed. Pay attention to the change speed of the number of lines. The number of pull-down lines in the data area changes slowly […]