当前位置:网站首页>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
边栏推荐
猜你喜欢
Es remote cluster configuration and cross cluster search
In depth analysis of kubernetes controller runtime
Kubesphere - build Nacos cluster
YOLOV3学习笔记
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
Tabbar settings
Une exploration intéressante de l'interaction souris - pointeur
Alibaba cloud OOS file upload
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
tabbar的设置
随机推荐
Kubernetes notes (VI) kubernetes storage
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Kubernetes notes (IX) kubernetes application encapsulation and expansion
ruoyi接口权限校验
智牛股项目--05
conda和pip的区别
致即将毕业大学生的一封信
ODL framework project construction trial -demo
YOLOV3学习笔记
Difference between shortest path and minimum spanning tree
技术管理进阶——你了解成长的全貌吗?
Some thoughts on machine learning
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Zhiniu stock project -- 04
Pdf files can only print out the first page
Oauth2.0 - explanation of simplified mode, password mode and client mode
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
arcgis创建postgre企业级数据库
Introduction to software engineering
BeanDefinitionRegistryPostProcessor