当前位置:网站首页>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
边栏推荐
- Leetcode solution - 02 Add Two Numbers
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- 技术管理进阶——你了解成长的全貌吗?
- Mysql database
- Convolution operation in convolution neural network CNN
- PMP notes
- Migrate data from Amazon aurora to tidb
- Cesium entity (entities) entity deletion method
- Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
- CKA certification notes - CKA certification experience post
猜你喜欢

tabbar的设置

ruoyi接口权限校验

有意思的鼠标指针交互探究

Oauth2.0 - use database to store client information and authorization code

Kubernetes notes (VII) kuberetes scheduling

Kubesphere - Multi tenant management

Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)

Kubernetes notes (III) controller

表达式的动态解析和计算,Flee用起来真香

Code generator - single table query crud - generator
随机推荐
Deep learning, thinking from one dimensional input to multi-dimensional feature input
Oauth2.0 - user defined mode authorization - SMS verification code login
Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
项目总结--2(Jsoup的基本使用)
Zhiniu stock project -- 05
conda和pip的区别
Loss function in pytorch multi classification
JMeter performance automation test
Oracle Database Introduction
Naive Bayes in machine learning
It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
Disruptor learning notes: basic use, core concepts and principles
技术管理进阶——你了解成长的全貌吗?
[system design] proximity service
Why should there be a firewall? This time xiaowai has something to say!!!
代码管理工具
Mysql database binlog log enable record
1. Somme des deux nombres
Code generator - single table query crud - generator
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)