Hi all, I am having an issue with ktor cold start ...
# ktor
m
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
Can you be more specific?
m
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
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
I am using Jackson Convertor. I am not sure how to make it eager
Copy code
install(ContentNegotiation) {
    register(ContentType.Application.Json, JacksonConverter())
}
Are you using the default engine configs?
j
Could you be making database migrations when the app starts? I was also experiencing the same problem.
m
Not migrations per se, but I think I probably need to warm up the database connection pool
m
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
Awesome! thanks. I am going to experiment with it
i
@Moses Mugisha - Can you please share what changes you did for fixing cold start issue, and did it really worked ?