search
HomeDatabaseRedisCause analysis: delayed double deletion of redis cache

Recommended learning: Redis video tutorial

Why is the cache deleted instead of updated?

If it is an update and there is a distributed transaction problem, the cache may be modified and the database modification may fail. If you just delete the cache, even if the database modification fails, the next query will directly fetch the data from the database, and no dirty data will appear.

What is delayed double deletion?

That is, when adding, deleting or modifying an entity class, the cache of the entity class must be cleared. The clearing position is before and after the database operation method.

Adopt proof by contradiction

Only delete first



Cause analysis: delayed double deletion of redis cache

##Only delete later

Cause analysis: delayed double deletion of redis cache

Conclusion

This leads to the conclusion that there are problems with both front deletion and back deletion. Therefore, the strategy of delayed double deletion is adopted

Thinking 2: Why is it delayed

It is still a proof by contradiction. The situation in the figure below shows the situation where the old cache still exists after double deletion. The delay is

to ensure that the change cache operations of other transactions have been completed before modifying the database -> clearing the cache.

Cause analysis: delayed double deletion of redis cache

Additional: Why should we delay double deletion to ensure cache consistency

Why To delay double deletion to ensure cache consistency

    Before modifying the database data, redis needs to be deleted first: this is to ensure that within the interval between the database data modification and the redis data deletion, If there is a hit, it is guaranteed that this data does not exist in redis. Without this deletion, when the database data has been modified, old data can still be read from redis, resulting in data inconsistency.
  • The second deletion is after the database data is modified. At this time, the corresponding data in redis needs to be deleted again. This time it is to delete the first redis deletion and the database data modification. If there is a request, Then the old data will be cached again in redis, but the data in the database will be modified next. If it is not deleted this time, the old data in the database will exist in redis.
  • So why do you need to delay deleting redis for a certain period of time after the database is modified the second time?
  • In order to wait for the previous read of the database, wait for the data to be written to the cache, and finally delete the dirty data, so it is the time it takes for the data to be sent from the database to the server cache for writing

Cause analysis: delayed double deletion of redis cache

But delayed double deletion, the delay time is very difficult to determine, so delayed double deletion is not recommended

According to comprehensive considerations, even if the database is modified first, After deleting the cache, there will be a certain period of time when old data is read, which is usually tolerated.

As long as the cache is deleted in time, other threads can read the latest value.

At the same time, in order to ensure that the cache will be deleted, mq can be used to ensure that the cache will be deleted.

If the message is not repeatedly consumed in mq, it will be handed over to other consumers for consumption. (Delete the cache)

Recommended learning:

Redis video tutorial

The above is the detailed content of Cause analysis: delayed double deletion of 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: 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

How to read redis queueHow to read redis queueApr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis cluster zsetHow to use redis cluster zsetApr 10, 2025 pm 10:09 PM

Use of zset in Redis cluster: zset is an ordered collection that associates elements with scores. Sharding strategy: a. Hash sharding: Distribute the hash value according to the zset key. b. Range sharding: divide into ranges according to element scores, and assign each range to different nodes. Read and write operations: a. Read operations: If the zset key belongs to the shard of the current node, it will be processed locally; otherwise, it will be routed to the corresponding shard. b. Write operation: Always routed to shards holding the zset key.

How to clear redis dataHow to clear redis dataApr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to set the redis expiration policyHow to set the redis expiration policyApr 10, 2025 pm 10:03 PM

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use