Key advantages of Kotlin Coroutines
Developers often grumble about all the complexity that Kotlin Coroutines hide under the carpet. But this library also hides its best advantages; we often benefit from them without even knowing about it. That is why I decided to start a series about the key advantages of Kotlin Coroutines.
Let’s start with simplicity. With coroutines, we can write simple, imperative code. For backend developers, that is nothing new, but for Android it is a game-changer that we can have that while running on the main thread.
At the same time, coroutines allow a simple introduction of synchronicity with async/await. That is a well-recognized pattern, but in Kotlin Coroutines it offers structured concurrency with ease.
Coroutines are also efficient. Coroutines are lighter than threads. Suspending functions are much lighter than Single, and Flow is much lighter than Observable. Coroutines can only be compared to virtual threads from Loom, but it is only because Loom uses its own coroutines under the hood.
Programmers also underestimate the importance of cancellation. It is critical for Android developers where if a view is destroyed, we should also cancel all its processes. Coroutines offer powerful and effortless cancellation thanks to structured concurrency.
Cancellation mechanisms are also a great benefit for backend developers. If you make async calls and one of them fails, others are cancelled by default.
When you define a backend application in a framework like Ktor, that is coroutines-first, if an HTTP connection is lost, call gets immediately cancelled, the same if WebSocket or RSocket connection is lost.
Synchronization offers numerous powerful tools for synchronizing coroutines. You can easily make one coroutine wait for another coroutine to complete using join or suspend until another coroutine provides a value using CompletableDeferred or Channel.
Coroutines also offer the best support for testing I could see in any library. Kotlin Coroutines supports operating in virtual time, which lets us write precise, fast, and deterministic tests for cases that are hard to test with other libraries.
Virtual time also allows us to test timeouts, retries, and other time-related operations. It also allows us to verify what happens in different scenarios. What if X is faster than Y but slower than Z? What if X is faster than Z? We can easily simulate and test all these scenarios with virtual time!
Kotlin Coroutines provide a powerful abstraction for expressing and processing asynchronous streams of values. This abstraction is called Flow. Flow is much lighter than RxJava or Reactor streams, and its implementation is much easier. It also has a rich feature set with many useful operators.
Coroutines are multiplatform, so you can use them in any Kotlin target and in common modules.
That is why I believe that Kotlin Coroutines is currently the best tool in the market for concurrency. Certainly the best for Android, but also the best for the backend, but only if we use its advantages instead of just treating it like “lightweight threads”.