Redis cache consistency problem

1. Redis cache consistency problem = how to ensure consistency between the data in the cache and the data in the database?

One, the two commonly used modes of redis

Note that the following two modes will have certain vulnerabilities under large concurrency.

1.1, double write mode

While modifying the database, modify the cache. If there are two concurrent operations, the first modification of the database is delayed (due to various reasons), during this period the second modification is performed, followed by the second modification cache. The first modification cache is executed last, which leads to the appearance of dirty data.

Insert picture description here

1.1.1. Solution one of dual writing mode------------>Locking method

The two operations of writing to the database and writing to the cache can be implemented by locking. A set of write database and write cache are executed before the second execution.

1.1.2, the solution of double write mode ------------> lock method

1.2, failure mode

Insert picture description here

Two, sophisticated solutions

Solved by canal, you can save the complexity of coding, just write the operation of writing the database. Because canal can actively update redis

Insert picture description here

Three, solve through canal

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43983411/article/details/110748691