<>Redis Why fast ?

* Redis The data is in memory , All operations are memory level operations .
* Redis It's single threaded , The loss caused by thread switching and locking is avoided .
* Redis use epoll As non blocking I/O Implementation of multiplexing ,IO Multiplexer listens for multiple socket, And will socket
Put in queue , One at a time from the queue socket To event dispatcher , The event dispatcher then socket
Assign to the corresponding event processor for processing , These processors are pure memory operations , Very efficient , Processing an event may take only a few microseconds .
Redis model structure :

* Redis There is a file event handler inside , It is single threaded , It consists of four parts , namely :IO
Multiplex program ,socket, Event dispatcher and event handler , The event processor is divided into : Connect reply processor , Command request processor and command reply processor .
<>IO Multiplexing

fd_set Arrays are entities 0 and 1 Digit group of ,fd Refers to the file descriptor .
frequently-used IO There are three multiplexing models :select,poll,epoll.

*
select: It maintains an array structure fd_set, call select Function time , Will be copied from user space fd_set
To kernel space , And monitor whether an event is triggered , If yes, traverse through indifference polling to find the location where the event is triggered , Then perform related read or write operations . The time complexity of polling is O(n).

* shortcoming : Kernel to monitored fd_set Set size limit , Maximum 1024 ; Every call select, All need to fd_set
The collection is copied from user state to kernel state , All need to traverse in the kernel All that came in fd_set , Low efficiency .
*
poll: And select similar , The difference is that it uses poll_fd The data structure implements a variable length array , There is no limit on the maximum number of file descriptors .

*
epoll:epoll And select The difference is ,epoll When monitoring whether an event is triggered , Callback functions are also set , If the event triggers , The callback function is executed , And will be ready
fd put to readyList in , Instead of polling and traversing all fd_set . also epoll There is no limit to the maximum number of file descriptors . In the case of high concurrency epoll
Can support more connections .

epoll There are three functions in :

* epoll_create; Used to create preservation epoll Space of file descriptor
* epoll_ctl: File descriptors for adding and deleting monitoring objects
* epoll_wait: Wait for the file descriptor to change

Technology