当前位置:网站首页>Redis summary
Redis summary
2022-07-07 08:37:00 【Silent flowers bloom】
Objective record
What is? Redis
Redis(Remote Dictionary Server) It's a use C language-written , Open source (BSD The license ) High performance non relational (NoSQL) Key value to database .
Redis You can store mappings between keys and five different types of values . The key type can only be string , Value supports five data types : character string 、 list 、 aggregate 、 Hash table 、 Ordered set .
redis Can handle over per second 10 Ten thousand read and write operations , Is the fastest known Key-Value DB. in addition redis It is also commonly used as a distributed lock .
Redis Why fast ?
- Completely based on memory , Most requests are purely memory operations , Very fast .
At the same time, the data is stored in memory , Be similar to HashMap,HashMap The advantage is the time complexity of both the lookup and the operation O(1); - The data structure is simple , It's also easy to manipulate data ,Redis The data structure in is specially designed ;
- Using single thread .
Unnecessary context switches and race conditions are avoided , There is no switching consumption caused by multiple processes or multiple threads CPU, You don't have to worry about locks , There is no lock release operation , There is no performance penalty due to possible deadlocks ; - Using multiple channels I/O Reuse model , Non blocking IO;
- Using the underlying model is different .
The underlying implementation and application protocols for communicating with clients are different between them ,Redis Build it yourself VM Mechanism , Because the normal system calls system functions , It's a waste of time moving and requesting ;
Redis What are the common data types ?
Redis There are mainly 5 Type of data , Include String,List,Set,Zset,Hash
String ( character string 、 Integer or floating point ): Do a simple key value pair cache
List list : Store some list data structures
set unordered set : intersection 、 Combine 、 Operation of difference set
hash Unordered hash table with key value pairs : Structured data , Like an object
zset Ordered set : De duplication but sorting
Redis RDB and AOF The difference between persistence , How to choose ?
What is? Redis Persistence ?
Persistence is to write data in memory to disk , Prevent memory data loss due to service outage .
Redis What is the persistence mechanism of ?
Redis There are two persistence mechanisms RDB( Default ) and AOF Mechanism :
RDB:Redis DataBase, Generate a snapshot of the current data and save it on the hard disk .
RDB yes Redis Default persistence method . Save the memory data to the hard disk in the form of snapshot according to a certain time , The corresponding generated data file is dump.rdb. Through... In the configuration file save Parameter to define the period .AOF:Append Only File Persistence , Record every data operation on the hard disk .
take Redis Each write command executed is recorded in a separate log file , When restarting Redis It will recover the data from the persistent log again .
RDB and AOF Advantages and disadvantages
- AOF File than RDB High update frequency , priority of use AOF Restore data .
- AOF Than RDB Safer and bigger
- RDB Performance ratio AOF good
- If both have priority loading AOF
How to choose
1、 Generally speaking , If you want to achieve enough PostgreSQL Data security of , Two persistence functions should be used at the same time . under these circumstances , When Redis It will be loaded prior to restart AOF File to restore the original data , Because in general AOF The data set saved by the file is better than RDB The data set of the file should be complete .
2、 If it can withstand data loss within a few minutes , Then you can only use RDB Persistence .
3、 Use only AOF Persistence , But it is not recommended . Because of timing generation RDB snapshot (snapshot) Very convenient for database backup , also RDB Data set recovery is also faster than AOF Fast recovery , besides , Use RDB You can also avoid AOF programmatic bug.
4、 If you only want the data to exist when the server is running , You can do it without any persistence .
How to solve cache breakdown 、 Cache penetration 、 Avalanche problem ?
Cache breakdown
Cache breakdown refers to data not in the cache but in the database ( Generally, the cache time expires ), At this time, there are many concurrent users , At the same time, the read cache does not read the data , At the same time go to the database to get data , Causes the database pressure to increase instantaneously , Cause too much pressure .
Unlike the cache avalanche , Cache breakdown refers to concurrent query of the same data , The cache avalanche is that different data has expired , A lot of data can't be found to look up the database .
Solution
- Set hotspot data never to expire .
- Add mutex lock
Cache penetration
Make a request for some data that must not exist , The request will penetrate the cache and reach the database .
Cache penetration refers to data that does not exist in cache or database , Causes all requests to fall to the database , Cause the database to bear a large number of requests in a short time and crash .
Solution
- Cache an empty data for these nonexistent data ;
- Filtering such requests .
Cache avalanche
Because the data is not loaded into the cache , Or cache data fails in a large area at the same time ( Be overdue ), Or the cache server is down , This results in a large number of requests reaching the database .
In a system with a cache , The system relies heavily on caching , The cache shares a large portion of the data requests . When a cache avalanche occurs , The database can't handle such a large request , Cause database crash .
Solution :
- In order to prevent cache avalanche caused by cache expiration in a large area at the same time , By observing user behavior , Set the cache expiration time reasonably
( The expiration time of cache data is set randomly , Prevent a large number of data expiration at the same time .) - In general, concurrency is not particularly large , The most commonly used solution is lock queuing
- Add the corresponding cache mark to each cache data , Whether the record cache is invalid , If the cache token fails , Then update the data cache .
- In order to prevent cache avalanche caused by cache server downtime , You can use distributed caching , In the distributed cache, each node only caches part of the data , When a node goes down, it can ensure that the cache of other nodes is still available .
- You can also warm up the cache , Avoid the cache avalanche caused by not caching a large amount of data soon after the system starts .
边栏推荐
- You should use Google related products with caution
- 测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
- rsync远程同步
- 归并排序和非比较排序
- 更改当前文件夹及文件夹下文件日期shell脚本
- Virtual address space
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- 单场带货涨粉10万,农村主播竟将男装卖爆单?
- Required String parameter ‘XXX‘ is not present
- Input and output of floating point data (C language)
猜你喜欢
Input and output of floating point data (C language)
Componentspace2022, assertions, protocols, bindings, and configuration files
Practice of implementing cloud native Devops based on rainbow library app
Automatic upgrading of database structure in rainbow
详解华为应用市场2022年逐步减少32位包体上架应用和策略
归并排序和非比较排序
2-3 lookup tree
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
下载和安装orcale database11.2.0.4
【无标题】
随机推荐
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Qt Charts使用(重写QChartView,实现一些自定义功能)
ES6_ Arrow function
Opencv learning notes 1 -- several methods of reading images
登山小分队(dfs)
Deit learning notes
Composer change domestic image
Golan idea IntelliJ cannot input Chinese characters
Leetcode 1984. Minimum difference in student scores
2-3查找树
MES system is a necessary choice for enterprise production
POJ - 3616 Milking Time(DP+LIS)
Laravel8 uses passport login and JWT (generate token)
Mock.js用法详解
Analysis of maker education in innovative education system
Data type - integer (C language)
JS的操作
go写一个在一定时间内运行的程序
Virtual address space
JEditableTable的使用技巧