Micronaut - The disruptive Java Framework

Micronaut - The disruptive Java Framework

If you haven't head of Micronaut, it is basically a new jvm framework (works with Java, Groovy and Kotlin) that feels like Spring, but way faster. That's because the underlying implementation of Micronaut uses Ahead of Time compilation to do dependency injection, instead of Spring's reflection-based dependency injection.

We went too far with Reflection

Spring and Java users since back in the early 2000s already knew that Reflection is really heavy and slow, but that was also back in the day wherein servers were getting comoditized (i.e. AWS started making servers cheap during this time) which meant, the trade off were acceptable. Reflection eats up memory and slows down your startup time? - no problem, increase the server capacity.

But then came containers and serverless and all of the sudden, the efficient use of server resources become important again. A lot of java applications did not go into containers like their ruby/python/nodejs/go counterparts, because java applications tend to have really slow cold start.

While java devs would celebrate when a substantially sized code base starts up in 1 minute, our counterparts in the other languages are used to 1-5 second cold startup. And having 1 minute cold startup on serverless is definitely wasteful.

My Rant on Java Microservices

This made it difficult to adopt microservices and serverless for the java community. Sure, there are plenty of java projects out there that doing microservices and serverless, and they do work (and I've done a few myself). But once you've done it in other languages, java microservices feels forced. Like it does not have the same feeling as making a nodejs or a python microservice.

Even with spring cloud, with all of Spring's bells and whistle, it still does not feel the same. Sure, you can have reactive microservices, and make the communication eventful and/or RESTful, and you make sure that each java microservice is just responsible for only one domain - but still, each of this java microservice still feels like a monolith.

Micronaut's Answer - Ahead of Time Compilation

Micronaut was introduced sometime last year (2018) and it's answer to all of (my) issues is Ahead of Time compilation. That is, during compile time (i.e. javac), micronaut will do some work and create classes on the fly, to manage the dependency injection. That means, on your application's startup, it will no longer have to do the expensive reflection operations to identify your beans and their properties, because it would have already done that during compile time and the data is already in the newly compiled classes.

These newly compiled classes though are abstracted from the developer. The developer does not need to know all about those classes. Instead, the developer just needs to remember to annoate his/her classes with @Singleton (for example) and inject it into another classes with @Inject.

Ahead of Time Compilation Voodoo - is it safe?

Android has been using this since 2016, and Project Lombok has been using it since 2009 (Note sure if you can consider Lombok as using AOT compilation, but it does run during compile time to inspect your java classes, and add methods unto your java classes directly).

So it seems pretty safe, but admittedly, this is the first time the java community would do it extensively in the server side.

Are there no more reflections in a Micronaut application?

Well, there are still some reflection. And also, if you use another library which uses reflection (i.e. Hibernate), Micronaut will not magically remove reflection from those libraries. What Micronaut focuses on is removal of reflection from the dependency injection side, which causes a lot of the startup and memory issues.

Performance

How fast is it? Well, there are plenty who have done their own testing regarding this, but from my own annecdotal experience, it is like im running python, or a nodejs web app - really fast startup time. But at the same time, it has the runtime performance still of a regular java application (I love you python, but you just dont scale as good as java).

You can refer to this blog post by Piotr for some performance measuresments : https://piotrminkowski.wordpress.com/2019/04/09/performance-comparison-between-spring-boot-and-micronaut/

Spring's 6.3 seconds vs Micronaut's 3.4 seconds.
Spring
Micronaut

But basically the gist is, spring starts up slower and consumes more memory than a micronaut application. Furthermore, spring accumulates startup time and runtime memory much faster than micronaut as both application grows.

Will Spring be able to Answer back?

Will Spring be disrupted or will it be able to answer back? - hard to say. From my opinion, if Micronaut's approach to dependency injection does prove itself in the future, I don't think Spring will be able to adopt the same approach. The core of Spring's dependency injection is reflection and changing that to AOT would be a massive rewrite that if they do go down that route, they would probably be letting go of all of their advantages in the first place.
However, since Spring has gone way beyond dependency injection since 2006, it's multitude of libraries would still probably survive regardless of future of Spring's dependency injection.

Conclusion

My bet is that frameworks like Microanut would disrupt the incumbent Spring. If it's not Micronaut, it would be Quarkus or something else that would come in and disrupt Spring. And I think it will happen within the next 5 years.

Appendix

Graeme Rocher, main author of Groovy on Grails, creator of Micronaut, presenting Micronaut in Devoxx.

Micronaut is Cloud Native

https://guides.micronaut.io/categories/cloudnative.html