Posted on

Marcin Moskala about IO disp

https://twitter.com/marcinmoskala/status/2082738114649272725

If you still struggle to understand how the IO dispatcher should be used, here is a pill that explains everything you need to know 👇

IO dispatcher is used when we need to make a blocking call, like reading a file or using a blocking API. This sometimes happens, as some libraries still only offer blocking APIs. You don’t need to use IO dispatcher if you use suspending functions. Those should never be blocked, so a lib with suspending API does not need IO.

IO dispatcher is by default limited to 64 threads (or the number of cores, whatever number is greater). This limit protects our resources. Imagine that you need to send newsletter using blocking API, and you send elements asynchoniously. Without limit, you would start as many threads as many emails you have to send, what would likely cause OutOfMemoryException, as each thread required 1 MB or RAM. Limit makes sending take longer, but it protects our memory.

The biggest problem with IO dispatcher is that is has one limit for the whole application. Imagine that our newsletter service uses all its threads for a couple of minutes. If other services want to use Dispatchers. IO as well, they need to wait in queue until newsletter process is finished.

To avoid such interdependencies, we prefer to have dispatchers with custom limits in each service. We create such dispatchers using Dispatchers. IO.limitedParallelism(limit).

Dispatchers created this way share a pool with Dispatchers. IO and Dispatchers.Default, but this underlying pool is inlimited, and each dispatcher has its own limit, so it is safe and efficient.

How do we set the limit? If we do not care much about execution time, we can set it to a smaller number. For our newsletter it can be 5. If we care more about not having one coroutines wait for enother, even in cost of more resources being spent, we set it to a greater number, like 50.

That is all you need to know about IO disatcher! If you like my teaching style, check our my book Kotlin Coroutines: Deep Dive and my workshops, especially Kotlin Coroutines.