Nerd For Tech
Published in

Nerd For Tech

An opinionated Kotlin backend service - Framework

TL;DR;

This article series explains the scaffolding for Kotlin / Ktor based backend services. It elaborates on the following topics:

Quick start

You should see the OpenAPI documentation:

Alternatively open: https://ktor-template-app.herokuapp.com where the app is running too.

Intermezzo

Before digging into the technical details I want to talk about the motivation behind my research and also about the requirements for the tech stack I was looking for.

Motivation

I love new technology. New technology is usually better than old technology. It lets me create less buggy, more robust, more secure, more maintainable, easier to change, faster etc. software and all that with fewer lines of code.

People might disagree and there’s a good article about green vs. brown programming languages that made me think here. I will circle back to that article at the very end of my series.

Requirements

As a software architect I use the ISO 25010 software quality model to define the important quality characteristic a component needs to have.

In my case I wanted (in order of priority):

  1. Maintainability: modular software, easy to decompose, adheres to single responsibility principle, separation of concerns, testable
  2. Portability: agnostic to the runtime/deployment environment, can be deployed on-premise or any cloud computing provider, containerized, no vendor lock-in, open-source if possible
  3. Reliability: production-ready technology, robust, resilient to failure, high availability

With these requirements in mind I set out to research the vast market of backend frameworks. At the end of this article series I will circle back to the requirements above and discuss which boxes I could tick off.

Backend Framework(s)

I looked into different tech stacks using different languages reading through a ton of material and also ran tests with some of the most popular frameworks (Spring Boot, Node.js) but then ultimately decided to go down a new route. I wanted to use a language that is maintainable and fun to use and for me neither Java, JavaScript/TypeScript or Ruby are very appealing compared to languages like Go, Swift or Kotlin. With my background as Android developer, Kotlin was the obvious choice.

Even with the scope narrowed down to Kotlin frameworks there’s still a lot of choice:

and more…

Source: Kotlin Server Side Frameworks

Spring (Boot)

Spring Boot was an easy one to eliminate. It was made with Java in mind which shows in verbosity and the use of configuration over code (endless annotations…). It feels hard to read (hard to maintain), heavy (verbose) and for the lack of a better word… old:

Compare that to the elegance and conciseness of a Ktor application:

Vert.x

I love writing reactive applications (I used to be a huge RxJava fan) and one of my side projects is to build a reactive backend stack (reactive down to the database).

For the purpose of this research I decided to not add the complexity of reactive programming on top of everything else though (I think the complexity of reactive programming stems from the fact that my brain is more used to imperative programming but that’s probably true for the majority of software engineers -> maintainability requirement).

Anyway I decided against Vert.x but it will definitely be a hot contender once I tackle my reactive backend stack “project”.

Javalin, http4k, Ktor

To be honest I did not do in-depth tests with Javalin or http4k. My decision to use Ktor was mostly based on the fact that it’s the framework created by the Kotlin inventors which hopefully means it will gain some traction and not wither away any time soon. The popularity on Github seems to support that hope (as of 4/29/21):

  • http4k: 29 watchers, 1.7k stars, 156 forks
  • Javalin: 106 watchers, 4.7k stars, 411 forks
  • Ktor: 175 watchers, 8.2k stars, 675 forks

One feature that I like about Javalin is its OpenAPI generator. To get OpenAPI documentation with Ktor I had to jump through some hoops (see here).

After settling on Ktor as a backend framework, my first step was to get the build and deployment up and running which leads us to the second article in this series: An opinionated Kotlin backend service — Build & Deployment.

Thanks for reading this and as usual feel free to provide feedback. Happy coding!

Similar articles

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

AWS SQS Use cases

Procedural Anomalies in SAS

Cookage — CPS and Validation Process

Git Initialized! — Initializing Git and Connecting to GitHub

Unity Game Dev : create a Player Bounds with “if statement”! (Part I)

Building Android O with a Mac

Enums and Enemy AI

How to casually set up custom domains for API Gateway with OVH

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
Emanuel Moecklin

Emanuel Moecklin

More from Medium

Building async API using Ktor & Kotlin Coroutines

Observability in Kotlin — with KTor and New Relic

Source / Sink Architecture pattern for MediaCodec (android)

Object equality in Java and Kotlin