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

to ensure that the change cache operations of other transactions have been completed before modifying the database -> clearing the 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
As long as the cache is deleted in time, other threads can read the latest value.
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!

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.

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.

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.

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

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.

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 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.

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

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 latest version

SublimeText3 Chinese version
Chinese version, very easy to use