search
HomeDatabaseRedisHow to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

前言

四月份的时候,有位朋友去美团面试,他说被问到Redis与MySQL双写一致性如何保证? 这道题其实就是在问缓存和数据库在双写场景下,一致性是如何保证的?本文将跟大家一起来探讨如何回答这个问题。

谈谈一致性

一致性就是数据保持一致,在分布式系统中,可以理解为多个节点中数据的值是一致的。

  • Strong consistency: This consistency level is most in line with user intuition. Whatever it requires the system to write will be read out. The user experience is good, but it is difficult to implement. It often has a great impact on the performance of the system
  • Weak consistency: This consistency level restricts the system from being able to read the written value immediately after the write is successful, nor does it guarantee that the written value can be read immediately. Promise how long it will take for the data to be consistent, but we will try our best to ensure that the data can reach a consistent state after a certain time level (such as the second level)
  • Eventual Consistency: Final Consistency It is a special case of weak consistency. The system will ensure that a data consistency state can be achieved within a certain period of time. The reason why final consistency is mentioned separately here is because it is a very respected consistency model in weak consistency, and it is also a model that is highly respected in the industry for data consistency in large distributed systems

Three classic caching modes

Caching can improve performance and relieve database pressure, but using cache can also lead to data inconsistency problems. How do we generally use cache? There are three classic caching patterns:

  • Cache-Aside Pattern
  • Read-Through/Write through
  • Write behind

Cache -Aside Pattern

Cache-Aside Pattern, that is, Bypass cache mode, is proposed to solve the problem of data inconsistency between the cache and the database as much as possible.

Cache-Aside read process

The read request process of Cache-Aside Pattern is as follows:

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

  1. When reading, read the cache first. If the cache hits, the data will be returned directly.
  2. If the cache does not hit, read the database, retrieve the data from the database, put it into the cache, and return the response at the same time.

Cache-Aside write process

The write request process of Cache-Aside Pattern is as follows:

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

When updating, first update the database and then delete the cache .

Read-Through/Write-Through (read-write penetration)

In Read/Write Through mode, the server uses the cache as the main data storage. The interaction between the application and the database cache is completed through the Abstract Cache Layer.

Read-Through

The brief process of Read-Through is as follows

Read Through简要流程

  1. Read from cache If the data cannot be read, it will be returned directly. If it cannot be read, it will be loaded from the database, written to the cache, and then the response will be returned.
  2. Is this brief process very similar to
Cache-Aside

? In fact, Read-Through is just an extra layer of Cache-Provider, and the process is as follows:

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)Read-Through is actually just

Cache-Aside

has a layer of encapsulation on top, which will make the program code more concise and reduce the load on the data source. Write-Through

Write-Through

In mode, when a write request occurs, the data source and cached data are also completed by the Cache Abstraction Layer The update process is as follows: How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)Write behind (asynchronous cache writing)

Write behind

followed by Read-Through/Write-ThroughThere are similarities. Cache Provider is responsible for reading and writing cache and database. There is a big difference between them: Read/Write Through updates the cache and data synchronously, while Write Behind only updates the cache and does not directly update the database, through Batch asynchronous way to update the database.

Write behind流程In this method, the consistency between the cache and the database is not strong.

Systems with high consistency requirements should be used with caution

. But it is suitable for frequent writing scenarios. MySQL's InnoDB Buffer Pool mechanism uses this mode. When operating the cache, should I delete the cache or update the cache?

In general business scenarios, we use the

Cache-Aside

mode. Some friends may ask, Cache-AsideWhen writing a request, why delete the cache instead of updating the cache?

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

When we operate the cache, should we delete the cache or update the cache? Let’s look at an example first:

  1. Thread A first initiates a write operation, and the first step is to update the database
  2. Thread B then initiates a write operation Write operation, the second step updates the database
  3. Due to network and other reasons, thread B updates the cache first
  4. Thread A updates the cache.

At this time, the cache saves A's data (old data), and the database saves B's data (new data). The data is inconsistent, and dirty data appears. . If deletes the cache instead of updating the cache, this dirty data problem will not occur.

Updating the cache has two disadvantages compared to deleting the cache:

  • If the cache value you write is obtained after complex calculations. If the cache is updated frequently, performance will be wasted.
  • When there are many database writing scenarios and few data reading scenarios, the data is often updated before it is read, which also wastes performance (actually, in scenarios where there is a lot of writing, It is not very cost-effective to use cache)

In the case of double writing, should the database be operated first or the cache first?

Cache-AsideIn the cache mode, some friends still have questions. When writing a request, why operate the database first? Why not operate the cache first?

Suppose there are two requests, A and B, requesting A to do the update operation and requesting B to do the query and read operation. How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

  1. Thread A initiates a write operation, the first step is del cache
  2. At this time, thread B initiates a read operation, cache miss
  3. Thread B continues Read DB, read out an old data
  4. Then thread B sets the old data into cache
  5. Thread A writes the latest data in DB

Jiang Zi has a problem La, The cache and database data are inconsistent. The cache stores old data, and the database stores new data. Therefore, Cache-Aside cache mode chooses to operate the database first instead of the cache first.

Cache Delayed Double Delete

Some friends may say that it is not necessary to operate the database first, just use the Cache Delayed Double Delete strategy? What is delayed double deletion?

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

  1. Delete the cache first
  2. Then update the database
  3. Sleep for a while (such as 1 second) and delete the cache again.

How long does it usually take to sleep for a while? Are they all 1 second?

This sleep time = the time it takes to read business logic data is several hundred milliseconds. In order to ensure that the read request ends, the write request can delete cached dirty data that may be brought by the read request.

Delete cache retry mechanism

Whether it is delayed double deletion or Cache-Aside first operates the database and then deletes the cache, If the second step of deleting the cache fails, the deletion failure will result in dirty data~

If the deletion fails, delete it a few more times to ensure that the cache deletion is successful~So you can introduce Delete cache Retry mechanism

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

  1. Write request to update the database
  2. The cache failed to delete for some reason
  3. Put the key that failed to be deleted into the message queue
  4. Consume messages from the message queue and obtain the key to be deleted
  5. Retry the deletion cache operation

Read the biglog Asynchronous cache deletion

The retry deletion cache mechanism is okay, but it will cause a lot of business code intrusion. In fact, you can also eliminate key asynchronously through the binlog of the database.

How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian)

Taking mysql as an example, you can use Alibaba's canal to collect binlog logs and send them to the MQ queue, and then confirm and process the update message through the ACK mechanism, delete the cache, and ensure data Cache consistency

Recommended learning: "Redis Video Tutorial"

The above is the detailed content of How to ensure double-write consistency between Redis and MySQL? (Meituan Ermian). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
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.

How to implement redis counterHow to implement redis counterApr 10, 2025 pm 10:21 PM

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

How to use the redis command lineHow to use the redis command lineApr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

How to build the redis cluster modeHow to build the redis cluster modeApr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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