当前位置:网站首页>Redis cache and existing problems--cache penetration, cache avalanche, cache breakdown and solutions
Redis cache and existing problems--cache penetration, cache avalanche, cache breakdown and solutions
2022-08-05 08:07:00 【Wind wind】
Redis cache and existing problems - cache penetration, cache avalanche, cache breakdown and solutions
Redis cache
Cache is a buffer for data exchange, a temporary place to store data, and generally has high read and write performance.
Why use a Redis cache?Because the IO speed of ordinary disk-based databases is too slow compared to business needs, Redis (memory-based database) can be used as cache middleware to store frequently accessed data in the database into Redis.Improve database access speed to meet business needs.
Using Redis cache can reduce backend load, improve read and write efficiency, and reduce access time.
Use Redis for caching, generally as shown below:
When the client has a data request, it will first access Redis. If it exists in Redis, it will return directly. If it does not exist, it will request the database, and the data will write the returned data to Redis, which is convenient for the next access., and return the data to the client.
Cache Update Policy
- Low consistency requirements: Use the memory elimination mechanism that comes with Redis.
- High consistency requirements
Read operation: If the cache hits, return directly; if the cache misses, query the database, write to the cache, and set the timeout time.
Write operation: Write the database first, then update the cache.To ensure the atomicity of database operations and cache operations.
Notes on using Redis cache
Cache penetration
Cache penetration means that the data requested by the client does not exist in the cache and the database, so the cache will never take effect, and these requests will hit the database, and such requests will be continuously initiated, which will bring huge pressure to the database.
Solution:
1. Cache empty objects
For non-existent data, also create a cache in Redis, the value is empty, and set a relativelyShort TTL time.
Advantages: Simple implementation and easy maintenance;
Disadvantages: Additional memory consumption; there will be short-term data inconsistencies.
2. Bloom filtering
Using the Bloom filtering algorithm, first determine whether the request exists before entering Redis, and if it does not exist, the request is rejected directly.
Benefits: Low memory usage.
Disadvantages: Complex implementation; potential for misjudgment.
3. Other ways
- Do the basic format verification of the data;
- Enhanced user permission verification;
- Do a good job of limiting the current of hotspot parameters.
Cache Avalanche
At the same time, a large number of cache keys are invalid at the same time or the Redis service is down, resulting in a large number of requests reaching the database, bringing huge pressure.We can look for a solution based on the described problem.
Workaround:
- Add random values to the TTL of different keys;
- Using Redis Cluster to improve service availability;
- Add downgrade current limiting policy to cache service;
- Add multi-level cache to the business;
Cache breakdown
The cache breakdown problem is also called the hot key problem, which is a sudden failure of a key that is accessed by high concurrent access and the cache reconstruction service is more complicatedNow, countless requests to access will bring a huge impact to the database in an instant.
Solution:
1. Mutual exclusion lock
Because when the hotspot key suddenly expires, there will be multiple requests to access the database at the same time, withIt has a huge impact on the database, so only one request needs to access the database and rebuild the cache, and other requests can access the cache after the cache is rebuilt.This can be achieved by adding a mutex.That is, lock the cache reconstruction process to ensure that only one thread executes the reconstruction process and other threads wait.
Advantages: Simple implementation; no extra memory consumption; good consistency.
Disadvantages: Slow performance due to waiting; risk of deadlock.
2. Set the hotspot key to never expire, that is, set the logical expiration time.
The hotspot key cache never expires, but a logical expiration time must be set. When the data is queried, the logical expiration time is judged to determine whether the cache needs to be rebuilt.**Rebuilding the cache also guarantees single-threaded execution through mutex locks.Rebuilding the cache is performed asynchronously using a separate thread.Other threads do not need to wait, just query the old data directly.
Advantages: The thread does not need to wait, and the performance is better.
Disadvantages: No consistency guarantee (weak consistency); additional memory consumption; complex implementation.
边栏推荐
猜你喜欢
随机推荐
随机码的生成
星座理想情人
线性代数对角化
Access Denied: "microsoft.web.ui.webcontrols" workaround
网络安全研究发现,P2E项目遭遇黑客攻击只是时间问题
力扣每日一题
七夕看什么电影好?爬取电影评分并存入csv文件
【 LeetCode 】 235. A binary search tree in recent common ancestor
Controlling number and letter input in ASP
在ASP控制数字及字母输入
本地能ping通虚拟机,虚拟机ping不通本地
执子之手,与子偕老。你同意么?
Qt writes custom controls: one of the text spotlight effects
Codeforce 8.1-8.7做题记录
Version number naming convention
数据源对象管理Druid和c3p0
php向mysql写入数据失败
向美国人学习“如何快乐”
2006年星座运势全解-射手
[Structural Internal Power Cultivation] Structural Realization Stages (2)









