当前位置:网站首页>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
边栏推荐
- Freertos+stm32l+esp8266+mqtt protocol transmits temperature and humidity data to Tencent cloud IOT platform
- Endnote IEEE Transactions on industrial electronics/tie/tpel reference format template
- Typescript for Web Learning
- 原生DOM与虚拟DOM
- Implementation notes of least square fitting conic in stm32
- ADC acquisition noise and comparison between RMS filter and Kalman filter
- 2022年电气试验考试试题模拟考试平台操作
- C # operate with MySQL
- C#使用MySql进行操作
- Unified gateway
猜你喜欢

.net使用Access 2010数据库

ADC acquisition noise and comparison between RMS filter and Kalman filter

Case: drawing Matplotlib dynamic graph

下载安装Flume

Binary sort tree

Vscode shortcut

Implementation notes of least square fitting conic in stm32

Px4 system terminal for pixhawk

智慧家——全家具功能

Return value is object type method call equals()
随机推荐
A sharp tool for information collection, Google hacker syntax
mysql cluster
Inheritance -- holy grail mode
Design and process analysis of anti backflow circuit for MOS transistor
LabVIEW开发监控聚变实验脉冲电源
在线小工具分享(不定时更新,当前数量:2)
Px4 system terminal for pixhawk
Middle order clue binary tree
Final review [machine learning]
Phoenix index
jarvisoj_ level2_ x64
Casually painted
Unified gateway
Kylin
JS reverse case: cracking login password
C thread pool control semaphore
关于EF翻页查询数据库
马斯克 VS 乔布斯,谁是21世纪最伟大的创业家
Data analysis slicer, PivotTable and PivotChart (necessary in the workplace)
C#使用MySql进行操作