This article will give you a brief understanding of cache penetration, cache avalanche, cache breakdown and cache consistency in Redis, and introduce the solutions for cache penetration and cache avalanche. I hope it will be helpful to everyone!
The cache fails in a large area at the same time, and subsequent requests will fall on the database, causing the database to be unable to withstand a large number of requests in a short period of time and collapse.
For example, on the e-commerce homepage, the key expiration time of all homepages is 12 hours and refreshed at 12 noon. If there is a flash sale event at 0:00 and a large number of users pour in, but all keys in the cache are invalid at that time, all Requests will fall to the database. If the database cannot handle it, it will just crash. Or if redis is down, a large number of requests will fall to mysql, causing it to hang up. [Related recommendations:Redis Video Tutorial]
So in this case, the expiration time of each key should be added. Random values to avoid a large number of key failures at the same time. If it is a redis cluster deployment, hotspot data can be distributed to different libraries.
Beforehand: Try to ensure the high availability of the redis cluster, make up for machine downtime as soon as possible, and choose an appropriate memory elimination strategy
In progress : Local ehcache cache hystrix current limit and downgrade to avoid mysql crash
Afterwards: The data saved by the redis persistence mechanism is restored to the cache as soon as possible.
A large number of requested keys do not exist in the cache. For example, a hacker creates a key that does not exist in the cache and initiates a large number of requests, resulting in a large number of requests being dropped. to the database.
First of all, you should do basic input parameter verification and directly intercept illegal parameters. For example, query the database id cannot be less than 0, verify the email address Format, etc.
If neither the cache nor the database can find the data for a certain key, write the key to redis, the value is null, and set the expiration time to avoid the next request. Fall on the database.
Through the Bloom filter, the Bloom filter can very conveniently determine whether a given data exists in massive data. All possible requested values can be stored in Bloom filter, when a request comes in, it first determines whether the request sent by the user exists in the Bloom filter, and intercepts it directly if it does not exist.
Cache breakdown refers to a very hot spot of Key, which is constantly carrying large concurrency, and large concurrency concentrates on accessing this point. , when the key expires, the continuous large concurrency will break through the cache and directly request the database
If strong consistency is required, the cache cannot be used, because the guarantee Without strong consistency, only eventual consistency can be guaranteed.
If the database update fails, the database still has old data, redis is empty, and the data will not be inconsistent. Reading empty data will Go to the database to query and then update to the cache.
Delete the cache first, and then update the database. Problems may also occur in high concurrency scenarios, such as deleting the cache. The database has not been updated yet. Another thread comes in and finds that redis is empty. It will read the database and then update to redis. At this time, the cached thread is deleted and then the database is updated, which will cause the database and redis data to be inconsistent. At this time, you can Put the operation of updating data into the queue, and the serialization operation will not occur, but this is generally not recommended because it will reduce efficiency.
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of Let’s talk about cache penetration, cache avalanche, cache breakdown and cache consistency in Redis. For more information, please follow other related articles on the PHP Chinese website!