Why Backend Javaer Should Try Kotlin

Jian-Min Huang
Jun 2 · 3 min read
Photo by Elisabeth Wales on Unsplash

I was a Java Developer mainly focus on backend side before. Spring is my most familiar framework. Since Spring Framework 5 highly supports Kotlin. It’s time to try Spring Framework with Kotlin.

If you google with Why Kotlin, Kotlin Pros. & Cons., Kotlin vs Java, etc. There are many great articles to explain and compare each other. So I’ll not repeat again. But even we know Kotlin has many awesome parts, why many Java developers tend to stay on Java instead of Kotlin?

Umm, It’s a long long story for explaining. But this part is not the main point I want to focus today. But if we consider with business view, stay on Java is a reasonable option. Because even writing Kotlin code is more comfortable than Java. I think the powerful reason is Java already has big ecosystem to support most production cases.

So today I want to show a backend server side scene to explain the title. I will use Coroutine to send or process request concurrently and compare with other code style we used to do.

In frontend side, send request concurrently is a common scene. But in the past, send or process request concurrently in Java backend side is not very popular. I think there are some reasons below.

  1. Model of Servlet(per request per thread) and blocking request is very easy to learn and use. If your server don’t need to handle huge traffic, vertical expansion is a simple and useful idea.
  2. Concurrent request style is more complex than sequential blocking request style.
  3. The ecosystem increase the support recently. For example, Spring 5 deprecate the RestTemplate (Blocking) and suggest we change to WebClient (Non-Blocking)

Talk is cheap. Show me the code.

I design a case for 2–1–2 requests that it’s very common in our real project scene. It means we can send first two requests concurrently. The third request need result of first two requests. Also last two requests need result of third request and we can send it concurrently.

The original blocking request sample is like below block.

As the code you can see if we sequential invoke the requests. It cost 100+200+300+400+500=1500ms. But the 2–1–2 case we explain before means some requests can be send concurrently.

Let’s check the Coroutine version!

We send first two requests concurrently, so it will only cost 200ms. The third request need result of first two, we send it and cost 300ms. The last two requests also need the result of third request. We send this two concurrently, so it will cost 500ms.

So the cost compare to original sample is (200+300+500=1000ms) < (100+200+300+400+500=1500ms).

This Coroutine sample shows a very important point that Java can not replace it. Coroutine can write concurrent code in sequential blocking code style.

Why this simple idea is worthy? If we consider other solutions like handle the thread pool manually, CompletableFuture, Reactive Programming, etc. These techniques and Coroutine are base on top of background thread pool. But other solutions it hard to write it simple and readable. I show the CompletableFuture and Reactive Programming case below, if you have prettier version please feedback with me.

Servlet 3 Async feature support to return a Future
Spring WebFlux support to return a Mono or Flux

Obviously, Coroutine code style is more simple and readable than other. But I think the most important point is coding with right libraries and coding styles at right scenes. For example, if your teams is familiar with Reactive Programming or CompletableFuture. It’s totally ok to stay on that. But if we only compare to these techniques, obviously Coroutine is more easy to learn and write.

It’s worthy to try Coroutine now, come to join me with Spring Framework with Kotlin!

Google Developers Experts

Experts on various Google products talking tech.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store