How redis maintains consistency with mysql
There are two ways to maintain consistency between redis and mysql, which are: 1. Perform the [redis.del(key)] operation before and after writing the database, and set a reasonable timeout; 2. Through The synchronization mechanism based on subscribing to binlog achieves consistency between redis and mysql.
The method to maintain consistency between redis and mysql is to adopt a delayed double delete strategy, first delete the cache and then write to the database; the second method is to update the cache asynchronously. First read Redis and then write mysql and then update the Redis data
The cache and database consistency solutions are as follows:
Method 1: Use delay Double delete strategy
Perform redis.del(key) operation before and after writing the database, and set a reasonable timeout.
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) Then write Database
(3) Sleep for 500 milliseconds
(4) Delete cache again
So, how is this 500 millisecond determined, and how long should it sleep for?
You need to evaluate the time-consuming data reading business logic of your project. The purpose of this is to ensure that the read request ends, and the write request can delete the cached dirty data caused by the read request.
Of course, this strategy also takes into account the time-consuming synchronization between redis and the database master-slave. The final sleep time for writing data: Add a few hundred milliseconds to the time it takes to read data business logic. For example: sleep for 1 second.
Set the cache expiration time
Theoretically, setting the cache expiration time is a solution to ensure eventual consistency. All write operations are subject to the database. As long as the cache expiration time is reached, subsequent read requests will naturally read new values from the database and backfill the cache.
Disadvantages of this solution
Combined with the double delete policy cache timeout setting, the worst case scenario is that the data is inconsistent within the timeout period, and additional write requests are added time consuming.
Method 2: Asynchronous update cache (synchronization mechanism based on subscribing to binlog)
Overall technical idea:
MySQL binlog Incremental subscription consumption message queue incremental data is updated to redis
1) Read Redis: hot data is basically all in Redis
2) Write MySQL: additions, deletions and modifications are all operations MySQL
3) Update Redis data: MySQ’s data operation binlog is updated to Redis
Redis update
(1) Data operations are mainly divided into two blocks:
One is full (write all data to redis at once) and the other is incremental (real-time update)
What we are talking about here is incremental, which refers to update, insert and delete of mysql Change data.
(2) After reading the binlog, analyze it, and use the message queue to push and update the redis cache data of each station.
In this way, once new write, update, delete and other operations occur in MySQL, the binlog related messages can be pushed to Redis, and Redis will update Redis based on the records in the binlog.
In fact, this mechanism is very similar to MySQL's master-slave backup mechanism, because MySQL's master-slave backup also achieves data consistency through binlog.
Here you can use canal (an open source framework of Alibaba), through which you can subscribe to MySQL's binlog, and canal imitates the backup request of mysql's slave database to update Redis data. Achieved the same effect.
Of course, you can also use other third parties for the message push tools here: kafka, rabbitMQ, etc. to implement push updates to Redis
The above is the detailed content of How redis maintains consistency with mysql. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.
