当前位置:网站首页>What you have to know about network IO model
What you have to know about network IO model
2022-07-06 09:41:00 【Server technology stack】
The Internet IO It refers to the technology of data transmission and exchange between client and server . Here's the picture , The exchange of data between the client and the server cannot be separated from the network IO, The Internet IO Be able to know whether the data is ready , How to read data , When have you finished reading or writing .
As the basic technology of bottom communication , In most applications, you need to use , But most of them are related to the Internet IO Dealing with some middleware frameworks , such as web The server tomcat, database mysql, Message queue rocketmq,rpc frame dubbo etc. , These frameworks are inseparable from the network IO, They help us shield the implementation of the underlying communication , As a background Developer , Get to know the Internet IO Knowledge can have a clear understanding of the complete process of network requests , Get to know the Internet io Knowledge can also provide help for some technology stack selection and middleware tuning , This article will combine my study and my own thinking to share the network io Relevant theoretical knowledge , When learning these knowledge, I found a lot of materials , Hopefully that helped , Let beginners avoid detours .
First give a network IO Outline of knowledge , You should have a feeling of deja vu when you look at it .
I believe you should be familiar with the knowledge listed above , But it is not enough to stay in the understanding stage , If we can systematically master their implementation , Features are better , Computer technology is also developing ,IO The system is also constantly improving , stay Java The earliest field is Bio Take the upper hand , But with the development of computer , To jdk1.4 After that nio, To jdk1.7 Then there was aio.
I believe that back-end friends go to the interview , It is possible that I have met the problem of network knowledge , Synchronization blocking may be asked IO, Synchronous nonblocking IO The difference between ,IO Multiplexed interview questions , The author has also encountered before the interview , In this article, I will start from synchronization blocking IO Explain to asynchronous blocking IO, To IO Multiplexing , Talk about their realization , characteristic , I believe that after reading this article , I'm no longer afraid of the interviewer asking this question .
understand IO Model , We need to understand from two aspects .
The first is IO request ( Data preparation )
The second is IO operation ( Copy data from kernel buffer to user process buffer )( Synchronous and asynchronous are embodied here )
Let's analyze one by one .
Synchronous blocking IO:BIO
I'm sure you're like me , such IO It's the first thing I came into contact with , In college class, the teacher took us to use bio Realize chat function .
bio Server example
// This machine starts a socket
ServerSocket serverSocket = new ServerSocket(63799);
// Wait for the client to connect , blocked
while (true) {
// Block waiting for client connection
Socket client = serverSocket.accept();
// New connection requests are handled by the thread pool
executorService.execute(new ClientAcceptRunnable(client));
System.out.println("client connected");
}
static class ClientAcceptRunnable implements Runnable {
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public ClientAcceptRunnable(Socket socket) {
this.socket = socket;
// Get the input stream from the client , Read client request data inputStream.read
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
}
@Override
public void run() {
String clientCommon = null;
while (true) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// Read client request data , This method is blocked
clientCommon = bufferedReader.readLine();
}
}
bio Client example
// Establish a connection with the server
Socket serverSocket = new Socket("127.0.0.1", 63799);
above bio Example , His biggest defect is that the server will block the connection waiting for the client , At the same time, from the client InputStream Reading data will also enter a blocking state , So I will Bio A connection requires a thread , This is usually inefficient . Usually, the client connection is handed over to multithreading , In this way, multiple requests can be processed at the same time , But this will consume more thread resources .
Pictured above , After the application initiates a read system call ( For example, from the client InputStream Request to read data ), The two stages of waiting for the data to be ready and copying the data from kernel mode to user mode are blocked , This model is a connection that requires a thread ( This connection io Before reading and writing , Threads are also blocked , Can't deal with other things ), Will be limited by the number of threads in the operating system .
Synchronous blocking IO characteristic
Facing the flow IO,read,write Methods will block , Until the other party sends a packet , You need to use multithreading to solve the blocking problem , Multiple client requests are processed simultaneously
Applicable scenario
api Simple features , It is suitable for a less connected and fixed architecture , Concurrency limit ,jdk1.4 The only choice before .
Synchronous nonblocking IO:NIO
such IO The way is Jdk1.4 After that ,api comparison BIO It's more complicated , But his performance is higher , You can handle more requests with fewer threads .
look down nio Server code , The initialization code is better than BIO A lot of code .
public static void main(String[] args) throws IOException {
// To start a socket
ServerSocketChannel server = ServerSocketChannel.open();
// Set to non blocking
server.configureBlocking(false);
// Binding port
server.bind(new InetSocketAddress(8080));
// Selectors
Selector selector = Selector.open();
// Registration events
server.register(selector, SelectionKey.OP_ACCEPT);
// Create a temporary data buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (true) {
int select = selector.select();
if (select == 0) {
continue;
}
// Set of triggered events
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
// Client connection Events
if (selectionKey.isAcceptable()) {
ServerSocketChannel channel = (ServerSocketChannel) selectionKey.channel();
try {
SocketChannel accept = channel.accept();
if (null != accept) {
accept.configureBlocking(false);
accept.register(selector, SelectionKey.OP_READ);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// There are data reading events
if (selectionKey.isReadable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
try {
socketChannel.read(byteBuffer);
String s = new String(byteBuffer.array());
System.out.println("hello: " + s);
byteBuffer.clear();
socketChannel.write(ByteBuffer.wrap(new String("aa").getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
iterator.remove();
}
}
}
Look through the code nio Is based on the event mechanism IO Model . He won't be like BIO Also block waiting for client requests , An event will be triggered when there is a client connection , In this way, there is no need to create a thread for each connection , Only one thread is responsible for receiving connection events , So I say it is a request that requires a thread .
NIO characteristic
Pictured above ,nio It won't block IO request , Return immediately after initiating the system call
Data preparation stage : Make a system call , The system returns directly to ( When the data is ready, it will return ok, If the data is not ready, an error is returned , Non blocking is reflected here ), without , Initiate multiple system calls , Until the data is ready
Data copy phase : Launch the system call again , The data is copied , return ok, Applications process data
NIO be based on Reactor Reactor , When socket Stream read / write socket when , The operating system will notify the application process accordingly , Apply the current read / write buffer or write operating system
Support buffer oriented , Channel based I/O Operation method ,JDK Of NIO Bottom layer epoll Realization , The implementation of the criticized empty polling bug It can lead to cpu soaring 100%, Is initiating io When asked , If the operating system data is not ready , Not to let the requested thread block sleep , Instead, an error is returned immediately
NIO Applicable scenario
Suitable for short connection , Such as chatting ,jdk1.4 Start supporting
Asynchronous non-blocking IO:AIO
AIO Is in NIO Upgraded version on ,AIO That is to say NIO 2. stay Java 7 Introduced in NIO Improved version NIO 2, It's asynchronous and non blocking IO Model . asynchronous IO It is based on event and callback mechanism , That is, after the application operation, it will directly return to , It won't get stuck there , When the background processing is complete , The operating system will inform the corresponding thread to carry out subsequent operations , I put aio It is called a valid request a thread . Suitable for long connection , Call the operating system to complete IO operation , Programming complexity ,jdk7 Start supporting !
This is a I/O Processing mode ,epoll yes AIO Implementation of programming mode ; let me put it another way ,AIO Is an interface standard , The operating system can be implemented or not , That is to say AIO Depends on the specific operating system implementation . stay windows in ,AIO It's through IOCP Realized , See JDK Source code , You can find it in Linux in ,AIO It's through epoll Realized , see JDK Source code
IO Multiplexing technology :i/o multiplexing
If one or more I/O The conditions are ready ( namely , Input ready , Or the descriptor can get more output ), We want to be informed , This function is called I/O Multiplexing , from select and poll Function provides .
A multiplexer , You can use the same thread to listen for many requests , If there is a io When the requested packet is ready, it returns .
I/O Multiplexing is usually used for network applications in the following scenarios :
1、 When the client processes multiple descriptors ( Usually interactive input and network sockets )
2、 When the client processes multiple sockets at the same time ( It's possible , But it's rare )
3、 If TCP The server handles both the listening socket and its connected socket
4、 If the server processes at the same time TCP and UDP If a server handles multiple services , There may be multiple agreements
And bio Comparison ?
Insufficient : Use select When , Two system calls are required , One is select function , One is recvform function
advantage : You can make a thread wait for multiple descriptors to be ready , Instead of a thread waiting for a descriptor
And multithreading BIO Compare ?
Another closely related I/O The model is used with blocking I/O Multithreading of . This model is very similar to the model described above , It's just that the program uses multiple threads ( One for each file descriptor ), Then each thread is free to call blocking system calls , Such as recvfrom.
stay linux in , About the use of multiplexing , There are three different kinds of API,select、poll and epoll,
IO Multiplexing , Can reduce traditional IO Processing thread , A request IO, Corresponding to a thread , That is to say, threads are multiplexed , By putting more than one IO Register to a multiplexer ,IO The multiplexer monitors a IO flow ( File descriptor ) Whether it is completed or not .
Three IO Multiplexing implementation comparison
1、select function : Monitor at most 1024 individual socket(32 The default of bit computer is 1024 individual .64 The default of bit computer is 2048.), If there is a socket Of IO The flow is ready , Notify the application to process , But I won't say which socket Okay , The application needs to handle the match itself socket( Traverse the rotation training to find , Poor performance ), Thread unsafe .
2、poll function : and select equally , But no 1024 individual socket The limitation of ( Based on linked list ), But it is also thread unsafe
3、epoll function : Thread safety ,socket Of IO flow ( File descriptor ) Be on it , Will inform the corresponding socket To deal with , Memory copy , utilize mmap() File mapped memory acceleration and kernel space messaging ; namely epoll Use mmap Reduce replication overhead .
Various IO Model comparison
summary
The main difference between the first four models is the first phase , Because the second stage of the first four models is the same : When data is copied from the kernel to the caller's buffer , The process is blocked on recvfrom In call to . However , asynchronous I/O Deal with these two stages , Different from the first four stages .
After watching BIO,NIO,AIO,IO Analysis of multiplexing , On the Internet IO Is there more cognition about the theory of ?
This article is mainly theoretical knowledge , About the Internet IO You can learn in practice netty The implementation of the ,netty Yes Java The Internet IO It is encapsulated and applied to many middleware .
Reference material
1、Scalable IO in Java
边栏推荐
- Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis cluster
- Compilation of libwebsocket
- The order of include header files and the difference between double quotation marks "and angle brackets < >
- MapReduce工作机制
- Redis之连接redis服务命令
- Use of activiti7 workflow
- 一大波開源小抄來襲
- YARN组织架构
- 《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
猜你喜欢
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
Master slave replication of redis
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Hero League rotation chart manual rotation
Kratos战神微服务框架(一)
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former
I2C summary (single host and multi host)
【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need
Hero League rotation map automatic rotation
随机推荐
一大波开源小抄来袭
Nc17 longest palindrome substring
I2C summary (single host and multi host)
There are software load balancing and hardware load balancing. Which one to choose?
Global and Chinese market of electronic tubes 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
面试突击62:group by 有哪些注意事项?
IDS' deletion policy
Hard core! One configuration center for 8 classes!
leetcode-14. Longest common prefix JS longitudinal scanning method
Redis之发布订阅
Day 5 of MySQL learning
Oom happened. Do you know the reason and how to solve it?
QML type: overlay
工作流—activiti7环境搭建
Several ways of MySQL database optimization (pen interview must ask)
六月刷题01——数组
Mapreduce实例(九):Reduce端join
Le modèle sentinelle de redis
Meituan Er Mian: why does redis have sentinels?