当前位置:网站首页>Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
2022-06-26 09:45:00 【wohu1104】
Redis A single threaded architecture is used 、 Non blocking I/O 、 Multiplexing model to achieve high-performance memory database services .Redis It's single threaded . So why single thread ?
Redis stay Reactor Event handlers are developed in the model , This event handler is divided into several Socket( Socket )、IO Multiplexing program 、 Event dispatcher 、 Event handler ( Connect to answer processor 、 Command request processor 、 Command reply processor ), The queue of event dispatcher is consumed by single thread event processor , It's also because of this ,Redis It's called the single thread model .
When multiple events occur concurrently ,I/O All that the multiplexer will listen to Socket, Associate different event handlers , This association operates in order (sequentially)、 Sync (synchronously)、 One socket at a time to the event dispatcher Socket, When the event generated by the previous socket has been processed ,I/O The multiplexer will continue to pass the next socket to the event dispatcher .
1. Single thread model
Open three redis-cli The client executes commands at the same time . client 1 Set a string key value pair :
127.0.0.1:6379> set hello world
client 2 Yes counter Do auto increment :
127.0.0.1:6379> incr counter
client 3 Yes counter Do auto increment :
127.0.0.1:6379> incr counter
Redis The model of client and server can be simplified as the following figure , Every client call goes through sending commands 、 Carry out orders 、 Three procedures to return the result .
Among them the first 2 Step is the key point to be discussed , because Redis Is a single thread to handle the command , So a command from the client to the server will not be executed immediately , All commands go into a queue , And then it's executed one by one .
So the above 3 The order of execution of client commands is uncertain , As shown in the figure below . But it's certain that no two commands will be executed at the same time 
So two incr No matter how the command is executed, the final result is 2, No concurrency issues , This is it. Redis The basic model of single thread .
2. Non blocking IO
When we call the socket's read-write method , By default they are blocked , such as read Method to pass in a parameter n, Indicates that the maximum number of bytes read before returning , If there is not a byte , Then the thread will get stuck there , Until new data arrives or the connection is closed ,read Method to return , The thread can continue processing . and write Methods generally do not block , Unless the kernel's allocated write buffer for the socket is full ,write Methods will block , Until there's free space in the cache .
Non blocking IO An option is provided on the socket object Non_Blocking, When this option is on , Reading and writing methods will not block , It's how much you can read , Write as much as you can . How much can be read depends on the number of data bytes in the read buffer allocated by the kernel for the socket , How much can be written depends on the number of bytes of free space in the write buffer allocated by the kernel for the socket . Both the read method and the write method will tell the program how many bytes it actually reads and writes through the return value .
With non blocking IO It means that the thread is reading and writing IO You don't have to block it anymore , Reading and writing can be done in a flash, and then the thread can continue to do other things .
3. Multiplexing ( Event polling )
Non blocking IO There is a problem , That's the thread reading the data , As a result, I read part of it and went back to , How does the thread know when it should continue reading . That is, when the data comes , How the thread gets notified . It's the same with writing , If the buffer is full , I can't finish writing , When should the rest of the data continue to be written , Threads should also be notified .
Event polling API To solve this problem , The simplest event polling API yes select function , It is provided by the operating system to the user program API. The input is a list of read-write descriptors read_fds & write_fds, The output is the corresponding readable and writable event . There is also a timeout Parameters , If nothing comes , Then wait at most timeout Time , Thread is blocked . As soon as anything happens during this period , You can go back to . After the time passed, nothing happened , And will return immediately . After getting the incident , The thread can continue to handle the corresponding events one by one . After processing, continue to poll . So the thread goes into a dead cycle , We call this cycle the event cycle , A cycle is a cycle .
Every client socket socket All have corresponding read-write file descriptors .
read_events, write_events = select(read_fds, write_fds, timeout)
for event in read_events:
handle_read(event.fd)
for event in write_events:
handle_write(event.fd)
handle_others() # Deal with other things , Such as scheduled tasks
Because we passed select The system call handles the read and write events of multiple channel descriptors at the same time , So we call this kind of system call multiplexing API .
Multiplexing of modern operating systems API No longer in use select system call , And switch to epoll(linux) and kqueue(freebsd & macosx), because select The performance of system call is very poor when there are many descriptors . They may be used in a slightly different form , But in essence it's all the same , Can use the above pseudo code logic for understanding .
Server socket serversocket The read operation of an object refers to the call accept Accept new client connections . When will a new connection come , through select The read event of the system call is notified .
4. Why can a single thread be so fast
Why? Redis Using the single thread model, the processing power can reach 10000 levels per second ? It can be summed up in three points :
- Pure memory access ,
RedisPut all the data in memory , The response time of memory is about 100 nanosecond , This is aRedisAn important foundation to achieve 10000 level of access per second . - Non blocking
I/O,RedisUseepollAsI/OThe realization of multiplexing technology , PlusRedisIts own event handling model willepollConnection in 、 Reading and writing 、 All closures are converted to pieces , Not in the networkI/OWaste too much time on . - Single thread avoids the consumption of thread switching and race state .
Single threading can bring several benefits :
- Single thread can simplify the implementation of data structure and algorithm . Readers familiar with high-level programming languages should understand that the implementation of concurrent data structures is not only difficult, but also troublesome in development and testing .
- Single thread avoids the consumption of thread switching and race state , For server development , Lock and thread switching are usually performance killers .
But there is a problem with single threads : There is a requirement for the execution time of each command . If a command is executed too long , Will cause other commands to block , about Redis This kind of high-performance service is fatal , therefore Redis It's a database for fast execution scenarios .
5. Multiple asynchronous threads
5.1 Lazy delete
stay 4.0 Version introduced unlink Instructions , It can do lazy processing on delete operations , Throw it to the background thread to reclaim memory asynchronously .
> unlink key
OK
You can put the whole Redis All the valid data in the memory can be imagined as a big tree . When unlink When the command is issued , It just keeps a branch of the tree from breaking , Then throw it into a nearby fire ( Asynchronous thread pool ). The moment the branch leaves the tree , It can no longer be accessed by other instructions in the main thread , Because the main thread will only access along this tree .
Redis Provides flushdb and flushall Instructions , Used to empty the database , This is also an extremely slow operation .Redis 4.0 It also brings asynchronization to these two instructions , Add... After the command async Parameter can uproot the whole tree , Throw it to the background thread to burn slowly .
> flushall async
OK
5.2 Asynchronous queues
The main thread takes the reference of the object from 「 The tree 」 After removal of the tumor , Will take this. key The memory reclaim operation is packaged as a task , Queue asynchronous tasks , The background thread will fetch tasks from the asynchronous queue . The task queue is operated by both the main thread and the asynchronous thread , So it must be a thread safe queue .
Not all unlink Operations are delayed , If the corresponding key The memory used is very small , There is no need to postpone processing , Now Redis The corresponding key Reclaim memory immediately , Follow del The instructions are the same .
5.3 Other asynchronous delete points
Redis Reclaim memory except del Instructions and flush outside , Will also exist in key The expiration date of 、LRU Eliminate 、rename The instruction and the slave library are all synchronized rdb It will be carried out immediately after the document flush operation .
Redis4.0 It also brings an asynchronous deletion mechanism to these deletion points , Opening these points requires additional configuration options .
slave-lazy-flushFinished receiving from the libraryrdbAfter the fileflushoperationlazyfree-lazy-evictionMemory reachesmaxmemoryEliminated whenlazyfree-lazy-expire keyExpired deletelazyfree-lazy-server-del renameCommand delete destKey
Reference resources :
- 《Redis Development and operation 》
- https://juejin.cn/book/6844733724618129422/section/6844733724710420487
边栏推荐
- 爬虫相关文章收藏:pyppeteer 、Burpsuite
- Explained: A Style-Based Generator Architecture for GANs (StyleGAN)
- 节流,防抖,new函数,柯里化
- Classified catalogue of high quality sci-tech periodicals in the field of computing
- 首期Techo Day腾讯技术开放日,628等你
- Mysql database field query case sensitive setting
- Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
- LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
- A Style-Based Generator Architecture for Generative Adversarial Networks
- Industrial and enterprise patent matching data (hundreds of thousands of data) 1998-2014
猜你喜欢

