Search code examples
javaperformanceionio

nonblocking-io vs blocking-io on raw data throughput


In apache HTTPComponent document there is a statement:

Contrary to the popular belief, the performance of NIO in terms of raw data throughput is significantly lower than that of blocking I/O."

Is that true? Can someone explain this in more details? And what is a typical use case where

request / response handling needs to be decoupled


Solution

  • Non blocking IO should be used when you can handle the request, dispatch it for processing on some other execution context (different thread, RPC call to another server, some other async mechanism) and release the web-server's thread to handle more incoming requests. When the processing of the response will be completed, a response handling thread will be invoked, and it will send the response to the client.

    I would recommend reading netty documentation for better understanding of the concept.

    As for higher throughput: When your server sends/recieves large amounts of data, all those context switches, and passing data between threads, can really hurt overall performance. Think of it like this: you receive a large request (PUT request with a large file). All you need to do is to save it to disk, and return OK. Starting to toss it between threads could result in few more mem-copy operations that would have been needed in case you've just threw it to disk in the same thread. And handling this operation in async manner would not improve performance: though you could have released the request handling thread back to web-server's thread pool and let it process other requests, your main performance bottleneck is your disk IO, and in this case - trying to save more files simultaneously, would only make things slower.

    I hope I was clear enough. Please feel free to ask more questions in comments if you need more explanations.