Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of keeping consistency between redis and mysql

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How to maintain consistency between redis and mysql? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

The method of keeping consistency between redis and mysql is to delete the cache before writing to the database by delaying double deletions, and the second method is to update the cache asynchronously, read Redis, write mysql and update Redis data.

The caching and database consistency solutions are as follows:

Method 1: adopt delayed double deletion strategy

Redis.del (key) operation is performed before and after writing the library, and a reasonable timeout is set.

The pseudo code is as follows

Public void write (String key,Object data) {redis.delKey (key); db.updateData (data); Thread.sleep (500); redis.delKey (key);}

The specific steps are:

(1) delete the cache first

(2) rewrite the database

(3) dormant for 500 milliseconds

(4) delete the cache again

So, how do you determine this 500 milliseconds, and how long should it be dormant?

You need to evaluate the time-consuming business logic of reading data for your project. The purpose of this is to ensure that the read request ends, and the write request removes the cache dirty data caused by the read request.

Of course, this strategy also takes into account the time consuming of redis and database master-slave synchronization. The final dormancy time for writing data: add a few hundred ms on the basis of reading the data business logic. For example: dormant for 1 second.

Set cache expiration time

In theory, setting an expiration time for the cache is the solution to ensure ultimate consistency. All write operations are based on the database, and as long as the cache expiration time is reached, subsequent read requests will naturally read the new value from the database and backfill the cache.

The disadvantages of the scheme

Combined with the double delete policy + cache timeout setting, the worst-case scenario is that the data is inconsistent during the timeout and increases the time-consuming of the write request.

Method 2: update cache asynchronously (synchronization mechanism based on subscription binlog)

The overall idea of technology:

MySQL binlog incremental subscription consumption + message queuing + incremental data update to redis

1) read Redis: the hot data is basically in Redis.

2) write MySQL: all additions, deletions and modifications operate MySQL

3) Update Redis data: MySQ's data operation binlog to update to Redis

Redis update

(1) data operation is mainly divided into two parts:

One is full (all data is written to redis at once) and the other is increment (real-time update)

Here we are talking about increments, which refers to mysql's update, insert, and delate change data.

(2) analyze after reading binlog, use message queue to push and update the redis cache data of each station.

In this way, once there are new write, update, delete and other operations in MySQL, the messages related to binlog can be pushed to Redis,Redis, and then the Redis can be updated according to the records in binlog.

In fact, this mechanism is very similar to the master-slave backup mechanism of MySQL, because the master and slave of MySQL also achieve data consistency through binlog.

Here you can use canal (an open source framework of Ali), through which you can subscribe to MySQL's binlog, and canal imitates the backup request of mysql's slave database, which makes the data update of Redis achieve the same effect.

Of course, you can also use other third parties: kafka, rabbitMQ, etc., to push and update Redis in the message push tool here.

Thank you for reading! After reading the above, do you have a general idea of how redis is consistent with mysql? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report