The introduction

In the system design, in order to improve the data processing speed, the use of cache is everywhere. For example, in order to bridge the gap between CPU processing speed and data IO, there are registers, CPU cache, memory, disk pyramid mode of different storage media. In upper-level business applications, in order to speed up request processing, a layer of cache is often added to store hot data. There are several patterns for processing cached data.

Cache-aside

The bypass caching pattern is one of the most commonly used caching patterns 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 is read
  1. The business side initiates the data query read request
  2. The processing service first attempts to read the load data from the cache
  3. Determining whether the data exists in the cache, and if so, returning the cached data directly
  4. Otherwise, the data is loaded from main memory such as the database and written to the cache
  5. Returns the final query result
  • Data update
  1. The business side initiates the data write/update request
  2. The processing service first writes updates to main memory, such as the database
  3. After completion, the corresponding cache data deletion will be invalidated
  • Pattern analysis

The design pattern of bypass cache is mainly applicable to the scenario where the frequency of reading data is far greater than the frequency of writing and updating data, that is, the data will not be transformed or change little once the data is written, and the hot data will be cached by adopting data elimination strategies such as LRU.

However, the data update operation in this mode is prone to inconsistent data because it needs to deal with data and cache, and is not an atomic operation. The data consistency needs to be processed according to the actual business requirements. See “Getting Cache and Database Consistency” for details.

Read Through/Write Through

The read-through-write mode is to unify the data cache processing operations for encapsulation processing, adding a cache layer, shielding the application of the details of underlying data processing. In the bypass cache mode, the application needs to handle the processing logic of the cache hit or miss itself, increasing the complexity. By adding a cache layer, the business service only deals with the cache and does not care about the source of the specific data. The process of reading and writing data is as follows:

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

  • Data is read

The data read 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 both main memory and cached data as a single transaction.

  • Pattern analysis

The read-through-write pattern is suitable for situations where the same data is read and written frequently and there is a strong consistency requirement for main memory and cache data, such as the business system of a bank.

The cache writes back write-behind

Cache write-back is similar to write-through, except that in write-though mode both main memory and cached data are processed while the request is being processed, whereas in write-back mode only the cached data is updated and the cached data is written back to main memory in an asynchronous manner. The process of data writing and updating is as follows:

Asynchronous write-back of cached data can be done in a number of ways, such as by timing the write-back at a fixed time and frequency, or by counting the number of updates (or a fixed size) and writing the write-back when a certain number (size) is reached. You can also combine the two: either the time or the number of updates reached a specified value to trigger a write back operation.

A similar pattern is used for data updates in MySQL. MySQL will read the stored data into memory as pages. When updating data, only the data loaded in memory will be updated. In this case, the cached page of data is inconsistent with the data in disk, and the changed data pages are called “dirty pages”. A data write back to a dirty page is triggered when a dirty page is eliminated, or when the database is idle or closed.

  • Pattern analysis

On the one hand, since the business request only needs to update the coarse data, it can complete the operation, which greatly reduces the time of data writing and improves the processing efficiency. On the other hand, because of the inconsistency between the cached data and the master data, the old data will be read. In addition, because there is a delay in writing to the main storage, data loss may occur under abnormal circumstances. Therefore, the adoption of this pattern needs to be tolerant of some data inconsistencies and acceptable or compensatory for possible data loss.

conclusion

No pattern is perfect, and the specific strategy for choosing which cache to cache requires consideration of the actual business situation and additional compensation for the weaknesses or drawbacks of the chosen pattern.

In addition, the design of cache and many other design patterns, the hardware level and the software level will appear the same solution when solving some problems, so understanding and familiar with the excellent design of the underlying system or hardware, for the upper application and the current micro-service situation of the scheme design is of great reference significance.