As we work on Kotlin, we implement many features that help build idiomatic and high-performing APIs. We’ve been dreaming of seeing a Web framework that utilizes these features to the max. Today, we can finally say we’ve built that kind of framework! Please welcome Ktor, which has just reached 1.0.
Ktor is a Kotlin framework for building asynchronous servers and clients in connected systems. It is being created by the Kotlin team, and as such, it takes full advantage of the language in order to provide a great developer experience and excellent runtime performance.
Ktor is built from the ground up using coroutines, which make it possible to express complex asynchronous constructs as if they were simple sequential code. Coroutines are also quite efficient at runtime. In addition, the use of Kotlin features allows us to provide a domain-specific language (DSL) — a concise and developer-friendly API for configuring your application with no magic, just code.
Of course, Ktor is not the only option for building server-side applications in Kotlin. We’re pleased to see that other frameworks such as Spring and Vert.x are providing first-class support for Kotlin, and using these frameworks is a great way to preserve your existing investment and experience. At the same time, Ktor offers unique advantages thanks to its use of coroutines and DSLs, which makes it a great option when you’re starting a new backend project or a microservice.
Ktor is already deployed in production in a number of projects running at scale. For example, HLTV.org serves about 200 million pageviews per month and about 3.1 billion pageviews in total on Ktor. The latest release of the ThoughtWorks Technology Radar lists Ktor in their Assess category.
The two main parts of Ktor are the HTTP server framework, running on the JVM, and the multiplatform HTTP client. As a JetBrains project, Ktor naturally comes with helpful tools to get you started with new projects quickly.
The HTTP server part is built upon proven technologies like Netty, Jetty, and also Java servlets. When using lightweight engines like Netty or Jetty, it starts ultrafast and is ready to receive connections in under a second. It is compatible with the 12 Factors paradigm, is container-friendly, and can easily be embedded into desktop (or even Android) applications. You can also run it in an application server such as Tomcat.
Ktor provides a rich and easy-to-use extensibility mechanism, on top of which most of the built-in functionality is implemented. Third-party features look no different from the built-in ones. Out of the box, Ktor includes all the essential features like sessions, authentication, JSON serialization, popular template engines, Web sockets, metrics, and many others. It also supports a productive development workflow, with a fast start, automatic reloading, and easy-to-use facilities for writing unit and integration tests.
The client part is asynchronous as well, built using the same coroutines and IO primitives that drive the server. This makes it perfect for building asynchronous microservice architectures, connecting all the backend functionality into asynchronous pipelines.
The client is implemented as a multiplatform library, which currently supports JVM, JS, Android, and iOS. This makes it possible to retrieve data on mobile devices and web pages in a uniform way, without blocking application execution or building complex chains of anonymous functions to handle successes or failures.
Like all other Kotlin libraries, Ktor is free and open-source, developed on GitHub and released under the Apache 2 license.
The Hello World of Ktor is small enough to fit in a tweet:
To go beyond that, the website provides a Quickstart guide, extensive documentation for writing servers and clients, and many sample projects. If you prefer videos, Ryan Harter’s KotlinConf 2018 talk is a great introduction to using Ktor on the backend.
Hop over to https://start.ktor.io/ to get a project template, check out the docs, and your new backend will be up and running in no time! If you need help, the friendly folks on the #ktor channel on Kotlin Slack are there for you.
Have a nice Ktor!