Cache problem The three brothers are caused by the fact that all the requests hit the database for different reasons
What is cache penetration?
Cache penetration refers to querying a data, which does not exist in redis and MySQL. That is to query a data that does not exist , causing each request to reach the database, causing a lot of pressure on the data.
Solution:
1. Cache an empty data when querying a non-existent data, so that the request will only check the library once, and transfer the traffic to redis Advantages: It is simple and convenient to implement Disadvantages
:
Consuming memory, which may cause short-term data Inconsistency, increased pressure on the cache
2. Bloom filter
What is cache breakdown?
Cache breakdown: A hotspot key has expired , and data synchronization is slow, causing all requests to reach the database.
Solution
1. Mutual exclusion lock
2. Logical expiration
What is cache avalanche?
Cache avalanche means that a large number of keys expire at the same time during a certain period of time, causing all requests to reach the database
solution
How to synchronize data between redis and MySQL?
If it is strong consistency (CP): double-write consistency (modify the cache after modifying the database), the data stored in redis must read more and write less. When modifying data, using read-write locks is a good choice.
Features: strong consistency, low performance, code intrusion is too high
If it is availability (AP): mq (the vast majority of data synchronization, AP implementation can use mq, allowing short-term data inconsistency, to achieve final consistency), canal