High-Performance Network Patterns: Reactor and Proactor
There are only two approaches for performing IO operations efficiently.
Explanation
Reactor is a non-blocking synchronous network pattern that detects ready-to-read/write events. Whenever an event is detected (such as a read-ready event), the application process must actively invoke the read method to complete the data read — that is, the application process must actively read data from the socket receive buffer into its own memory. This process is synchronous; the application process can only process the data after reading is complete.
Proactor is an asynchronous network pattern that detects completed read/write events. When initiating an asynchronous read/write request, the address of a data buffer (to store the result data) and other information must be provided so that the system kernel can automatically complete the read/write operation on our behalf. Here, the entire read/write work is performed by the operating system, unlike Reactor where the application process must actively initiate read/write calls. Once the operating system completes the read/write operation, it notifies the application process to process the data directly.
Put simply, IO operations require waiting for data transfer, and transfer time is far slower than memory read/write speed — let alone CPU execution speed.
To address this, the concepts of non-blocking and asynchronous operations were introduced, corresponding to synchronous and blocking operations.
Blocking vs. non-blocking refers to whether you need to wait for completion after making a request. If no waiting is required, it is non-blocking.
For example, when you go to a restaurant, placing an order is making a request. Blocking means you sit and wait for the food to arrive, doing nothing during that time. Non-blocking means you browse your phone or chat with friends while waiting, so when the food finally arrives, the waiting process wasn’t so tedious.
Synchronous vs. asynchronous refers to whether you need to inquire about task completion or wait to be notified. If you must inquire, it is in the synchronous state.
Using the restaurant analogy again: after you are seated, if you keep looking around for a waiter to bring your food, that is the synchronous serving state. During this time, you can keep watching (blocking) or you can play with your phone and then look around (non-blocking).
You could have spent this time shopping around the mall, but then you wouldn’t have been able to eat on time. Alternatively, you can call ahead to order a table of dishes, and the restaurant calls you back when the food is ready — this is asynchronous, which is typically non-blocking.