当前位置:网站首页>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
边栏推荐
- In depth analysis of kubernetes controller runtime
- Common interview questions
- Alibaba cloud Alipay sandbox payment
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- The win7 computer can't start. Turn the CPU fan and stop it
- Kubesphere - set up redis cluster
- Disruptor learning notes: basic use, core concepts and principles
- In depth learning
- Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
- Difference between shortest path and minimum spanning tree
猜你喜欢

深入解析kubernetes controller-runtime

Deep learning, thinking from one dimensional input to multi-dimensional feature input

Core principles and source code analysis of disruptor

Maximum likelihood estimation, divergence, cross entropy

Cesium 点击获取模型表面经纬度高程坐标(三维坐标)

Project summary --04

Kubesphere - Multi tenant management

Kubernetes notes (II) pod usage notes

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

Zhiniu stock -- 03
随机推荐
ThreadLocal的简单理解
phpstudy设置项目可以由局域网的其他电脑可以访问
Kubernetes notes (VI) kubernetes storage
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Cesium entity (entities) entity deletion method
BeanDefinitionRegistryPostProcessor
Simple solution of small up main lottery in station B
YOLOV3学习笔记
技术管理进阶——你了解成长的全貌吗?
Kubernetes notes (V) configuration management
Mysql database
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
How to create and configure ZABBIX
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
Openresty best practices
Exportation et importation de tables de bibliothèque avec binaires MySQL
Kubesphere - Multi tenant management
Code generator - single table query crud - generator
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Mysql5.7 group by error