An opinionated Kotlin backend service - Framework
This article series explains the scaffolding for Kotlin / Ktor based backend services. It elaborates on the following topics:
- An opinionated Kotlin backend service - Framework
- An opinionated Kotlin backend service - Build & Deployment
- An opinionated Kotlin backend service - API Routing and Documentation
+ OpenAPI Generation with Ktor
+ Object validation in Ktor/Kotlin
+ JSON Serialization in Ktor
- An opinionated Kotlin backend service - Database / Migration / ORM
- To be written: An opinionated Kotlin backend service - Testing
- clone the repo
- cd into Ktor-Template
sudo docker-compose up)
- Open http://localhost:2000
You should see the OpenAPI documentation:
Alternatively open: https://ktor-template-app.herokuapp.com where the app is running too.
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.
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.
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):
- Maintainability: modular software, easy to decompose, adheres to single responsibility principle, separation of concerns, testable
- 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
- 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.
Even with the scope narrowed down to Kotlin frameworks there’s still a lot of choice:
Source: Kotlin Server Side Frameworks
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:
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”.
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
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!
Architecture Proposal to Build Servers With Ktor
The aim of this article is to show how to create a Ktor server with a clean and testable architecture.
How to Deploy a Ktor Server Using Docker
Full description about how to deploy your Ktor Server using Docker & Docker compose.