当前位置:网站首页>Detailed explanation of redis
Detailed explanation of redis
2022-06-10 11:09:00 【csx_ zzh】
1.Redis
1.1 Redis What can you do with it ?
1. It can be used as a database . Like it , Focus on , Ranking and other Internet demand with high performance requirements
2. It is the longest used for caching , It is the first middleware to realize distributed cache .
3. It can be used as a computing tool , It can calculate the number of online days and other data at very little cost .
4. It can also implement distributed locks , It can be used as a message queue .
1.2 Redis How is it different from traditional relational database ?
redis It's based on key value pairs NoSQL database , The values of key value pairs have a variety of data structures and algorithms
redis Store in memory , So its speed is amazing , Far beyond relational databases .
Relational database is based on two-dimensional data table to store data , His data format is more rigorous , And support relationship query
The data of the relational database is stored on the hard disk , Can store a lot of data , But his performance is not redis Powerful .
1.3 Redis What are the data types ?
1.redis Support 5 Core data types . They are lists , character string , Hash , aggregate , Ordered set .
2.redis It also provides Geo,Bitmap,HyperLogLog etc. . They all have the top 5 Three core data types .
3.redis stay 5.0 The new increased Streams data type , He is a powerful , Support multicast , Persistent message queue .
1.4 Redis It's single threaded , Why can it be so fast ?
1.redis Most of the operations of are implemented in memory .
2. For server programs , Switching threads and locks is often a performance killer , Single thread avoids the consumption caused by thread switching and competition .
3.redis stay IO Multiplexing mechanism . Make it on the Internet IO It can handle a large number of client requests concurrently , Achieve high throughput .
About Redis Single thread architecture implementation , Here's the picture :
1.5 Redis In persistence fork Make a sub process , There are already two processes , How can we say it's a single thread ?
redis It's single threaded , It mainly refers to the network IO There is a thread to read and write key value pairs ,
and Redis Other features of, such as persistence , Delete asynchronously , Cluster data synchronization, etc , It depends on other threads to execute
So redis Single threaded is just a way of saying , Its bottom implementation is not single threaded .
1.6 set and zset What's the difference? ?
set
1. Elements in a collection are unordered , Non repeatable . At most... Can be saved in a collection 2^32-1 Elements .
2. In addition to supporting the addition, deletion, modification and query of elements , It also supports the intersection of multiple sets , Combine , Difference set .
zset
1. An ordered set retains the characteristic that elements are not repeatable .
2. An ordered set gives each element a score , And take this as the basis for sorting .
3. Ordered collections cannot contain the same elements , But the element scores can be the same .
1.7 The way Redis Medium watch command
A lot of times , Ensure that the data in the transaction has not been modified by other clients before executing the transaction .redis Provides watch Command to solve such problems .
This is an optimistic lock mechanism , Client pass watch command , Require the server to one or more key To monitor .
If the client before executing the transaction , these key The value has changed , The server will refuse to execute the transaction submitted by the client , And return a null value to him .
1.8 say something Redis in List Structure related operations
Lists are linearly ordered data structures , Its internal elements can be repeated , And a list can store at most 2^32-1 Elements .
List of common commands :
lpop/rpop : Pop up an element from the left or right side of the list .
lpush/rpush : Add data from the left or right side of the list .
lindex : Returns the data at the specified index .
lrange : Specify the index range , And return the data in this range .
blpop/brpop : Pop up an element from the left or right side of the list , If the list is empty , Then enter the blocking state .
1.9 How do you design Redis The expiration time of ?
1. No expiration time is set for hot data . To achieve " Physics " The on the never expires , So as to avoid the problem of cache breakdown .
2. When setting the expiration time , Add a random number , Avoid a lot of key At the same time , Cause cache avalanche .
1.10 Redis in ,sexnx What is the return value of the command , How to use this command to realize distributed locking ?
sexnx It returns the integer type , return 1 Indicates that the setting of the value is successful . return 0 Indicates that setting the value failed (key Already exist ).
Generally, we do not recommend using sexne To implement distributed locking . In order to avoid deadlock , We need to set an expiration time for the lock .
and sexnx The command and the command to set the expiration time are not atomic . Locking may be successful , Failed to set the expiration time , There is still a potential deadlock .
Yes, with this situation ,redis Improved set command , Added to him nx Options , Enable this option set The effect of the command is just like setnx The same .
use redis Implement distributed locks , Is in the redis A piece of data representing the lock in Limun , Usually just use a string . After improvement setnx To achieve .
problem :
It looks perfect , But in fact, there are hidden dangers , Here's the picture . process A When the task is not completed , The lock has expired and has been released . Wait for the process A After the execution of the task , It will still try to release the lock , Because its code logic is to release the lock after the task is completed . however , Its lock has already been automatically released , At this time, it may release the locks of other threads .

