当前位置:网站首页>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
边栏推荐
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Disruptor learning notes: basic use, core concepts and principles
- Virtual memory technology sharing
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- Pytorch builds the simplest version of neural network
- Mysql database
- .NET程序配置文件操作(ini,cfg,config)
- Project summary --2 (basic use of jsup)
- PMP notes
- 智牛股项目--04
猜你喜欢

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface

Zhiniu stock -- 03

SVN分支管理

GPS坐标转百度地图坐标的方法

Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency

技术管理进阶——你了解成长的全貌吗?

Merge and migrate data from small data volume, sub database and sub table Mysql to tidb

Pytorch dataloader implements minibatch (incomplete)

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

ThreadLocal的简单理解
随机推荐
Migrate data from Amazon aurora to tidb
Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
JMeter performance automation test
Core principles and source code analysis of disruptor
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
Mysql database
Phpstudy setting items can be accessed by other computers on the LAN
phpstudy设置项目可以由局域网的其他电脑可以访问
Use abp Zero builds a third-party login module (I): Principles
Apifix installation
智牛股项目--04
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
项目总结--01(接口的增删改查;多线程的使用)
Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
Jedis source code analysis (II): jediscluster module source code analysis
In depth analysis of kubernetes controller runtime
Loss function in pytorch multi classification
Openresty best practices
Kubernetes cluster environment construction & Deployment dashboard