#Why is the cache deleted instead of updated?
When updating data, you may encounter distributed transaction problems, causing cache updates to succeed but database modifications to fail. Even if the database modification fails and only the cache is deleted, the next query will still obtain data directly from the database and no dirty data will be generated.
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
Only delete later
## 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.
Supplement: Why should we delay double deletion to ensure cache consistency?
Why should we delay double deletion? Ensure cache consistency
- Before modifying the database data, redis needs to be deleted first: this is to ensure that during the interval between the modification of the database data and the deletion of the redis data, if there is a hit , ensure that this data does not exist in redis. If no deletion operation is performed, old data can still be read from redis after the database data has been modified, which will lead to data inconsistency.
- The second deletion is after modifying the database data. 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. During this time, if there is a request, the old data will be cached again in redis. However, 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 the data is sent from the database to the server cache for writing The time
But delayed double deletion, the delayed time is very difficult to determine, so delayed double deletion is not recommended
Based on comprehensive considerations, even if the database is modified first and the cache is deleted, old data will be read for a certain period of time, 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 cache)
The above is the detailed content of What does redis cache delayed double deletion mean?. For more information, please follow other related articles on the PHP Chinese website!