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
Is Redis a SQL or NoSQL Database? The Answer ExplainedIs Redis a SQL or NoSQL Database? The Answer ExplainedApr 18, 2025 am 12:11 AM

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis: Improving Application Performance and ScalabilityRedis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AM

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis: Exploring Its Data Model and StructureRedis: Exploring Its Data Model and StructureApr 16, 2025 am 12:09 AM

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis: Classifying Its Database ApproachRedis: Classifying Its Database ApproachApr 15, 2025 am 12:06 AM

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Why Use Redis? Benefits and AdvantagesWhy Use Redis? Benefits and AdvantagesApr 14, 2025 am 12:07 AM

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Understanding NoSQL: Key Features of RedisUnderstanding NoSQL: Key Features of RedisApr 13, 2025 am 12:17 AM

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

Redis: Identifying Its Primary FunctionRedis: Identifying Its Primary FunctionApr 12, 2025 am 12:01 AM

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis: A Guide to Popular Data StructuresRedis: A Guide to Popular Data StructuresApr 11, 2025 am 12:04 AM

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor