Hi I wanted to use Kotlin for backend services. Th...
# server
n
Hi I wanted to use Kotlin for backend services. There are bunch of frameworks for server side. Most famous ones are Springboot, spring and Ktor. My server would be mostly IO bound. Also, another concern is currently my org uses REST but we might switch to GRPC later. I want something which might require minimal effort If I change the transport layer.
r
just to be sure what is your question? I have a hunch that you're asking what the pros and cons are of various frameworks and what people recommend? Am I right in my assumption?
n
Yes that is totally correct. Also, the one which ease in grpc migration
👍🏽 1
r
I can't tell about GRPC 😞 but I've good a bit of experience with Spring Boot and Ktor. I think I prefer Ktor over Spring Boot. However it isn't a completely clear cut preference The main advantages of Ktor imo is that I can use a more reasonable DI framework (e.g. koin) and that the way of writing endpoints does depend of weird annotations (question of taste I think, I am certain not everybody shares this point of view) To me however, Ktor lose a lot by the overuse of extension methods, which in some cases makes the code really hard to test, and in a lot of cases (if you ask me) the usage of extension methods feels like that someone got a new tool, using it everywhere ignoring what the reasonable tool actually is, and I recall bumping into a lot of cases that would've had much cleaner and straight forward solution had traditional object oriented design been used (in combination with function, but that goes without saying in Kotlin methinks). If I recall right (it's been 1 year approx) I also recall a reluctance to have interfaces for classes, furthermore making testing problematic That said, I'd probably still chose Ktor over Spring, especially due to the DI, but it isn't completely clear cut for me at least
1
ł
another choice would be http4k. I like it because of *First class testability (*_Create lightning fast, rock solid test suites for individual endpoints, applications or multi-service systems, all running fully in-memory)._ So a service is just an ordinary function. You can either deploy it as a server (Apache, Jetty, Netty, Undertow, Ktor, or Ratpack) or just write a unit test without any infrastructure. So the unit test is lightning fast. Opposite of spring boot. If you work with unit tests a lot and want them to run very quickly I recommend it very much.
🙌 3
👌 1
🙌🏽 1
c
Note that http4k is not compatible with coroutines, which IMO removes the main advantage of Kotlin. https://github.com/http4k/http4k/issues/94#issuecomment-774505850 If you like coroutines, nothing comes even close to Ktor.
👍 4
👍🏽 1
n
what about springboot? Why is it not being preferred?
ł
that is true, http4k does not support coroutines, there is even my comment in the issue you've mentioned
👀 1
🙂
a
Spring is an older technology that tries to shore up the weaknesses of older versions of Java. Spring Boot is just a bootstrapper for Spring, so I will not refer to it specifically. While Ktor, Http4k, and Javalin are primarily HTTP servers, Spring tries to be an entire application framework. In my opinion, Spring is no longer relevant in a Kotlin application, as Kotlin solves many of the problems Spring tries to solve, but better. In fact, Spring actively prevents you from using many of Kotlin's best features, such as null safety and immutable properties. One of Kotlin's greatest strengths is its strong compiler, but Spring throws it all away for reflection.
👍 2
ł
I'm not a spring fan, but there is a kotlin support in spring boot, so you can use null safety and immutable properties with constructor binding annotation
a
Curious: does it work for spring-data?
ł
in spring boot 3.0 reflection is avoided, due to the native images support, but it is not a mature version yet (published couple of months/weeks ago)
I've worked with spring data for the mongodb, and yes, I've used immutable data structures
a
Maybe take what I say with a grain of salt then.
t
Same experiences (with spring boot 2) that I can use data classes for both api and entities
n
how is springboot concurrency model? Will it use threads ?
c
I'd say spring boot is still a good enough choice for now. For me the biggest benefit from Kotlin is that I can enjoy the great language as well as a huge toolbox and community from old-fashion java world. Since Spring boot is popular enough and still actively developing, it is much easier to use the out-of-the-box features and makes your backend service production-ready. Spring 3.0 is based on Java 17, and is easily delivered as a native image without JVM at all. Also, you may want to separate your core logic from the framework as much as possible, so potentially the same logic can be exposed via http, grpc, async like kafka listener, or even old-fashion SFTP integration. Each of them will have their own framework to help you out. Most likely you will have a core library writing in Kotlin, use most loved features from the language such as null safety and coroutines. And then you can connect your core logic to the framework, with a minimal adaptor which may hide away some Kotlin unique features.
j
Hi everyone... These are my thoughts regarding the Kotlin server tools (if you think different, just continue the discussion 😉): • Spring is a bit slow and bulky. However, it has the best support • Micronaut is like a better Spring... But if I had to invest on using something new, I would go for Vert.x (below) • Vert.x offers the best performance (but the learning curve is steeper than other tools) • Quarkus seems good, but Kotlin support is not GA yet • Coroutines may not pay the learning effort considering that Loom will be probably out next September, and it seems promising (by looking at the Helidon Níma benchmarks) • Ktor is backed by JetBrains (which ensure your investment on the long term) • From this point, I'll talk about the 'indie' tools. The faster one is Kooby (Jooby for Kotlin). Which seems very well crafted • HTTP4K is a good option for the performance/development speed tradeoff • Hexagon is also a good option for the development speed/performance tradeoff, but with a syntax closer to Express.js Wrapping things up: on a company I would use Vert.x for performance critical services and Ktor for bussiness services. I would use HTTP4K or Hexagon if the team is committed to use them and ideally to contribute to them. If performance is important, you can check the TFB benchmark for some hints.
329 Views