search
HomeDatabaseRedisHow to synchronize MySQL data to Redis cache

How to synchronize MySQL data to Redis cache

May 27, 2023 am 09:08 AM
mysqlredis

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:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Beyond SQL - The NoSQL PerspectiveRedis: Beyond SQL - The NoSQL PerspectiveMay 08, 2025 am 12:25 AM

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis: A Comparison to Traditional Database ServersRedis: A Comparison to Traditional Database ServersMay 07, 2025 am 12:09 AM

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redis: Introduction to a Powerful In-Memory Data StoreRedis: Introduction to a Powerful In-Memory Data StoreMay 06, 2025 am 12:08 AM

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Is Redis Primarily a Database?Is Redis Primarily a Database?May 05, 2025 am 12:07 AM

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redis: Database, Server, or Something Else?Redis: Database, Server, or Something Else?May 04, 2025 am 12:08 AM

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redis: Unveiling Its Purpose and Key ApplicationsRedis: Unveiling Its Purpose and Key ApplicationsMay 03, 2025 am 12:11 AM

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces

Redis: A Guide to Key-Value Data StoresRedis: A Guide to Key-Value Data StoresMay 02, 2025 am 12:10 AM

Redis is an open source memory data structure storage used as a database, cache and message broker, suitable for scenarios where fast response and high concurrency are required. 1.Redis uses memory to store data and provides microsecond read and write speed. 2. It supports a variety of data structures, such as strings, lists, collections, etc. 3. Redis realizes data persistence through RDB and AOF mechanisms. 4. Use single-threaded model and multiplexing technology to handle requests efficiently. 5. Performance optimization strategies include LRU algorithm and cluster mode.

Redis: Caching, Session Management, and MoreRedis: Caching, Session Management, and MoreMay 01, 2025 am 12:03 AM

Redis's functions mainly include cache, session management and other functions: 1) The cache function stores data through memory to improve reading speed, and is suitable for high-frequency access scenarios such as e-commerce websites; 2) The session management function shares session data in a distributed system and automatically cleans it through an expiration time mechanism; 3) Other functions such as publish-subscribe mode, distributed locks and counters, suitable for real-time message push and multi-threaded systems and other scenarios.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools