Digression: We will now carefully talk about I / O multiplexing mechanism, because this argument is too popular, and popular to the general people do not understand what that means. Bo hit a metaphor: S ditty in the city opened a courier shop, the city is responsible for fast delivery service. Ditty because of funding constraints, hired a group of couriers, then ditty find enough money, enough to buy a car express delivery.
Mode of operation a
client each sent a courier, ditty let a courier stared, then drove to the courier express delivery. Slowly ditty discovered there is a problem with this mode of operation
-
Dozens of courier time is spent basically robbed the car, most of the couriers are in the idle state, who grabbed the car, who will be able to send express
-
With the increase in express, courier more and more, shop more and more crowded express ditty find no way of hiring new couriers
-
Coordination between the courier takes a lot of time
Taking these shortcomings, songs and work hard to put forward the following mode of operation
Mode of operation two
songs only hire a courier. And then, the customer sent courier, delivery location marked by good songs, then turn in one place. Finally, in order to pick up the courier express, once a take, then drove to the express delivery, good to send a courier to come back to win.
Contrast
comparison of the two above-mentioned mode of operation, is not it obvious that a second, higher efficiency, better yet. In the parable:
-
Each courier ------------------> each thread
-
Each delivery --------------------> each socket (I / O stream)
-
Express delivery location --------------> of different states socket
-
Customer requests express delivery --------------> request from a client
-
Ditty mode of operation --------------> run server-side code
-
Audit car ----------------------> CPU's
So we have the following conclusions
1, a mode of operation is the traditional concurrency model, each I / O streams (Express) has a new thread (courier) management.
2, the second is the mode of operation I / O multiplexing. Only a single thread (a courier), by tracking the state of each I / O stream (each express delivery location) to manage a plurality of I / O streams.
The following analogy to real redis threading model, shown in FIG.
Referring to the figure, simply put, it is. Our redis-client when in operation, will produce socket with different types of events. In the server, some I / 0 multiplexing program, which was put into the queue. Then, the file event dispatcher, in order to get the queue, forwarded to a different event handlers.
It should be noted that the I / O multiplexing mechanism, redis also provides select, epoll, evport, kqueue such as multiplexing function library, you can get to know yourself.