Kotlinx.coroutines: Best practices, should use JavaNIO and suspendCoroutine or JavaIO and Dispatchers.IO

Created on 2 Nov 2018  路  3Comments  路  Source: Kotlin/kotlinx.coroutines

Hi, I'm trying to do a scalable concurrent web server using coroutines. I have two aproach to consider coroutine wise:

  1. Use JavaIO ServerSocket and Socket. Then dispatch the read and writes to the Kotlin IO Dispatcher. (There's a max amount of thread in the pool)

  2. Use JavaNIO AsynchronousSocketChannel. Then use suspendCoroutines to convert a callback API to coroutines. (Using this model I can limit the max amount of thread in the pool)

What are pros ands cons of both options? There is a third option? Thanks in advance.

question

Most helpful comment

Hi, if you are trying to build something scalable, your approach has to be measurement-driven because there is a lot of controversy about which approach is better/more performant etc.

E.g. for short-living requests with no blocking on its path blocking IO can be better (of course if it is properly written), while for a long-living requests/sessions NIO clearly wins.
So the general answer is "it depends" (as always).

But if you don't know what to pick, it is better to use something written by experts.
Depending on your use-case, there is Ktor (with suspend-based CIO, Netty and Jetty on your choice), pure Netty for implementing something close to network layer (e.g. custom negotiation protocol) or Java 11 HTTP client.

All 3 comments

Threads are expensive.
Non blocking IO performs better.
You can also consider Netty or a simple Java 11 http client.

Hi, if you are trying to build something scalable, your approach has to be measurement-driven because there is a lot of controversy about which approach is better/more performant etc.

E.g. for short-living requests with no blocking on its path blocking IO can be better (of course if it is properly written), while for a long-living requests/sessions NIO clearly wins.
So the general answer is "it depends" (as always).

But if you don't know what to pick, it is better to use something written by experts.
Depending on your use-case, there is Ktor (with suspend-based CIO, Netty and Jetty on your choice), pure Netty for implementing something close to network layer (e.g. custom negotiation protocol) or Java 11 HTTP client.

Hi, if you are trying to build something scalable, your approach has to be measurement-driven because there is a lot of controversy about which approach is better/more performant etc.

E.g. for short-living requests with no blocking on its path blocking IO can be better (of course if it is properly written), while for a long-living requests/sessions NIO clearly wins.
So the general answer is "it depends" (as always).

But if you don't know what to pick, it is better to use something written by experts.
Depending on your use-case, there is Ktor (with suspend-based CIO, Netty and Jetty on your choice), pure Netty for implementing something close to network layer (e.g. custom negotiation protocol) or Java 11 HTTP client.

Thanks that麓s a really good answer. In production we will use Netty or Jetty for sure. This is for internal uses and testing, we don麓t need critical performance improvement but it麓s better to ask and not being lazy.

Was this page helpful?
0 / 5 - 0 ratings