"One week's work on digital power" -- encoder and decoder

Common circuit design

Summary of common commands of vim

2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(1)详细解析教程

Jz2440 - - - utiliser le programme de gravure uboot

Kubernetes cluster deployment (v1.23.5)

What you need to know to test -- URL, weak network, interface, automation

c语言语法基础之——指针(字符、一维数组) 学习

Super data processing operator helps you finish data processing quickly

【CVPR 2021】Unsupervised Multi-Source Domain Adaptation for Person Re-Identification (UMSDA)
随机推荐
pcl install
How about the security of flush stock trading software? How to open an account in flush
Summary of common commands of vim
Leetcode basic calculator 224 227. follow up 394
Curriculum learning (CL)
2021-11-12 vrep vision sensor configuration
Flutter's brain map notes are easy to find and search!
欧冠比赛数据集(梅西不哭-离开巴萨也可能再创巅峰)
[trajectory planning] testing of ruckig Library
2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(2)详解
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
Common circuit design
How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card
Common SQL add / delete / modify query statements
教你用shell脚本检测服务器程序是否在运行
Use recursion or a while loop to get the name of the parent / child hierarchy
Spark based distributed parallel processing optimization strategy - Merrill Lynch data
PHP extracts TXT text to store the domain name in JSON data
Edge computing is the sinking and extension of cloud computing capabilities to the edge and user sides
【AAAI 2021】Few-Shot One-Class Classification via Meta-Learning 【FSOCC via Meta-learning】