RPC is a way to solve inter-process communication. An RPC call is essentially a process of network information exchange between service consumers and service providers.
Network communication is the basis of the whole RPC call process.
Common network IO models
Network communication between two PCS is the operation of network IO between two PCS in the last century.
Common network IO models can be divided into four types: Synchronous blocking (BIO) synchronous non-blocking (NIO) IO multiplexing and asynchronous non-blocking (AIO)
The most commonly used are synchronous blocking IO and IO multiplexing
Blocking IO (blocking I/o) :
Waiting for data and copying data
IO multiplexing:
Why blocking IO and IO multiplexing are the most common?
Network IO applications all need the support of system kernel and programming language, blocking IO and IO multiplexing system kernel and programming language support is the most perfect.
Which network IO model does RPC framework tend to choose for network communication?
IO multiplexing and high concurrency scenarios, can use fewer processes (threads) to process more SOCKET I/O requests, but the difficulty is high
Blocking I/O Blocks the process (thread) for each I/O request processed on a socket, but the difficulty is low.
RPC call is a high concurrency scenario, considering the support of kernel \ programming language and the characteristics of IO model itself, we choose IO multiplexing
Zero copy (Zero – copy)
Waiting for data: The system kernel writes data to the kernel after waiting for the nic to receive the data
Copy data: After obtaining data, the system kernel copies the data to the user process space
A complete read/write operation of an application process needs to be copied back and forth between user space and kernel space, and each copy requires a context switch from the USER process to the system kernel, or from the system kernel to the user process.
Cancel the data copy operation between the user space and the kernel space. Each read and write operation of the application process can directly write data to the kernel or read data from the kernel, and then copy the data in the inner core to the network adapter through DMA, or copy the data in the network adapter to the kernel.
Implementation: user space and kernel space write data to the same place (virtual memory)
There are two solutions to zero copy: Mmap +write and Sendfile
Zero copy at the operating system level avoids data copy operations between user space and kernel space, which improves CPU utilization.
Zero copy in Netty
Zero copy of data in user space is very important to deal with the problem of unpacking and sticky packet in TCP transmission
Netty’s ByteBuffer can use Direct Buffers, and use out-of-heap Direct memory for Socketd read and write operations, which can solve the data copy between user space and kernel space.