koolak82
koolak82
Nov 3 · 5 min read

Micronaut Real World Example App

micronaut.io is the new Java micro-framework for the new Cloud-age serverless applications/services.

Now the cloud age is not coming in 2019, The year AWS was launched, 2008?? may be, can be safely said as origin of the cloud age (not going into debate, since that’s not the point). And the Java is also not new to the cloud, probably by the ubiquity of the Java platform. Starting with Write Once Run Anywhere and some absolute winner capabilities for writing Server Side Software, be it Static typing, Garbage Collection, Concurrency built in etc., it has been in use Cloud since the time Cloud revolution started.

Hence the above quote (of mine) needs a bit of unravelling. One part beside left to touch upon in the statement is “What is a micro-framework?”

Put simply, micro-framework is what makes you do the boilerplate setup, plumbing, and infra when writing micro-services (another buzzword, but I am assuming you know what micro-service is?)

Now, Spring Boot is the gold standard for micro-framework when it comes to micro-frameworks in Java world. One can go as far as that Spring saved Java when Java and J2EE was having horrendous time. Arguably in late 2000s and early 2010s when Java 6 was looking like distant dream and J2EE was like rocket ship to use.


Cloud and Serverless

What changed in Cloud though is what Java had to go through in next iteration plus the fundamentals of new framework in Cloud too. So nothing against Spring, but here are the things what seems or are more or less lacking for Java to go Cloud Native full throttle and how does Micronaut (and similar new frameworks) help in the same.

I was kind of cheating or holding on till this point when I didn’t mention Serverless. Cloud is of course, not just Serverless. And of course, everything said till the point hold true irrespective of bring Serverless into the picture or not.

  • Serverless demands faster startup time.
  • And as much Lesser memory footprint as required.

Now, Java is no slouch by any means in either of departments, and comparing with the others is not the intention here. And for Server Side Application use case in old days, probably these were not the breaking requirements either. Further to explain, consider this, an optimised Java Server, in late 2000s can work on 300–512M and was able to serve 10,000 of concurrent connections, the famous C10K requirement, with ease. I am not making something here. And even if not giving the kind of technical proofs here, which one should expect from a tech blog, let me add here that Java has rich set of Non-Blocking (NIO 2) and different Concurrency Paradigms and Frameworks (From Threads to Fork Join to Streams to Actor / Akka, to RxJava in chronological order, some going into early 2010s) which have been used by many many projects, companies et al. 300–512M is not an exorbitant memory by any means by 2010s standard.

Similarly on the startup time front, again a somewhat latency is expected and tolerable in server side apps. Of course, servers are expected to take some time when they start but when they are running, they are expected to give quick response. Again, Java is close to top on that front. Various benchmarks published showed server startup in low seconds. 1–5 ms was pretty norm for spring boot. And I am talking about real MicroServices here, not the “Hello World!” kind of tutorial apps. With that set of dependencies, and database in the backend, and Hibernate layer in between, the startup time is acceptable for all and Netflix, Uber may be.

There is a mute point of throughput which I guess I addressed implicitly by mentioning C10K.

So, what changes in micronaut?

Actually the above. Repeating the definition, since it’s a micro-framework for Java serverless application/services, it takes cares of the two main problem highlighted. And how does it do that and improve on the older frameworks, like Spring?

Micronaut basically takes care of the startup time first through no use of Reflection. And instead using Ahead of Time design with Annotation Processes (in Java at least) for good-old IoC.

So, it’s a trade off from moving to the Compile time to runtime. Acceptable may be since we develop less and deploy and use more in Cloud / Serverless.

The improvement comes from Annotation Processor (for Java). But here all the thing is done at compile time instead of runtime.

And Micronaut specifically calls out that it avoids Reflection as much as it can. The ultimate goal is to be 100% reflection free. Which is important to achieve Faster startup time, and Smaller memory footprint which are important for cloud use cases. Other desirable requirement for cloud is to have the constant metrics (latency, and response time) for all the requests. Which is also taken care of no usage of reflection and everything at compile time hence the better usage of inlining at the start itself.

All of these things can be grouped Ahead of Time. Another buzzword but it is different from the JEP 295. Again going back (a paragraph), it is done all through Annotation Processing in Java and similar ways in Groovy.

Also the app devs in android development, might be familiar with Dagger. Micronaut quotes that it is similar to Dagger but is for backend server side Java as opposed to Android. I came to know about that in the process of exploring Micronaut.

Other design goal of Micronaut besides avoiding reflection is also worthy of mention which it states to be Proxy free also as much as possible. That too helps in faster startup time and also avoid the cost of dynamic method invocation (though less) as opposed to direct method call. I believe (though not 100% expert on this) Proxy could be lazy and can have initialisation at actual runtime and could constitute some variance in response time as opposed to linear and deterministic response.


Real World App

So, now an attempt at practice with the theory. And there is a playground for the same. Now I could guess that there are similar todo apps for Javascript frameworks, and now standard petclinic for java. May be more. But this one too was a nice find in the process.

So, I have attempted backend API in Micronaut, sort of working already.

This already has

  • OpenAPI
  • MyBatis
  • Hibernate Seed Data / JPA
  • JWT authentication already built in

Go ahead, please see the code, fork or PR. I am pretty sure I will learn more this way and may be able to help others too.

Happy MicroNauting!

koolak82

Written by

koolak82

(Sphagetti) Javascript, (Bloated) Java .. in the middle, and (frightening, mysterious) DB at the back

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade