Quarkus GraalVM. Supersonic Subatomic Java, Kotlin & Scala

Share This Post

Due to the actual microservices server paradigm & server costs corresponding to resource’s consumption, Quarkus is a framework that caught my attention some years ago, as it solves JVM language problems on these concerns. In this article you will find Quarkus GraalVM examples and explanation. We are going to create a basic Quarkus REST-API application & create a native image with GraalVM.  Also we’ll mention some existing alternatives to Quarkus.

Quarkus GraalVM


The generated Quarkus project for this article can be found in our GitHub repository.

GraalVM

GraalVM is a universal virtual machine for running applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin and LLVM-based languages such as C and C++.

Quarkus project

This framework is designed to:

  • minimize the size of the generated jar files.
  • lower memory usage.
  • fastest application startup.
Screenshot 2021 05 28 at 13.40.51

Some metrics from the official site:

Screenshot 2021 05 28 at 13.41.44

All these improvements can be accomplished thanks to the following:

  • Use of GraalVM: native image build
  • Build Time Metadata Processing: removes unused classes
  • Reduction in reflection usage
  • Native Image Pre Boot: preboot image as much as possible at build time serializing the state in the executable.

Note that for java projects jdk8 is supported but jdk11 is the default from version 1.13.  Also Quarkus embraces the https://12factor.net/ witch is basic in modern software development.

LICENCE: under the Apache Software License 2.0 or compatible license.

Documentation & guides

The documentation in the official Quarkus page is complete enough and with lots of guides for specific implementations & integrations.  Also checkout the official  Quarkus github repository and the wiki.

Bootstrapping a project

We have several options to start a new project:

  • Using https://code.quarkus.io/ (similar to Spring initializer) to bootstrap your project dependencies & archetype.  Some extensions are in preview or experimental state, so be careful.
  • With official quarkus maven archetypes in github.
  • Via command line with the maven (or gradle) plugin.
  • With IDE Quarkus plugins (f.ex. for Intellij is already bundled).
  Kanban practices for Software Development Teams

Simple example

We are going to start with a simple example on how to create, build & run a project with the starter web-site.  First we select some packages and download the project as a zip:

Screenshot 2021 05 28 at 13.44.53

Quarkus starter web-site helps you with some info on how to proceed:

Screenshot 2021 05 28 at 13.45.54

Now we are going to move it to a proper folder:

mkdir ~/projects/articles
mv ~/Downloads/quarkus-basic ~/projects/articles/quarkus-basic
cd ~/projects/articles/quarkus-basic

You can now import the project into your IDE.  Checkout the README.md & some code examples generated by the initializer page.

Build & run the project

Be sure you use jdk11:

# With sdk-man (use or install):
sdk use java 11.0.10.hs-adpt
# Build & run with maven
./mvnw compile quarkus:dev

This should show you some logging in the terminal:

Screenshot 2021 05 28 at 13.48.29

Checkout the Dev UI & the application end-point.

Screenshot 2021 05 28 at 13.49.06

Build native image with GraalVM docker image

We can use maven-quarkus-plugin to use a dockerized GraalVM to create a native image of our application.

NOTE: On MacOS11.3 with docker version (20.10.5) it didn’t work,  I was getting an out of memory error when packaging to native. I tried to set quarkus.native.native-image-xmx=6g  in the application.properties to set some extra memory for the Java heap (see docs), but it didn’t work either. It works out-of-the-box in an Ubuntu 20.04.2 LTS.

And now execute next in terminal to generate the native image:

./mvnw package -Pnative -Dquarkus.native.container-build=true

It will take about 3 minutes to the quarkus-maven-plugin to package our small project:

Screenshot 2021 05 28 at 13.50.33

And the memory (& cpu) usage is quite big:

Screenshot 2021 05 28 at 13.51.08

As we can see, the generated runner’s at the target folder is quite small compared to Spring generated jars:

  Scala Generics II: Covariance and Contravariance
Screenshot 2021 05 28 at 13.51.33

You can execute the native application from your terminal:

./target/quarkus-basic-1.0.0-SNAPSHOT-runner

As you can see in the image below it starts really quickly (less than 1 second the first time & 0.030s second time):

Screenshot 2021 05 28 at 13.52.24

Conclusion

Quarkus + GraalVM is great for small microservices projects for all performance improvements it brings into the scene, but these improvements decrease as much as project size increases.

Also to have in mind that the performance on Quarkus applications could be affected by the use of some Quarkus libraries in preview/experimental status, that need some performance & resource improvements.

Current alternatives are:

Links

Quarkus official:

Further reading:

If you need any help with this kind of projects, let us know! We are experts in software development!

Author

  • David Garcia Folch

    15 years as a technical/functional analyst and java backend engineer. 8 years: backend/full-stack SOLID architecture designs and POC's implementations… leading & mentoring dev teams in best practices

    View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Subscribe To Our Newsletter

Get updates from our latest tech findings

Have a challenging project?

We Can Work On It Together

apiumhub software development projects barcelona
Secured By miniOrange