当前位置:网站首页>From query database performance optimization to redis cache - talk about cache penetration, avalanche and breakdown

From query database performance optimization to redis cache - talk about cache penetration, avalanche and breakdown

2022-06-26 01:10:00 Bald and weak.

Write it at the front

Realize the functions required by the page , But considering that this page is accessed by users frequently , So performance must be optimized as much as possible .

Generally, the biggest performance bottleneck of a system , It's the database io operation . Starting from the database is also the most cost-effective starting point for tuning .

It is generally divided into two levels , One is to improve the database sql Its own performance , Second, try to avoid directly querying the database .

To improve the performance of the database itself, the first step is to optimize sql, Include : Use index , Reduce the number of unnecessary large table associations , Control the number of rows and columns of the query field . In addition, when the amount of data is huge, we can consider sub database and sub table , To reduce single point pressure . about mysql The optimization of the , Please study by yourself , No more details here .
MySQL senior - What is an index ?explain How to use it -MySQL Query optimization encyclopedia

The focus is on another level : Try to avoid directly querying the database .

The solution is : cache .
Caching can be understood as an umbrella for databases , Any request that can hit the cache , Will not directly access the database . The processing performance of cache is database 10-100 times .
What we are talking about here is using Redis Optimize as a caching system .

chart :
 Insert picture description here

Problems that may occur during high concurrency

However, there are three problems in the high concurrency environment .

1、 If redis It's down. , Or the link is not available , What do I do ?
2、 If redis Cache expires during peak periods , At this moment, the request will be like an avalanche , How to directly access the database ?
3、 If the user keeps querying a piece of data that doesn't exist , The cache does not , The database doesn't either , So what happens , How to deal with it ?

Cache penetration

Cache penetration refers to querying a certain nonexistent data , Because the cache is a miss , Will query the database , But there is no such record in the database , And in the consideration of fault tolerance , We didn't send this query to the null Write cache , This will cause the non-existent data to be queried by the storage layer every time it is requested , It loses the meaning of caching . When the flow is large , Probably DB It's gone , If someone takes advantage of something that doesn't exist key Attack our applications frequently , This is the loophole .

solve :
Cache empty results , But its expiration time will be very short , Up to five minutes .

Cache avalanche

Cache avalanche is when we set up the cache with the same expiration time , Causes the cache to fail at the same time at a certain time , Forward all requests to DB,DB Avalanche with excessive instantaneous pressure .

solve :
Add a random value to the original failure time , such as 1-5 Minutes at random , In this way, the repetition rate of each cache expiration time will be reduced , It's hard to trigger a collective failure .

Cache breakdown

For some with expiration set key, If these key It may be accessed at some point in time with super high concurrency , It's a very “ hotspot ” The data of . This is the time , A question needs to be considered : If this key Just before a large number of requests come in at the same time , So all about this key All data queries fall into db, We call it cache breakdown . And cache avalanche :

Cache breakdown is a hot spot key invalid
 Insert picture description here
Cache avalanches are many key Collective failure
 Insert picture description here
When the cache expires at a certain point in time , Right at this point in time Key There are a lot of concurrent requests coming , These requests usually find that the cache is expired from the back end DB Load data and reset to cache , At this time, a large number of concurrent requests may instantly put the back end DB Overwhelmed .

solve :
Distributed lock —— first key1 In the query db, get data Into the cache process , They all have a lock Lock it first , Others have to wait , Wait for this person to set the cache successfully , To release the lock , Then other people get data directly from the cache ; Will not cause database read and write performance defects .

redisson

Redisson It's a Redis On the basis of implementation Java In memory data grid (In-Memory Data Grid). It not only provides a series of distributed Java Common objects , There are also many distributed services . These include (BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service) Redisson Provides the use of Redis The simplest and most convenient way .Redisson The aim is to promote the user's understanding of Redis Separation of concerns (Separation of Concern), This allows users to focus more on processing business logic .
 Insert picture description here

redisson Use

How to use redis Implement distributed locks ? This article teaches you to use redisson Implement distributed locks , The encapsulated method is better !

原网站

版权声明
本文为[Bald and weak.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252304385142.html