Async with style — Kotlin web backend with ktor, coroutines, and jasync-mysql

Sep 27, 2018 · 2 min read

Kotlin 1.3 is in RC and will be released soon, so I thought it is a good time to give it a spin with coroutines and ktor that will be graduating out of experimental mode with the GA. With all that and jasync-sql (That I am working on) it is possible to write async backend that looks sequential.

visit almaty on Unsplash

With ktor, coroutines, and jasync-sql it is possible to create a complete async backend, written in sequential style but without blocking threads during requests from the user all the way up to the database.

How does it work?

With that in hand we can easily create a cool extension method on connection to do what we wanted the first place:

suspend fun Connection.sendPreparedStatementAwait(
query: String,
values: List<Any>): QueryResult =
sendPreparedStatement(query, values).await()

What’s left is just to put the value in the response:


Connections pool

The solution for that is to use a ConnectionPool. It has the same interface of a Connection with a specified configuration:

  • maxObjects — the number of simultaneous connection.
  • maxQueueSize — the number of pending queries.
  • maxIdle — the millisecond before an idle connection is reclaimed.
  • validationInterval — interval in milliseconds to ping the server to make sure connection is ready.

This is the configuration I used:

val poolConfiguration = PoolConfiguration(
maxObjects = 100,
maxQueueSize = 10_000,
maxIdle = TimeUnit.MINUTES.toMillis(15),
validationInterval = TimeUnit.SECONDS.toMillis(30)

That’s all

Hope you will find it helpful!