Backend

Why Kotlin for backend is a real thing?

Posted on by Adam Kobor

A year and a half ago Kotlin was just a buzzword for me, and every time it came up in a conversation, everybody was just like "Oh yeah, I've heard of it, it's for Android, yeah?". Well, I thought so too, until I dove a bit deeper into it.

I've been using Kotlin for exactly one year now, and nowadays everybody asks if it's "mature" enough to use it on the backend. Let's take a look why I think that it's not only suitable or adequate for it, but: the perfect choice.

TLDR: Because it just works (in every sense)!

Kotlin is only for Android, and basically just a bunch of syntactic sugar over Java

Android was the first platform, where Kotlin gained popularity, with a big momentum after v1.0.0 was released in February 2016. Then, at the I/O 2017, Google announced first-class support for Kotlin on Android, and maybe that's why most of the people think that Kotlin is an Android-exclusive language. However, JetBrains' intent was to create a modern, safe and interoperable language for the JVM.
So no, Kotlin is not a superset or an extension of Java, it's a standalone programming language.

Kotlin is just a hype that will pass quickly

Just a few facts to refute this:

Kotlin is developed by JetBrains, the company behind a lot of great IDEs (IntelliJ IDEA, WebStorm, PyCharm, PhpStorm and much more), and they are seriously committed to it. Add to this the fact that - according to Stack Overflow's annual developer survey - Kotlin's popularity is increased year by year, and you can be sure that Kotlin will be there in the next decade.

We have a monolith and we don't want to rewrite it

I have good news: you don't have to! As I stated above, Kotlin is interoperable with Java, which means that you can have Kotlin and Java in the same project, they will play nicely together. There is also a well-detailed official documentation about interop:

The advantages

Modern language features

Imagine Kotlin as a language that was built by picking the most loved and futureproof features from other languages! Because Kotlin is a relatively "young" language, it doesn't have old APIs and outdated concepts, making it a safe bet for the future, and a hotbed for new language features.

Just a few highlights of these modern features:

Use any library written in Kotlin or Java, it's up to you!

There are more and more useful libraries written in Kotlin, but because of the Kotlin <-> Java interop, you can use your old favorites too, even if they were written in Java. For example at Makery we are using KotlinTest and MockK for testing, both of them are implemented in Kotlin. However we prefer "database first" solutions, so we use jOOQ when it comes to databases. jOOQ is implemented in Java, and also generates Java, but that never caused any issues in our projects.
If you're into functional programming, you should also check out Arrow!

You can find more info about Kotlin resources here.

Tooling is convenient as it should be

Because JetBrains is the company behind Kotlin, it's self-evident that you'll get the most perfect developer experience when you use their IDEs (e.g. IDEA, Android Studio). But don't be sad if you prefer Eclipse or Visual Studio Code,, there are plugins for them too.

When it comes to build tools, most of the tutorials and documentations mention Gradle, but you can use Maven as well. The main advantage of Gradle is that it offers a Kotlin DSL, which means that you don't have to write your build configurations in XML or Groovy, but you can do it in a type-safe way with Kotlin, supported by most of the IDEs. There is also a comprehensive guide that helps you migrate your existing build.gradle files written in Groovy to Kotlin DSL.

New kids on the frameworks' playground

When you ask developers about the de facto standard backend framework in the Java ecosystem, I bet that most of them would say Spring or Spring Boot, despite that Spring is widely criticized nowadays because it's far from being lightweight. Though if you don't want to learn a new framework, you can use Spring with Kotlin without any problem, there is also an official tutorial about it.

Nevertheless I strongly recommend to check out these pretenders, because all of them play nicely with Kotlin:

It's no accident that I highlighted Micronaut and put it in the first place. In my opinion Micronaut is the closest thing to Spring Boot speaking of patterns and architecture, which makes it easy to quickly understand its core concepts. Besides the similarities, Micronaut has reflection free, compile-time dependency injection and AOP, which results in fast startup time and low memory footprint. Last but not least it comes with modern  features out of the box, like OpenAPI generation, GraalVM support, super fast ORM, and non-blocking client and server.

We are using Micronaut behind different type of microservices (API gateway, search engine, etc.) in production since version 1.0.4, and we are highly satisfied with it.

A straightforward way to cross-functional teams

Personally, I think that a software company's strength - among others - comes from how many of its teams are cross-functional. I'm not saying that everybody should be a Backend Developer and a Scrum Master at the same time (please NO!), but I believe that there should be no silos in software development. We are software engineers or developers in the first instance, not frontend developers, mobile technologists, or something too specific like these.

So consider the following situation: an Android developer from your company is interested in backend as well, or vice versa, the backend guy wants to learn mobile development. As Kotlin works well on both platform, the transition would be much easier for both of them, mostly because they don't have to learn a new language.

Summary

Kotlin is an absolutely mature choice for your backend stack, and you can try it without any risk. Why not give it a shot and enjoy all of its advantages right now?