https://kotlinlang.org logo
Title
m

Moses Mugisha

06/22/2020, 1:01 PM
Hi all, I am having an issue with ktor cold start for each request endpoint after deployment. What is the normal workaround or is this a misconfiguration on my end?
m

mp

06/22/2020, 3:11 PM
Can you be more specific?
m

Moses Mugisha

06/22/2020, 3:13 PM
So, I have a ktor app installed in kubernetes. After each deploy, the endpoint latency is terrible for the first request, but then gets better for sub sequent requests. Is there a way around this?
also, its not a serverless model (like lambda) in k8s. I cant figure out why that would be happening
m

mp

06/22/2020, 9:37 PM
perhaps the way you are setting up your various components is deferring work until it is first used
it is certainly not an intrinsic problem with ktor
e.g. if you are using Guice, make sure you're using Stage.PRODUCTION to eagerly evaluate all bindings, etc.
One of my production ktor services is about 35kloc, has various external services to connect to (e.g. a sql database, redis, etc), and is fully up and running in about 1200ms, to give you an idea of what is reasonable. No issues on the first request.
m

Moses Mugisha

06/22/2020, 11:06 PM
I am using Jackson Convertor. I am not sure how to make it eager
install(ContentNegotiation) {
    register(ContentType.Application.Json, JacksonConverter())
}
Are you using the default engine configs?
j

Jeff

06/23/2020, 7:28 AM
Could you be making database migrations when the app starts? I was also experiencing the same problem.
m

Moses Mugisha

06/23/2020, 7:33 AM
Not migrations per se, but I think I probably need to warm up the database connection pool
m

mp

06/23/2020, 5:01 PM
See https://bitbucket.org/marshallpierce/ktor-demo/src/master/. From the start of main to fully initialized ktor is about 400ms.
Note how it's using CompletableFuture.supplyAsync to load things like jackson on another thread (specifically, in the common fork join pool)
m

Moses Mugisha

06/23/2020, 8:56 PM
Awesome! thanks. I am going to experiment with it