Home  >  Article  >  Database  >  How redis updates cache

How redis updates cache

(*-*)浩
(*-*)浩Original
2019-11-27 10:21:5010651browse

There are four Design Patterns for redis to update the cache: Cache aside, Read through, Write through, Write behind caching. Let’s take a look at these four Patterns one by one.

How redis updates cache
Cache Aside Pattern

This is the most commonly used pattern. The specific logic is as follows: (Recommended learning: Redis video tutorial)

Invalidation: The application first gets the data from the cache. If it does not get it, it will get the data from the database. After success, , put into the cache.

Hit: The application fetches data from the cache and returns after fetching it.

Update: First save the data into the database, and then invalidate the cache after success.

Note that our update is to update the database first, and then invalidate the cache after success. So, can this method avoid the problem mentioned earlier in the article? We can figure it out.

One is the query operation, and the other is the concurrency of the update operation. First of all, there is no operation to delete the cache data, but the data in the database is updated first. At this time, the cache is still valid, so concurrency The query operation takes data that has not been updated.

However, the update operation immediately invalidates the cache, and subsequent query operations pull the data out of the database. Unlike the logic problem at the beginning of the article, subsequent query operations will always fetch old data.

How redis updates cache

Read Through

Read Through routine is to update the cache during the query operation, that is, when the cache expires (expiration or LRU swap out), Cache Aside is the caller's responsibility to load the data into the cache, while Read Through uses the cache service to load it itself, so it is transparent to the application.

Write Through

Write Through routine is similar to Read Through, but it occurs when data is updated. When data is updated, if the cache is not hit, the database is directly updated and then returned. If the cache is hit, the cache is updated, and then the Cache itself updates the database (this is a synchronous operation)

The picture below comes from Wikipedia's Cache entry. You can understand the Memory as the database in our example.

How redis updates cache

Write Behind Caching Pattern

Write Behind is also called Write Back. Some students who know the Linux operating system kernel should be very familiar with write back. Isn't this the Page Cache algorithm of the Linux file system? Yes, you see the basics are all the same. Therefore, the foundation is very important. I have said this not once before.

Write Back routine, in a nutshell, when updating data, only the cache is updated, not the database, and our cache will update the database asynchronously in batches.

The advantage of this design is that the data I/O operations are extremely fast (because the memory is directly operated). Because of the asynchronous write backg, write backg can also merge multiple operations on the same data, so the performance improvement is Quite impressive.

However, the problem it brings is that the data is not strongly consistent and may be lost (we know that abnormal shutdown of Unix/Linux will cause data loss, because of this).

In software design, it is basically impossible for us to make a design without defects, just like time is exchanged for space in algorithm design, and space is exchanged for time. Sometimes, strong consistency and high performance, High availability and high performance are in conflict. Software design has always been about trade-off.

In addition, the implementation logic of Write Back is relatively complex, because it needs to track which data has been updated and needs to be flushed to the persistence layer. The write back of the operating system will be truly persisted only when the cache needs to be invalidated, for example, when the memory is not enough, or the process exits, etc. This is also called lazy write.

There is a flow chart of write back on wikipedia. The basic logic is as follows:

How redis updates cache

For more Redis related technical articles, please visit Getting Started with Redis Learn in the tutorial column!

The above is the detailed content of How redis updates cache. For more information, please follow other related articles on the PHP Chinese website!

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