When locking, you should set an identification for the lock , The process should remember this logo . When the process is unlocked , To judge , It is the lock you hold that can be released , Otherwise, you can't release . It can be for key Give a random value , To act as the identity of the process .
When unlocking, judge first 、 Re release , These two steps need to ensure atomicity , Otherwise, if the second step fails , There will be deadlocks . The get and delete commands are not atomic , This requires the adoption of Lua Script , adopt Lua The script arranges the two commands together , And the whole Lua The execution of the script is atomic .
1.11 say Redis Persistence strategy of
redis Support RDB Persistence ,AOF Persistence ,RDB-AOF The three persistence methods of mixed persistence .
边栏推荐
- 使用ApiPost测试接口时需要先登录的接口怎么办(基于Cookie)?
- Leetcode18 subset problem
- Digital supply chain collaboration system for digital commerce cloud communication industry: empowering communication enterprises to improve supply business and enhance market competitiveness
- 高性能计算(2)——万丈高楼平地起
- 数商云通讯行业数字化供应链协同系统:赋能通讯企业改善供应业务,增强市场竞争力
- Yutai semiconductor rushes to the scientific innovation board: the annual revenue is 830million, and the actual controller is American
- 面经题目01
- How to choose cities, schools and majors for the college entrance examination?
- PV operation daily question - ticket sales
- How to state clearly and concisely the product requirements?
猜你喜欢

Fcpx plug-in: premiumvfx animation presets v1.0.1 Special Edition

Fortex方达发布电子交易生态体系 与客户共享共赢

在线文档协作工具,是提高工作效率的第一步

企评家分不同维度解析:湖南长城科技信息有限公司企业成长性

子网划分不会?瑞哥带你深入理解IP地址,手把手教你子网划分!

Jeu plus durable casque sans fil réel, batterie super longue durée de vie, héros G1 en main

单片机触发器或非门工作原理以及用途

杰理之BLE timer 时钟源不能选 OSC 晶振【篇】

Extending visual transformer to gigapixel images based on hierarchical self supervised learning

Nucleic acid detection robot
随机推荐
Jeu plus durable casque sans fil réel, batterie super longue durée de vie, héros G1 en main
kubernetes 设置 Master 可调度与不可调度
PV operation daily question - orange apple question (advanced version)
Vs code supports configuring remote synchronization
PV operation daily question - orange apple question (advanced version)
pytorch安装教程
【SignalR全套系列】之在.Net6中实现SignalR分组通信
【黄啊码】我用这个方式清理了谷歌浏览器的控制台
解析:稳定币不是「稳定的币」 其本质是一种产品
杰理之长按复位及高电平复位【篇】
PV operation daily question - ticket sales
Mit6.824-lab2d-2022 (detailed explanation of log compression)
Modstartcms enterprise content site building system (supporting laravel9) v4.1.0
牛客面经02
golang CLI框架--cobra
杰理之BLE功耗异常【篇】
Google Earth engine (GEE) - country identifier grid dataset
Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)
NoClassDefFoundError 和 ClassNotFoundException 有什么区别
常用颜色RGB、灰度值、取色值、透明度。