当前位置:网站首页>Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
2022-07-06 13:10:00 【Java misty rain】
Contents of this article :
1. Tell me what is Redis?
2.Redis What can be used for ?
3.Redis What are the data structures ?
4.Redis Why is it fast ?
5. Can you tell me I/O Multiplexing ?
6. Redis Why did you choose single thread early ?
7.Redis6.0 What's going on with multithreading ?
8.Redis Persistence ⽅ What are the formulas ? What's the difference? ?
9.RDB and AOF What are the advantages and disadvantages of each ?
10.RDB and AOF How to choose ?
11.Redis Data recovery ?
12.Redis 4.0 Hybrid persistence for, you know ?
13. Master slave replication understand ?
14.Redis There are several common topologies for master and slave ?
15.Redis Do you understand the principle of master-slave replication ?
16. Talk about the way of master-slave data synchronization ?
17. What are the problems with master-slave replication ?
18.Redis Sentinel( sentry ) Understand? ?
19.Redis Sentinel( sentry ) You know how it works ?
20. The leader Sentinel Do you understand ?
21. How the new master node is selected ?
22.Redis Do you understand ?
23. How to partition data in a cluster ?
24. Can you talk about Redis The principle of clustering ?
25. Talk about the scaling of clusters ?
26. What is cache breakdown 、 Cache penetration 、 Cache avalanche ?
27. Can you talk about the bloom filter ?
28. How to ensure the of cache and database data ⼀ Sexual nature ?
29. How to ensure the consistency between local cache and distributed cache ?
30. How to deal with heat key?
31. How to warm up the cache ?
32. hotspot key The reconstruction ? problem ? solve ?
33. Bottomless hole problem ? How to solve ?
34.Redis How to deal with insufficient memory ?
35.Redis What are your expired data recovery strategies ?
36.Redis What are the memory overflow controls / Memory retirement strategy ?
37.Redis Blocking ? How to solve ?
38. Big key Do you understand the problem ?
39.Redis Common performance problems and solutions ?
40. Use Redis How to implement asynchronous queues ?
41.Redis How to implement delay queue ?
42.Redis Do you support transactions ?
43.Redis and Lua Do you understand the use of scripts ?
44.Redis Do you understand the pipeline ?
45.Redis Implementation of distributed lock, understand ?
46. say something Redis Underlying data structure ?
47.Redis Of SDS and C What's the advantage of medium string ?
48. How the dictionary is realized ?Rehash Understand? ?
49. How the jump table is realized ? principle ?
50. Compress the list to understand ?
51. Quick list quicklist Understand? ?
52. If Redis There are 1 One hundred million key, Among them is 10w individual key It starts with a fixed known prefix , How to find them all ?
Text :
Basics
1. Tell me what is Redis?
Redis Icon
Redis It's based on key value pairs (key-value) Of NoSQL database .
More powerful than the general key value to the database ,Redis Medium value Support string( character string )、hash( Hash )、 list( list )、set( aggregate )、zset( Ordered set )、Bitmaps( Bitmap )、 HyperLogLog、GEO( Location of geographic information ) And other data structures , therefore Redis It can satisfy many application scenarios .
And because Redis Will store all data in memory , So its reading and writing performance is very excellent .
More Than This ,Redis The data in memory can also be saved to the hard disk in the form of snapshot and log , So in the event of a similar power failure or machine failure , Data in memory does not “ The loss of ”.
In addition to the above functions ,Redis Key expiration is also provided 、 Publish subscribe 、 Business 、 Assembly line 、Lua Additional functions such as scripts .
All in all ,Redis It is a powerful performance weapon .
2.Redis What can be used for ?
Redis
cache
This is a Redis Most widely used , Basically everything Web All apps will use it Redis As caching , To reduce data source pressure , Improve response time .
Counter Redis Natural support counting function , And the counting performance is very good , It can be used to record the number of views 、 Like and so on .
Ranking List Redis Provides list and ordered set data structures , Reasonable use of these data structures can be very convenient to build a variety of ranking system .
Social networks Fabulous / Step on 、 fans 、 Common friends / like 、 push 、 The drop-down refresh .
Message queue Redis It provides publish subscribe function and blocking queue function , It can meet the general message queue function .
Distributed lock Distributed environment , utilize Redis Implement distributed locks , It's also Redis Common applications .
Redis The application of is usually combined with the project to ask , Take the user service of an e-commerce project as an example :
Token Storage : After the user logs in successfully , Use Redis Storage Token
Login failure count : Use Redis Count , Login failed more than a certain number of times , Lock account
Address cache : Cache provincial and urban data
Distributed lock : Login in distributed environment 、 Registration and other operations plus distributed lock
……
3.Redis What are the data structures ?
Redis There are five basic data structures .
string
String is the most basic data structure . A value of type string can actually be a string ( Simple string 、 Complex string ( for example JSON、XML))、 Numbers ( Integers 、 Floating point numbers ), Even binary ( picture 、 Audio 、 video ), But the maximum value cannot exceed 512MB.
String mainly has the following typical usage scenarios :
Caching function
Count
share Session
The speed limit
hash
Hash type means that the key itself is a key value pair structure .
Hash mainly has the following typical application scenarios :
Cache user information
Cache object
list
list (list) Type is used to store multiple ordered strings . List is a more flexible data structure , It can act as a stack and a queue
The list mainly includes the following usage scenarios :
Message queue
The article lists
set
aggregate (set) Types are also used to hold multiple string elements , But it's not the same as the list type What it looks like is , Duplicate elements are not allowed in the collection , And the elements in the set are unordered .
The collection mainly includes the following usage scenarios :
label (tag)
Pay close attention to
sorted set
Elements in an ordered set can be ordered . But it's different from a list that uses index subscripts as sort criteria , It sets a weight for each element (score) Sort by .
The main application scenarios of ordered collection :
User likes Statistics
User sorting
4.Redis Why is it fast ?
Redis The speed of ⾮ Often fast , Standalone Redis Can ⽀ Support more than 100000 concurrent events per second , be relative to MySQL Come on , Performance is MySQL Of ⼏⼗ times . The main reasons for the speed are ⼏ spot :
Completely based on memory operations
send ⽤ Single thread , Avoid the consumption caused by thread switching and race state
be based on ⾮ Obstructed IO Multiplex ⽤ Mechanism
C language ⾔ Realization , Optimized data structure , be based on ⼏ A basic data structure ,redis Did ⼤ Quantity optimization , Extremely good performance ⾼
5. Can you tell me I/O Multiplexing ?
Quote Zhihu's last highly praised answer to explain what is I/O Multiplexing . Suppose you are a teacher , Give Way 30 Three students solve a problem , Then check if the students are doing it right , You have the following options :
The first option : Check one by one in order , First check A, And then there was B, And then C、D... If a student gets stuck in the middle , The whole class will be delayed . This model is like , You do it one by one in a loop socket, No concurrency at all .
The second option : You create 30 It's a split , Each doppelganger checks whether a student's answer is correct . This is similar to creating a process or... For each user - Thread processing connection .
The third option , You stand on the platform and wait , The one who answers raises his hand . At this time C、D raise hands , That means they are finished with their questions , You go down and check in turn C、D The answer , Then go back to the podium and wait . here E、A Raise your hand again , And deal with it E and A.
The first is blocking IO Model , The third is I/O Reuse model .
Multiplexing model
Linux The system can be implemented in three ways IO Multiplexing :select、poll and epoll.
for example epoll The way is to put the user socket Corresponding fd Sign up for epoll, then epoll Help you monitor what socket There's a message on the phone , This avoids a lot of useless operations . At this time socket Non-blocking mode should be used .
such , The whole process is only going on select、poll、epoll These calls only block , Sending and receiving customer messages is not blocked , The entire process or thread is fully utilized , This is event driven , So-called reactor Pattern .
6. Redis Why did you choose single thread early ?
Official explanation :https://redis.io/topics/faq
official FAQ Express , because Redis Is a memory based operation ,CPU Become Redis It's rare to see a bottleneck in the market ,Redis The most likely bottleneck is the size of memory or network constraints .
If you want to make the most of it CPU, You can start multiple computers on one machine Redis example .
PS: There is such an answer on the Internet , The official explanation of Tucao make complaints about it. , In fact, it is the historical reason , Developers hate multithreading , Later this CPU The problem of how to use it is left to the users .
meanwhile FAQ It's also mentioned in the book , Redis 4.0 Then it started to become multithreaded , Apart from the main route , It also has background threads dealing with slower operations , For example, cleaning up dirty data 、 The release of useless connections 、 Big Key And so on .
7.Redis6.0 What's going on with multithreading ?
Redis Don't you mean using single thread ? Yes? 6.0 Become multithreaded ?
Redis6.0 Multithreading is used to process data Read / write and protocol analysis , however Redis Carry out orders It's still single threaded .
Doing so ⽬ Because Redis The performance bottleneck is ⽹ Collateral IO⽽⾮CPU, send ⽤ Multithreading can improve IO The efficiency of reading and writing , from ⽽ As a whole ⾼Redis Performance of .
Persistence
8.Redis Persistence ⽅ What are the formulas ? What's the difference? ?
Redis Persistence ⽅ The case is divided into RDB and AOF Two kinds of .
RDB
RDB Persistence is to generate the current process data snapshot The process of saving to the hard disk , Trigger RDB The persistence process is divided into manual and automatic triggers .
RDB⽂ It's ⼀ A compressed ⼆ Base number ⽂ Pieces of , It can restore the state of the database at a certain time . because RDB⽂ The file is saved on the hard disk , So even Redis Collapse or exit , as long as RDB⽂ Pieces exist , Can ⽤ It restores the state of the restored database .
Manual trigger corresponds to save and bgsave command :
save command : Block the current Redis The server , until RDB Until the process is complete , For instance with large memory, it will cause long-term blocking , Online environment is not recommended .
bgsave command :Redis Process execution fork Action create subprocess ,RDB The persistence process is the responsibility of the subprocess , It will automatically end when it is finished . The blockage only happens in fork Stage , The average time is very short .
The following scenarios will automatically trigger RDB Persistence :
Use save Related configuration , Such as “save m n”. Express m Data set exists in seconds n At the time of revision , Automatic triggering bgsave.
If full replication is performed from a node , Automatic execution of master node bgsave Generate RDB File and send to slave
perform debug reload Command reload Redis when , It will also trigger automatically save operation
Execute by default shutdown On command , If it's not on AOF Persistence is performed automatically bgsave.
AOF
AOF(append only file) Persistence : Each write is logged as a separate log , Reexecute on reboot AOF The command in the file restores the data .AOF Its main function is to solve the real-time problem of data persistence , So far Redis The mainstream way of persistence .
AOF The workflow operation of : Command write (append)、 File synchronization (sync)、 File rewriting (rewrite)、 Restart loading (load)
The process is as follows :
1) All write commands are appended to aof_buf( buffer ) in .
2)AOF The buffer synchronizes the hard disk according to the corresponding policy .
3) With AOF The files are getting bigger , It needs to be done regularly AOF File rewriting , Compression reached Purpose .
4) When Redis When the server restarts , Can be loaded AOF File for data recovery .
.............................
.... Bloggers are too lazy, too many words , I don't want to write. .... The article has been finished PDF, Friends in need can get it for free by private letter !
边栏推荐
- 微信小程序开发心得
- 2-year experience summary, tell you how to do a good job in project management
- Error: sorting and subscript out of bounds
- 图书管理系统小练习
- [算法] 劍指offer2 golang 面試題2:二進制加法
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- All in one 1405: sum and product of prime numbers
- Novatel board oem617d configuration step record
- TYUT太原理工大学往年数据库简述题
- 阿里云一面:并发场景下的底层细节 - 伪共享问题
猜你喜欢
平衡二叉树详解 通俗易懂
[algorithm] sword finger offer2 golang interview question 2: binary addition
记录:初次cmd启动MySQL拒接访问之解决
染色法判定二分图
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
架构师怎样绘制系统架构蓝图?
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
Inheritance and polymorphism (I)
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
随机推荐
Compile GDAL source code with nmake (win10, vs2022)
[算法] 剑指offer2 golang 面试题2:二进制加法
Edit distance (multi-source BFS)
抽象类和接口
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
图书管理系统小练习
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
Exception: ioexception:stream closed
[Chongqing Guangdong education] Shandong University College Physics reference materials
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
阿里云微服务(二) 分布式服务配置中心以及Nacos的使用场景及实现介绍
GNSS positioning accuracy index calculation
Basic DOS commands
TYUT太原理工大学2022数据库题库选择题总结
堆排序【手写小根堆】
[untitled]
雇佣收银员【差分约束】
What are the advantages of using SQL in Excel VBA
2022国赛Re1 baby_tree