Database Consistency Problems and Solutions

What is database consistency problem

data inconsistency between databases

Why Database Consistency Issues Occur

When we are doing business, if we go to MySQL to access data every time, the efficiency is relatively low. We will introduce a cache mechanism to store the data in Redis, and every time we go to Redis to read and go to MySQL to write data

Since data is written in MySQL, Redis also needs to be updated, and because each key in Redis has an expiration time, when it expires, there will be data inconsistencies between MySQL and Redis

How to solve

1. Update the cache first, then update the database

2. Update the database first, then update the cache

there are two problems

1. Step 1 succeeds, Step 2 fails

2. Concurrency order problem

A more optimal approach

Delayed double delete

Delayed double deletion For the first solution , sleep for a while after updating the database and update the cache again

message queue

For the second solution, update the database first, send a message to the message queue after success, delete the cache after consuming the message, and use the retry mechanism of the message queue to achieve the effect of final consistency

Subscribe to the binlog log, and then operate the log

Binlog is a log file in the MySQL database that records all database change operations, including insert, update, and delete operations. By monitoring the binlog file, you can obtain the change operations of the database and synchronize these operations to other nodes or systems to ensure data consistency.

Guess you like

Origin blog.csdn.net/csxylrf/article/details/131329457