当前位置:网站首页>Interviewer: are you sure redis is a single threaded process?
Interviewer: are you sure redis is a single threaded process?
2022-07-28 02:00:00 【Lin Feifan 1998】
This time, we mainly share Redis Thread model The interview questions .
- Redis Is it a single thread ?
- Redis What is the single thread mode ?
- Redis Why is it so fast to adopt single thread ?
- Redis 6.0 Why use single thread before ?
- Redis 6.0 Why did you introduce multithreading later ?
Redis Is it a single thread ?
Redis Single thread means 「 Receive client requests -> Resolve request -> Read and write data -> Generate data to the client 」 This process consists of a thread ( The main thread ) To complete , This is what we often say Redis It's single threaded .
however ,Redis The program is not single threaded ,Redis At startup , Yes. Start the background thread (BIO) Of :
- Redis stay 2.6 edition , It will start 2 A background thread , Deal with closed files separately 、AOF The two tasks of washing dishes ;
- Redis stay 4.0 After the version , Added a new background thread , Used for asynchronous release Redis Memory , That is to say lazyfree Threads . For example, to perform unlink key / flushdb async / flushall async Wait for the order , These deletion operations will be handed over to the background thread to execute , The advantage is that it will not lead to Redis The main route is stuck . therefore , When we want to delete a big key When , Do not use del Command deletion , because del It is processed in the main thread , And that leads to Redis The main route is stuck , So we should use unlink Command to asynchronously delete large key.
The reason Redis by 「 Close file 、AOF Brush set 、 Free memory 」 These tasks create separate threads to handle , Because the operation of these tasks is very time-consuming , If you put these tasks on the main thread , that Redis The main thread is easily blocked , In this way, subsequent requests cannot be processed .
The background thread is equivalent to a consumer , Producers throw time-consuming tasks into the task queue , consumer (BIO) Keep polling this queue , Take out the task and execute the corresponding method .

Close file 、AOF Brush set 、 The three tasks of freeing memory have their own task queues :
- BIO_CLOSE_FILE, Close file task queue : When there are tasks in the queue , The background thread will call close(fd) , Close the file ;
- BIO_AOF_FSYNC,AOF Disk brushing task queue : When AOF Log is configured to everysec After the options , The main thread will AOF The log writing operation is encapsulated into a task , Also put it in the queue . When it is found that there are tasks in the queue , The background thread will call fsync(fd), take AOF File swipe ,
- BIO_LAZY_FREE,lazy free Task queue : When there are tasks in the queue , Background threads will free(obj) Release object / free(dict) Delete all objects in the database / free(skiplist) Release the jump table object ;
Redis What is the single thread mode ?
Redis 6.0 The single line mode before the version is shown in the following figure :

The blue part in the figure is an event loop , It is the responsibility of the main thread , You can see the network I/O And command processing are single threaded . Redis When initializing , I will do things in the following years :
- First , call epoll_create() Create a epoll Objects and calls socket() A server socket
- then , call bind() Bind port and call listen() Monitor should socket;
- then , Will call epoll_crt() take listen socket Add to epoll, Register at the same time 「 Connection event 」 Processing function .
After initialization , The main thread enters a Event loop function , I mainly do the following things :
- First , First call Processing send queue function , See if there are tasks in the sending queue , If there is a task to send , Through write Function to send the data in the client sending buffer , If this round of data is not over , Will register the write event handler , wait for epoll_wait Find writable before processing .
- next , call epoll_wait Function waits for the event : If it is Connection event arrival , It will call Connect the event handler , This function will do these things : call accpet Get the connected socket -> call epoll_ctr Connect connected socket Add to epoll -> register 「 Read events 」 Processing function ; If it is Read events arrival , It will call Read event handler , This function will do these things : call read Get the data sent by the client -> Parse command -> Processing command -> Add the client object to the send queue -> Write the execution result to the send buffer and wait for sending ; If it is Write events arrival , It will call Write event handler , This function will do these things : adopt write Function to send the data in the client sending buffer , If this round of data is not over , Will continue to register the write event handler , wait for epoll_wait Find writable before processing .
That's all Redis Working mode of single line mode
Redis Why is it so fast to adopt single thread ?
The official benchmark results are , Single threaded Redis Throughput can reach 10W/ Per second , As shown in the figure below :

