https://kotlinlang.org logo
#ktor
Title
# ktor
j

Jeff

05/26/2020, 12:27 PM
Is it just me or Ktor has very slow startup times? I have a Ktor backend hosted in GCP and uses Cloud SQL (postgres). When the backend has been idle for some time, it requires a new startup which takes almost 1 minute. This breaks the user experience. Why does the app have to start all over again, shoulldn't this happen only once during deployment? I checked and saw that part of the initialization that slows the startup is database work (flyway). Is there a way to speed this up or achieve it asynchronously?
👀 1
i

ian.shaun.thomas

05/26/2020, 12:48 PM
Which gcp compute service are you using, cloud functions?
j

Jeff

05/26/2020, 12:59 PM
I am using Google App Engine
i

ian.shaun.thomas

05/26/2020, 1:08 PM
I'm assuming app engine, given it states it is serverless, works off cloud functions under the hood and thus is extremely similar to aws lambda. I think you have a misunderstanding of how serverless works. Spinning up a runtime has a cost, serverless doesn't guarantee your code is hot which is what I would expect to see in your description. When your code goes idle for a while, your function is going to be terminated and new instances will take a while. You should see the same issue scaling up if you make parallel requests and have scaling enabled. Each new instance has to boot a runtime, deploy your code, start your code, then finally execute. https://cloud.google.com/appengine/docs/standard/java/how-instances-are-managed Based on this, if you're using
Automatic scaling
, the behavior sounds similar to a typical aws lambda deployment
Residence
Copy code
Instances are shut down based on usage patterns.
Startup and shutdown
Copy code
Instances are created on demand to handle requests and automatically turned down when idle.
e

Evan R.

05/26/2020, 1:14 PM
Like Ian said, your ktor app will be shut down and spun up automatically based on traffic. Therefore, you shouldn’t be performing database migrations as part of the startup of your app to minimize cold start time. Instead, I’d recommend running your database migrations in a container via Cloud Run, so when you push a new version of your ktor service you can separately trigger the migrations outside the app engine spinup.
4
i

ian.shaun.thomas

05/26/2020, 1:18 PM
yep and word to the wise, Java frankly isn't a good choice for serverless. It has a long boot time compared to node/python which are both far closer to instant. Not to say Java doesn't have a place it just shouldn't be the choice for things that need to be available with lowest possible latency such as handling http requests.
💯 2
2
n

napperley

05/26/2020, 11:52 PM
@Jeff - In order for Kotlin to work well in a serverless environment you would need to use Kotlin Native, which has fast startup times and low memory usage (will heavily depend on what Kotlin Native/C libraries are used, and some other factors) that will easily rival the Go programming language. Also there would need to be a Kotlin Native serverless ecosystem to go with it. At a minimum a serverless library/framework would need to be available.
😂 1
Based on my understanding Ktor Server hasn't been ported yet to Kotln Native, however there are some plans ( https://github.com/ktorio/ktor/issues/571#issuecomment-562118651 ) to make it happen simple smile . Currently there are a limited number of Kotlin Native libraries/frameworks available but it is growing at a gradual pace, and some major Kotlin libraries have already been ported over to Kotlin Native (eg Ktor Client).
I am guessing that the Ktor Server release for Kotlin Native will occur around the same time that Kotlin 1.4 is released. Who knows there might be some Ktor serverless announcements to go with it.
j

Jeff

05/27/2020, 2:45 PM
Thanks for the input @ian.shaun.thomas, @Evan R. & @napperley I could use the Cloud Run option for my existing project. Would it be a better option to switch from serverless to Kubernetes engine?
e

Evan R.

05/27/2020, 2:47 PM
App engine is fine for the actual Ktor server, it just makes more sense to separate the database migrations from your server code
👍 1
j

Jeff

05/27/2020, 2:50 PM
I want to begin a new project for a platform similar to Medium. I am not sure whether I should go with Firebase or my own backend with Ktor and Postgres. Any suggestions on best db and backend language?
t

TanVD

05/28/2020, 10:57 PM
I wonder why do you need Kotlin Native for serverless. JVM is not that fast, but application may start in 300-700ms which is ok for most of use cases :)
🤔 1
181 Views