当前位置:网站首页>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 :
 Insert picture description here

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 .

 Insert picture description here
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 .
原网站

版权声明
本文为[csx_ zzh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101054353283.html