Home >Database >Redis >How to synchronize MySQL data to Redis cache

How to synchronize MySQL data to Redis cache

王林
王林forward
2023-05-27 09:08:101404browse

1 Mysql checks the data and then writes it to Redis synchronously

Disadvantage 1: It will cause delay to the interface, because synchronous writing to redis itself has delay, and retry is required. If redis If the write fails, you need to try again, which is even more time-consuming.

Disadvantage 2: There is no decoupling. If redis crashes, the thread will be blocked directly.

Disadvantage 3: If someone is the database, it will not be synchronized unless the corresponding database is manually deleted. Redis, but there is a time difference in the process of deleting Redis

2 After Mysql checks the data, it synchronizes Redis in the consumer thread by sending MQ

Disadvantage 1: There are more layers of MQ, that is, it will There is a high probability of causing synchronization delay problems.

Disadvantage 2: Prevent the availability of MQ

Disadvantage 3: If someone is the database, it will not be synchronized

Advantage 1: Can greatly reduce the problem of delayed return of the interface

Advantage 2: MQ itself has a retry mechanism, no need to manually write retry code

Advantage 3: Decoupling, Mysql query and Redis synchronization are completely separated and do not interfere with each other

3 Subscribe to Mysql's Binlog file (can be done with the help of Canal)

CanalServer will pretend to be a MysqlServer slave library to subscribe to the MysqlServer main library Binlog file

When Canal starts, it will configure the corresponding message MQ (RabbitMQ, RocketMQ, Kafka). When it detects changes in the Binlog file, it will convert the changed sql statement into json format and send it as the message content. In the

project in MQ, as long as you monitor the corresponding MQ, you can get the content of Binlog changes. The Json data has a clear operation type (CURD) and the corresponding data. Just synchronize the corresponding data to redis

Disadvantage 1: The entire operation process of canal subscribing to Binlog is single-threaded, so in the face of ultra-high concurrency, the performance may not be excellent. You can deploy multiple Canals and multiple consumers, but you need to pay attention to avoid repeated consumption problems and perform idempotence verification

Advantage 1: Even if the database is modified manually, it will be monitored and synchronized.

Advantage 2: Asynchronous synchronization, no extra delay in interface return

4 Delayed double deletion

Delete the redis data before executing the modified sql

Execute update sql

Delay for a period of time

Delete redis data again

// 延迟双删伪代码
deleteRedisCache(key);   // 删除redis缓存
updateMysqlSql(obj);        // 更新mysql
Thread.sleep(100);           // 延迟一段时间
deleteRedisCache(key);   // 再次删除该key的缓存

Disadvantages: This delay time is difficult to control, how long the delay is, this is very It’s difficult to evaluate

If you don’t use delayed double deletion, you just delete the cache and then modify the MySQL data. What problems will arise if there are only these two steps?

5. Single request, single thread is no problem, but problems will occur under high concurrency and multi-threading

6. If Thread1 thread wants to update data, Thread1 thread will clean up redis at this time

7. At this time, Thread2 thread has come, but Thread1 has not finished updating mysql.

8. Thread2 query redis must be null. At this time, Thread2 will check mysql, and then the found data Write to cache

9. Since Thread1 has not had time to modify the mysql data, the data found by Thread2 at this time is [old data], and Thread2 writes the old data to Redis again

10 . At this time, the Thread3 thread comes, and after querying Redis and finding that there is data, it directly gets the cached data. At this time, [Thread3 finds out the old data] and returns directly with the old data. This is the problem.

11. The second deletion function of delayed double delete is to prevent Thread2 from writing old data again. With delayed double delete, Thread3 will still get null when querying Redis, and will get the latest data from mysql

12. So the normal delay time should be the entire time from Thread2 checking cache to getting mysql data and then saving it to redis, as the delay time of Thread1, but the time of Thread2 process will be affected by many factors. Therefore, it is difficult to determine how long it will take

5 Delayed double writing

// 延迟双写伪代码
updateMysqlSql(obj);        // 更新mysql
addRedis(key);   // 再次删除该key的缓存

The above code defect;

  • Under high concurrency, two threads execute at the same time The above code is modified to mysql, and the modification content is blocked, which may lead to inconsistency between Redis and Mysql data

  • The T1 thread finishes executing updateMysqlSql and releases the row lock. At this time, the T2 thread executes again updateMysqlSql and addRedis, and finally T1 executes addRedis. This situation will cause the database to be changed to the data of the T2 thread, but Redis is the data of the T1 thread

Optimization

// 完美延迟双写伪代码
开启事务
updateMysqlSql(obj);        // 更新mysql
addRedis(key);   // 再次删除该key的缓存
提交事务

Correction of the above code:

Put the two lines of code into a transaction. Only when T1 finishes executing Mysql and Redis, can T2 start executing, thus ensuring data consistency. It is recommended to use distributed lock

Double-write disadvantage: Mysql and Redis are single-threaded. Performance is not good, so it is not recommended to use

The above is the detailed content of How to synchronize MySQL data to Redis cache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete