DEV Community

Cover image for Micronaut vs Spring Framework
javaknowledgehub
javaknowledgehub

Posted on

Micronaut vs Spring Framework

In this post we will:

  1. Learn what is the cold start problem and why it is bad
  2. How micronaut solves this problem?
  3. Create a simple service in micronaut.
  4. Compare spring service with micronaut service in terms of
    • Memory use
    • Startup time

Cold Start Problem

When a spring boot application takes a long time to process requests when a service is used after a long time.

Example: Let us assume there is a service that is running on N different nodes. Currently, the service is running smoothly and all nodes produce results in very less time. This is known as the Warm Start.
Now, we use another service after a long time or load on current running service increases. We experience a spike in response time.

Why this spike?

In a serverless architecture like AWS Lambda, service is managed by the cloud provider. It also provides you with advantages like autoscaling, availability. Also, the cost of using serverless services is finely granular(per 100ms time of process). When a service is not getting used, it is not getting billed. Thus there is a high probability that the service may get removed and had to be initialized again before it can be used again.
Similarly, when the load has increased on a service that is autoscaled. A new node is assigned to it. But the new node is not available immediately, it has to be initialized before it gets ready for use. Any new request hitting this node will have to wait for some time (initialization time) before it can be processed.
Alt Text
Initialization time: This is the time that is required for build and configurations to get downloaded, for the container to set up and for code to be deployed in the container. This time is often long in case of spring boot application because the builds are often heavy and they take a long time to get deployed.
Alt Text

Why Micronaut is good?

Micronaut creates light-weight builds that take very less time to deploy. It creates builds that are small in size. Thus can be downloaded in less time. It also creates a build that can be deployed in less time.

Check out full video presentation by Graeme Rocher to understand fully why lighter builds are created by micronaut that take less time to start here.

Create a simple micronaut service

Step 1: GOTO this website https://micronaut.io/launch/
This website is like https://start.spring.io/ in spring boot
Fill out details required to create a new web application and click on generate.
Alt Text

Step 2: Now import the downloaded project in your IDE like Eclipse, IntelliJ.
Alt Text

Step 3: Structure of a Micronaut application is the same as that of a Spring Boot Application and creating a webservice is also almost same.
Create a new class HelloController. Then write following code in it. It is similar to the code written for creating web service in a spring application.
Alt Text
Step 4: Now create the build for this sample application by
Run as ----> Maven Install.

Step 5: Now check the target folder, the size of the build created is small.
Now if we run the application with the command
java -jar {war name}.
It starts the web application in almost no time. This quick statup time prevents the latency in serverless application in production.
Alt Text
Startup time and the build size of the micronaut application is less as compared to a spring application. This difference keeps on increasing as you keep including new dependencies in the pom file.

Is Cold Start really a problem?

In most of the cases, cold start is not really a problem. The cold start affects only the first request that is made after a long time. All other subsequent request made to the server are not affected by the cold start.
If you are creating an application, where every request and every second makes a difference or if your application experiences fluctuating traffic then cold start can be problem. Thus it is important to know what is the cause of this problem and how it can be solved.

Also spring boot is much more mature than micronaut framework. It allows you to make major changes in the application with simple change in configuration. Micronaut being a new framework is not very mature and provides less features than spring.

Checkout my website : https://technicalknowledgehub.com/

Top comments (0)