当前位置:网站首页>Important knowledge points of redis
Important knowledge points of redis
2022-07-03 06:14:00 【Muyu】
Redis Important knowledge points
Cache penetration 、 An avalanche 、 Cache breakdown
Cache penetration
describe : A lot of data that is not in the query cache or in the database , It will lead to a larger amount of database access , Be prone to problems .
resolvent : Encounter data with no value in the cache and database , Just set a null value in the cache , And set an expiration time , In this way, there will be no massive access to the database . And when there is this value in the database , Because the expiration time exists , The cache will also update the correct value .
An avalanche
describe : In the cache , Large area data , At the same time , At this time, the pressure on the database will soar , If it's serious , The database could crash
terms of settlement : When setting the data expiration time , You can use random numbers to set , This can effectively avoid data expiration at the same time .
Cache breakdown
describe : There are some hot data , The number of visits is very large , When this data expires , There will be many requests to access the database at the same time , The pressure on the database has increased .
terms of settlement : This situation can lock the method of accessing the database , Only one request is allowed to access the database at a time , Check the cache again after other requests get locks , If it's worth it, just return.
Local cache and distributed cache
Local cache
Do not do distributed
Attention to development : Cache penetration and avalanche problems are easier to solve ,
Cache penetration directly sets one for the data that is not found in the database null perhaps -1 And so on .
Avalanches need to be set Set a random expiration time when .
Cache breakdown requires locking the code , The locked position can be used
public String getValue(){ if( Not in cache ){ // Lock the object synchronized(this){ // After getting the lock, judge whether there is data in the cache if( Not in cache ){ // Query the database }else{ return val; } } }else{ // Return value return val; } } // You can also use the method of locking the method public synchronized void setValue2(){ ... }It should be noted that , Put the value found in the database into the cache , Release the lock again .
Distributed lock
In the case of distributed , Each server locks its internal container , If you lock in the way of local cache , So how many servers , How many times will you access the database . Under normal circumstances , That's enough , It won't bring much pressure to the database , But if there are special needs , Then you need to use distributed locks .
Problem analysis : The reason why the local lock does not work is that the containers of each server are not interconnected , We can't visit each other , So we need to use a public space to lock , We can use this public space redis.
Solution :

When you want to lock, you are redis Save one in key by lock Field of ( Customizable ), And set up key Take with you NX, This is the only way key Set when it doesn't exist key Value , When there are multiple requests at the same time set lock When , Only one will take effect , This serves as a lock .
set lock You should also add an expiration time when , This time needs set Methodical EX perhaps PX To set up ( Guaranteed atomicity ), Prevent code from executing downward in extreme cases ( For example, power outage. , So even if you use catch Nor can you release the lock ), Deadlock caused by .
lock Of value It needs to be set to a random number , such as UUID, In this way, when the business ends and the lock needs to be released , Can be judged , If it's your own lock , Let go , Otherwise, it is easy to release the locks of other threads ( It may take too long to perform business , After the time limit , The lock will automatically release )
Delete lock You need to use the official script to delete , To ensure atomicity
if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end

This is a java An example of the usage in

The above is a summary of the video of Lei Fengyang in zishang Silicon Valley , If you are interested, you can watch the original video .
This part is 50-57 speak
边栏推荐
- Zhiniu stock project -- 04
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- Kubernetes notes (VIII) kubernetes security
- BeanDefinitionRegistryPostProcessor
- Merge and migrate data from small data volume, sub database and sub table Mysql to tidb
- 表达式的动态解析和计算,Flee用起来真香
- PMP笔记记录
- Method of converting GPS coordinates to Baidu map coordinates
- Common interview questions
- Oauth2.0 - explanation of simplified mode, password mode and client mode
猜你喜欢

Kubernetes notes (IX) kubernetes application encapsulation and expansion

Es remote cluster configuration and cross cluster search

【系统设计】邻近服务

Migrate data from Mysql to tidb from a small amount of data

Creating postgre enterprise database by ArcGIS

Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation

Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)

Mysql

Phpstudy setting items can be accessed by other computers on the LAN

Kubernetes cluster environment construction & Deployment dashboard
随机推荐
Creating postgre enterprise database by ArcGIS
Sorry, this user does not exist!
Migrate data from Mysql to tidb from a small amount of data
Zhiniu stock project -- 05
88. 合并两个有序数组
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
1. Somme des deux nombres
剖析虚幻渲染体系(16)- 图形驱动的秘密
Characteristics and isolation level of database
Oauth2.0 - user defined mode authorization - SMS verification code login
IE browser flash back, automatically open edge browser
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Core principles and source code analysis of disruptor
MySQL帶二進制的庫錶導出導入
MySQL带二进制的库表导出导入
Mysql5.7 group by error
Pytorch builds the simplest version of neural network
智牛股--03
Common interview questions
Cesium entity (entities) entity deletion method