Talking about cache mode

Time:2021-6-24

introduction

In the system design, in order to improve the data processing speed, cache is used everywhere. For example, in order to make up for the difference between CPU processing speed and data IO, there are different storage media of register, CPU cache, memory and hard disk pyramid mode in turn. 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 to deal with cache data.

Bypass cache aside

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

Talking about cache mode

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

The design mode of bypass cache is mainly suitable for the situation that the read frequency of data is much higher than the write update frequency, that is, once the data is written, the data will not change or rarely change. The hot data is cached by using data elimination strategies such as LRU.

But the data update operation in this mode needs to deal with data and cache, and is not atomic operation, so it is easy to have data inconsistency. It needs to be processed according to the actual business requirements for data consistency. For details, please refer to one article: cache and database consistency [link]

Read through / write through

Read through and write through mode is to encapsulate the cache operation of data and add a cache layer to shield the details of underlying data processing. In bypass cache mode, the application needs to handle the cache hit or miss processing logic by itself to increase the complexity. By adding a 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:

Talking about cache mode

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

  • data fetch

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

  • Data update

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

  • pattern analysis

The read through / write through mode is suitable for the situation that the same data is read and written frequently, and the main memory and cache data have strong consistency requirements, such as the bank business system.

Cache write back write behind

Cache write back is similar to write through, except that in write through mode, both the main memory and the cached data will be processed while processing the request, while write back mode only updates the cached data, and then writes the cached data back to main memory asynchronously. The data write update process is as follows:

Talking about cache mode

There are many ways to write back the cache data asynchronously, such as: write back regularly according to a fixed time frequency, or count the number of data updates (or fixed size) and write back when it reaches a certain number (size). You can also combine the two: when either the time or the number of updates reaches the specified value, the write back operation will be triggered.

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

  • pattern analysis

On the one hand, because the business request only needs to update the coarse data to complete the operation, it greatly reduces the time of data writing and improves the processing efficiency; On the other hand, because of the inconsistency between the cache data and the master data, the old data will be read. In addition, due to the delay in writing to the main memory, data loss may occur in abnormal cases. Therefore, the adoption of this mode needs to be able to tolerate certain data inconsistency and accept or compensate for the possible data loss.

summary

No mode is perfect. The specific choice of caching 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, cache design and many other design patterns, hardware level and software level will have the same solution when solving some problems, so 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 scheme design in the current micro service situation.