The reason Redis Using single thread ( The Internet I/O And execute orders ) So fast , There are several reasons :
- Redis Most of the operations All in memory , And an efficient data structure is adopted , therefore Redis The bottleneck may be the memory or network bandwidth of the machine , Rather than CPU, since CPU It's not a bottleneck , So it's natural to adopt a single threaded solution ;
- Redis Using the single thread model can Avoid the competition between multiple threads , It saves the time and performance overhead caused by multi-threaded switching , And it will not lead to deadlock .
- Redis Adopted I/O Multiplexing mechanism Handle a large number of clients Socket request ,IO Multiplexing mechanism refers to the processing of multiple by one thread IO flow , It's what we often hear select/epoll Mechanism . Simply speaking , stay Redis In the case of running only one thread , This mechanism allows the kernel to , There are multiple listeners at the same time Socket And connected Socket. The kernel listens to these all the time Socket Connection request or data request on . Once a request arrives , I'll give it to Redis threading , And that's how it works Redis Threads handle multiple IO The effect of flow .
Redis 6.0 Why use single thread before ?
We all know that a single threaded program cannot use the multi-core of the server CPU Of , So early Redis The main work of the version ( The Internet I/O And execute orders ) Why use single threads ? We might as well have a look first Redis Official FAQ.

The core meaning is :CPU It's not a constraint Redis The bottleneck of performance , More often, it is affected by memory size and network I/O The limitation of , therefore Redis There is nothing wrong with using single threads in the core network model , If you want to use multi-core Services CPU, You can start multiple nodes on one server or adopt the method of fragmented cluster .
In addition to the official answer above , The reasons for choosing single thread also have the following considerations .
After using single thread , High maintainability , The multithreading model is excellent in some ways , But it introduces the uncertainty of program execution order , It brings a series of problems of concurrent read and write , Increased system complexity 、 At the same time, there may be thread switching 、 Even lock and unlock 、 Performance loss caused by deadlock .
Redis 6.0 Why did you introduce multithreading later ?
although Redis The main work of ( The Internet I/O And execute orders ) It has always been a single threaded model , however stay Redis 6.0 After the version , There are also many I/O Thread to handle network requests , This is because with the performance improvement of network hardware ,Redis Performance bottlenecks sometimes occur in the network I/O On the disposal .
So in order to improve the parallelism of network request processing ,Redis 6.0 Multithreading is used to process network requests . But for read-write commands ,Redis Still using one-way processing , So everybody Don't misunderstand. Redis There are multiple threads executing commands at the same time .
Redis Official expression ,Redis 6.0 Multi threading introduced by version I/O Features at least double performance .
Redis 6.0 Version support I/O Multithreading features , The default is I/O Multithreading only handles write operations (write client socket), Read operations are not handled in a multithreaded manner (read client socket). To enable multithreading to process client read requests , We need to Redis.conf In the configuration file io-threads-do-reads Configuration item set to yes.
// Read requests also use io Multithreading io-threads-do-reads yes meanwhile , Redis.conf The configuration file provides IO Configuration item for the number of multiple threads .
// io-threads N, Means to enable N-1 individual I/O Multithreading ( The main thread is also a I/O Threads )io-threads 4 Setting the number of threads , The official advice is if 4 Nuclear CPU, It is recommended that the number of threads be set to 2 or 3, If 8 nucleus CPU It is recommended that the number of threads be set to 6, The number of threads must be less than the number of machine cores , The larger the number of threads, the better . therefore , Redis 6.0 After the version ,Redis At startup , By default, there will be 6 Threads :
- Redis-server : Redis The main thread , Mainly responsible for executing orders ;
- bio_close_file、bio_aof_fsync、bio_lazy_free: Three background threads , Handle the task of closing files asynchronously 、AOF Disk brushing task 、 Free memory task ;
- io_thd_1、io_thd_2、io_thd_3: Three I/O Threads ,io-threads The default is 4 , So it will start 3(4-1) individual I/O Multithreading , To share Redis The Internet I/O The pressure of the .
边栏推荐
猜你喜欢

What is method and methodology: understand the underlying logic of self-improvement

Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice

开源飞控(PX4、ardupilot)

LeetCode高频题128. 最长连续序列,经常被互联网大厂面试考到

Digital economy is the core of future economic development

企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建

ue4 unreal NDisplay插件 简易使用 三折幕 详细...

Linux Installation mysql8.0.29 detailed tutorial

BGP联邦实验

Summary: Prometheus storage
随机推荐
What are the important applications of MES system in manufacturing enterprises
HyperMesh circular array - plug in
集合/容器
简单为美-编程思路
存储成本降低 80%,有赞数据中台成本治理怎么做的?
Linux安装mysql8.0.29详细教程
ue4 unreal NDisplay插件 简易使用 三折幕 详细...
【面试:并发篇28:volatile】有序性
递归的使用:1.将平铺数组转为树 2.将树转化为平铺数组
"Do you" want to be a test / development programmer? We strive to sprout
幸福的晚年
Gbase 8C database object size function (I)
一些事情的思考
GBase 8c 恢复控制函数
Unity 通用红点系统
Tencent cloud hiflow scene connector
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
Flink 在 讯飞 AI 营销业务的实时数据分析实践
什么是方法,什么是方法论:了解自我精进提升的底层逻辑
Flink's real-time data analysis practice in iFLYTEK AI marketing business