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

Shreyas Patil

12/10/2020, 11:42 AM
If we're developing a big scale application with Ktor in which there might be many modules. Which DI framework is recommended to use? I saw Kodein, Koin, etc. But they uses reflection I guess which might affect performance? I can use Dagger but it not looks good solution in Ktor since everything here is in functions in Ktor. Any suggestion for choosing right DI Framework? My question might be wrong because I'm not from backend development. I've used DI in Android a lot so just want to see how it can be used here
👀 1
j

John O'Reilly

12/10/2020, 11:48 AM
one advantage at least of using Koin or Kodein is that they work in Kotlin Multiplatform code
👍 4
s

Shreyas Patil

12/10/2020, 11:49 AM
Yeah! But I want some suggestions in the context of performance on server
Because application needs quick response on front-end after performing some database operations on server.
a

aleksey.tomin

12/10/2020, 12:12 PM
As I know Kodein doesn't use reflection.
s

Shreyas Patil

12/10/2020, 12:13 PM
Read this 👇
c

christophsturm

12/10/2020, 12:26 PM
DI server performance does not really matter because you wire up the components at startup.
👌 3
2
s

Shreyas Patil

12/10/2020, 12:38 PM
Okay, thanks!
j

Joost Klitsie

12/10/2020, 2:10 PM
From hobby project perspective, I did not see any issues with Kodein and I actually liked the fact that I can use the same DI framework for the backend as well as my frontend. It made me very productive (which is good, as I don't have so much time for hobby projects). I do think that Christoph's comment is very valid, if you only inject at runtime once you initialize the application, having a 20ms delay is not going to hurt your overall performance. Also there is not a lot of magic happening, I only tried spring boot once or twice but that took more time to get to know how injection is working (BEANS 🥫), and it wouldn't surprise me if that under the hood also uses reflection, especially for field injection and that is not known to be an issue as far as I know.
s

Shreyas Patil

12/10/2020, 2:11 PM
Thanks @joong.lee. Agree 💯
In short, Whatever we use for DI, finally JVM server == Use of Reflection.
c

christophsturm

12/10/2020, 2:29 PM
use of reflection != bad
s

Shreyas Patil

12/10/2020, 2:30 PM
c

christophsturm

12/10/2020, 2:31 PM
never trust microbenchmarks
☝️ 1
ymmv but I’m pretty sure that DI container selection will not even change your startup time anything thats statistically relevant
s

Shreyas Patil

12/10/2020, 2:33 PM
hmm right
c

christophsturm

12/10/2020, 2:34 PM
probably you spend much more startup time in the java classloader than anywhere else
t

thanksforallthefish

12/10/2020, 4:03 PM
in real life, I got the impression that matters so little that while projects like graalvm move a lot at compile time and drastically reduce startup of an application, most of the backend framework actually tries to reduce the configuration you need to write as a developer that they don’t mind using classpath scanning, reflection, and other “slow” operation just so a lot can be autoconfigure for you (at least, I saw this with spring boot, micronaut and quarkus).
👍 1
different is probably if you start using lazy beans, though even then only one request is really impacted, the first one triggering the lazy bean
👍 1
that being said, spring (the one I know the best) is moving away from reflection with kofu and jafu projects, so there are probably benefit to be had there too. so, once more, there is probably no silver lining, find what suits your use case best
👍 1
s

Shreyas Patil

12/10/2020, 4:08 PM
Thanks @thanksforallthefish
68 Views