当前位置:网站首页>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.】
List of articles
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 :
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 
Cache avalanches are many key Collective failure 
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 .
redisson Use
边栏推荐
- 认识map
- Blob
- Electronic training.
- 制作3D浪漫炫酷相册【附源码】
- 数字电路——加法器
- New library launched | cnopendata wholesale price data of agricultural products
- Mpu6050 reads the ID incorrectly and 0xd1 occurs (the correct ID should be 0x68 or 0x69). Solution.
- vite打包构建时 @charset utf-8警告问题处理;
- Establish a j-link GDB cross debugging environment for Px4
- 213. house raiding II
猜你喜欢

Installation and startup of redis

C#使用MySql进行操作

填鸭数据即时收集解决方案资源

Music spectrum display toy -- implementation and application of FFT in stm32

使用Gin框架运行Demo时报错“ listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden”

vite打包构建时 @charset utf-8警告问题处理;

New library launched | cnopendata China new house information data

黑盒测试 — 测试用例 之 判定表法看这一篇就够了

统一网关Gateway

DGUS新升级:全面支持数字视频播放功能
随机推荐
接口的幂等性——详细谈谈接口的幂等即解决方案
Sqlserver is case sensitive
Blob
Nacos registry
mysql错误代码2003的解决办法
Sword finger offer II 096 String interleaving
新库上线 | CnOpenData中国新房信息数据
.net使用Access 2010数据库
PHP performance optimization
ETCD数据库源码分析——集群通信初始化
When you run the demo using the gin framework, there is an error "listen TCP: 8080: bind: an attempt was made to access a socket in a way forbidden"
Middle order clue binary tree
The maze of God's perspective in robot vision
Phoenix index
黑盒测试 — 测试用例 之 判定表法看这一篇就够了
mysql
Zhihuijia - full furniture function
Mpu6050 reads the ID incorrectly and 0xd1 occurs (the correct ID should be 0x68 or 0x69). Solution.
Multiple interface calls, using promise all、Promise. Race and promise any
debezium