Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The advantages of redis cache and the solution of cache penetration

2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the advantages of redis cache and the solution to cache penetration". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the advantages of redis cache and the solution to cache penetration.

Advantages and disadvantages of caching and its application scenarios

Here we mainly discuss the memory-based caching scheme represented by Redis.

Advantages of caching

Improve access speed and reduce time consumption of back-end storage such as database

Relieve the pressure on the back end such as the database

Problems caused by caching

Every time a component is added to any system, it will inevitably bring additional complexity while bringing new features. It can be said that the design process of the system is a compromise process. The introduction of caching also brings some issues to consider:

Data inconsistency: there is a certain time window inconsistency between the cache layer and the storage layer, which is related to the cache update strategy.

Code maintenance costs: the logic of both the cache and storage layers needs to be processed

Operation and maintenance cost: to ensure the availability and concurrency of redis, architectures such as redis sentinel or redis cluster will be introduced, which increases the complexity of the system and the difficulty of operation and maintenance.

Application scenario

Complex operations with high cost

Accelerate request response

Cache update policy

LRU/LFU/FIFO algorithm

Timeout culling

Active update

Application

Low consistency business recommendation: maximum memory + phase-out strategy

High consistency: timeout culling and active updates

Cache penetration

Cache traversal: refers to querying a data that does not exist at all, and neither the cache layer nor the storage layer will hit it. This can increase the pressure on the storage layer.

Discovery of cache penetration:

Usually it can be counted separately in the program.

Total calls

Number of cache layer hits

Number of storage layer hits

If a large number of storage layer misses are found, there may be a cache penetration problem.

Solution of cache traversal

Cache empty object

Memory consumption: the reason is that in order to prevent a large number of empty objects (being attacked), the scheme can set a relatively short expiration time to allow it to be automatically removed.

Data inconsistency: the reason is that data has been added to the storage layer, but the cache empty object has not expired. The solution is to use message queuing.

Bloomfilter intercept

This method is suitable for the application scenarios where the data hit is not high, the data is relatively fixed, and the real-time performance is low (usually a large data set). The code maintenance is more complex, but the cache space is less.

Bottomless pit optimization

Because the cache cluster usually hash the key and then map it to the corresponding nodes, the distribution of key has nothing to do with business, and batch operations usually need to be obtained from different nodes. Compared with stand-alone batch operations, distributed batch operations involve multiple network operations.

Common IO optimization ideas:

Optimization of the command itself, such as optimizing SQL statements, etc.

Reduce the number of network communications

Pipeline

Mget

Reduce intervention costs, such as client use of persistent connections / connection pooling, NIO, etc.

Cluster client optimization scheme

Serial IO, grouping key requests by nodes, and then processing them in turn

Parallel IO, grouping key requests by nodes, and then processing them in parallel

Hash_tag implementation, multiple key can be forcibly assigned to a node, its operation time = 1 this network time + n command time, the highest performance, but the data maintenance cost is high, the data is easy to tilt

Avalanche optimization

Avalanche definition: because the cache layer carries a large number of requests, the storage layer is effectively saved, but if the cache layer cannot provide services for some reason, then all requests will reach the storage layer, and calls to the storage layer will surge.

In the final analysis, the cache can't handle it, putting pressure on the storage layer.

Three aspects of preventing and alleviating the problem of caching avalanche

To ensure high availability of cache layer services, redis provides Redis Sentinel and Redis Cluster

Dependency isolation components for backend current limiting and downgrading, degraded mechanisms such as Hystrix

Drill in advance, before the project goes online, after the cache layer is down, the load of the application and backend and the problems that may occur.

Hot spot key reconstruction optimization

The strategy of cache + expiration time can not only accelerate data reading and writing, but also ensure that the data is updated regularly. This model can basically meet most of the needs. But there are two problems:

Hot spot key, the concurrency is very large

Rebuilding the cache cannot be completed in a short time (generate the cache in a long time)

Hot key reconstruction

Mutex lock

Never expires, but there will be inconsistencies during reconstruction

At this point, I believe you have a deeper understanding of "the advantages of redis cache and the solution of cache penetration". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report