Estimate project
← All posts

Java frameworks for cloud-native environments compared

A new series kicks off on our blog! It focuses on two Java frameworks positioned as alternatives to Spring Boot - Quarkus and Micronaut. Expect performance benchmarks across different microservice workloads, in both imperative and reactive styles, and much more. Below is the opening article of the series!

20 min read
MicronautJavaQuarkuscloud-nativeSpring Boot

Not long ago, two fast-growing alternatives to Spring Boot appeared in the Java ecosystem, both competing for a bigger slice of the market. They are Quarkus and Micronaut, and unlike Spring Boot, both were designed from day one with native images and cloud-native systems in mind. That is the reason Spring Native came into existence. The majority of comparisons available online put Quakus and Micronaut side by side with Spring Boot, but without looking at native images.

This article opens a series about these frameworks. Expect plenty of benchmarks measuring performance under various microservice workloads, using both imperative and reactive approaches. We will discuss frameworks, charts, and even guides on moving existing applications away from Spring.

What motivated the creators

Many people have used Spring, and it has a reputation for being dependable and easy to work with, but it is not free of problems. One of them is its heavy reliance on runtime reflection, which can lengthen application startup and increase memory usage.

This is why Quarkus and Micronaut were created: to close the gap in cloud-native applications from the point of view of the Java language. Keeping these frameworks lightweight enough for microservices requires steering clear of runtime reflection. This goal was reached by combining GraalVM, AOT code generation and native images, which cut both application startup time and memory footprint.

Spring influence

Both Quarkus and Micronaut took inspiration from Spring Boot, since there was no point in reinventing the wheel. A lot of the frequently used annotations have counterparts in Quarkus and Micronaut, although they are not always identical. As a result, moving from Spring Framework to these frameworks does not involve a steep learning curve.

As noted above, Quarkus and Micronaut ship modules that support selected Spring mechanisms and annotations, which simplifies migrating from Spring. These modules work by substituting Spring annotations with the ones the framework provides. The convenience has a price, though: more maintenance, which can raise the total cost of keeping the application running over the long term.

The Quarkus team went further still and published an e-book titled “Quarkus for Spring Developers”, which introduces Quarkus to software developers and offers advice on moving from Spring quickly and painlessly.

What do Quarkus and Micronaut share?

Both frameworks are very modular, so we can pick only the modules our project actually needs. They also apply the “convention over configuration” principle, which means Quarkus and Micronaut can be added to a project just by declaring them as a dependency, and they need very little configuration to run smoothly.

A central feature is the Inversion of Control (IOC) container; the implementations differ somewhat but have a lot in common. Using Dependency Injection (DI) and IOC correctly makes our code easier to test, more open to change, and loosely coupled.

To work around the restrictions native images impose on reflection, Micronaut and Quarkus both rely on Ahead-of-Time (AOT) compilation in place of many processes that would otherwise generate code at runtime. This lowered both the application’s memory footprint and its startup time. It is especially valuable in serverless environments, where microservice instances are scaled up and down dynamically depending on load balancing requirements. From my experience, integration tests run much faster with native images, which shortens development and deployment time.

Annotating code is the typical way to use the mechanisms these frameworks provide. Such decorators frequently depend on proxies, i.e. objects that sit between the original object and the client. The proxies are generated ahead of time (AOT) instead of being built at runtime with runtime proxies or CGLIB. A well-known aspect-oriented programming (AOP) decorator is the @Transactional annotation, which marks a method or class as taking part in a transaction.

These applications can be configured in several ways. Configuration files, config servers or environment variables are all available, which makes it easier to treat them as resources in Kubernetes or other deployment approaches.

Now that we know what Quarkus and Micronaut have in common, we can look at how their implementations differ. At first sight these differences may go unnoticed or seem minor, since they are hidden behind a solid abstraction layer.

Common modules and implementations at a glance

Spring Boot with native images

Spring’s first release dates back to 2002, and it is still maintained by Pivotal (now part of VMware). In March of 2021, Spring introduced beta access to a new module, Spring Native, which lets Spring build native images and use ahead-of-time (AOT) compilation. Spring Boot 3.0.0, generally available since November 2022 with GraalVM Native Image support, replaces the experimental Spring Native project. That said, not every Spring Boot and Spring Cloud extension is fully supported.

Webserver

Spring Boot 3.0.0 supports servlets on Tomcat, Jetty or Undertow. With the Webflux module and a reactive approach, the Web component is supported on Jetty, whereas Websockets are supported by Tomcat, Jetty 9, Undertow and Reactor Netty.

Serialization

By default, Jackson handles serialization and deserialization of JSON and XML data. Spring can also use Gson as a JSON serializer. If your application needs HATEOAS (Hypermedia as the Engine of Application State), however, you have to stick with Jackson, because Gson is not supported there.

Instead of REST API communication, you can also use GraphQL thanks to the Spring GraphQL module.

Database

Native images do not fully support every data module, but there are still a few to pick from, namely Spring Data JDBC, Spring Data R2DBC and Spring Data JPA. With JPA, the Hibernate build-time bytecode enhancement plugin is mandatory. Spring’s default JDBC connection pool is HikariCP. Keep in mind that all of these are merely abstractions on top of the real database connection. At the moment, Spring supports only those database connectors whose metadata lives in the GraalVM Reachability Metadata Repository or is supplied by the user. Supported databases currently include Postgres, MySQL and H2.

For NoSQL, the following databases are available: ElasticSearch, Neo4j, Redis and MongoDB. While going through the documentation of the NoSQL modules not mentioned here, I came across a few notes warning that some features may fail because they rely on CGlib proxy. You therefore have to verify them yourself.

Cloud

This wiki page describes the limitations of Spring Cloud modules. Still, we will go over other modules below, since the page does not cover everything – not every module is built by the Spring Cloud team.

For integration with cloud providers and cloud-native architecture, there are modules for both GCP and Azure, so users have options. The wiki lists which features are supported under native compilation. AWS has an open milestone for Spring 3.0.0, and the progress on native support can be followed here.

Spring Cloud Function additionally offers integration with AWS lambda, Azure functions and GCP. Unfortunately, Spring Cloud Kubernetes is not supported yet.

For tracing and metrics, Micrometer is the only option. Spring Cloud Sleuth works in Spring 2.X together with Spring Native, but it is not supported in Spring Boot 3.0.0, because it was moved into Micrometer Tracing.

We can also use the Eureka Client for service discovery, although Eureka Server is not supported.

Summary

Even though Spring is a mature tool, I think it has the largest drawback compared with its rivals. Several of its modules were neither written nor designed with native compilation in mind. As a result, some of them need extra configuration in native images, come with limitations, or are not supported yet. On top of that, I ran into several issues while compiling native images, some of them caused by the Macbook’s M1 chip. Compiling a simple application with buildpacks could take over 10 minutes, and at times it never finished.

Micronaut

Micronaut was created in May 2018 by Graeme Rocher, known as the author of the Grails framework. Together with Object Computing, he set up a foundation for Micronaut that gathers developers from well-known companies such as JetBrains, Microsoft and Amazon to keep evolving it. The framework has a highly modular design, and its authors say the IOC container can be used as a standalone library without any other Micronaut features. It supports Java, Kotlin and Groovy.

Webserver

Out of the box, Micronaut’s web server does not use servlets; it is built on the Netty Framework for both blocking and non-blocking IO. Netty is an asynchronous, event-driven framework whose distinguishing trait is the ability to serve many clients on a single thread. Classic servlets, by contrast, create one thread per client that stays blocked until the request completes. Servlets are still available via Micronaut Servlet, which supports Tomcat, Jetty and Undertow.

Looking more closely at the HTTP server, we find that Micronaut, just like Spring, implements the RFC-6570 specification for URI templates, as well as RFC-491 for custom HTTP Methods.

Serialization

Micronaut also relies on Jackson as its default JSON and XML serializer, configured to use the Bean Introspection API, which removes the need for reflection. On top of that, the Micronaut Serialization module is built on the Bean Introspection API. It also handles Jackson, JSON-B and BSON annotations. What I find the coolest is that annotations are validated at compile time. For example, if you put an invalid date format in @JsonFormat, compilation fails with an error.

The authors have paid attention to security too: only classes annotated with @Serdeable can be serialized or deserialized. This prevents us from serializing objects we would rather not expose, such as entities. Classes we cannot annotate ourselves can be imported with the @SerdeImport annotation, typically placed on the Application class.

Instead of REST API communication, GraphQL can be used through the Micronaut GraphQL module.

Database

Micronaut Data, a counterpart of Spring Data, works with many JDBC drivers, among them H2, Postgres, Oracle, MariaDB, MySQL and MS SQLServer. To use one, you just add it as a dependency and Micronaut Data configures it automatically. A key difference from Spring Data is that Micronaut Data avoids reflection and runtime proxies altogether, and it verifies the type safety of queries at compile time. For connection pooling, Micronaut uses Apache DBCP2, Oracle UCP, HikariCP and Tomcat. There is also a Jooq integration for writing type-safe queries in Java.

Micronaut Hibernate Reactive and Micronaut R2DBC allow reactive communication with the database via supported drivers such as H2, PostgreSQL, MySQL, MariaDB and SQL Server.

Popular NoSQL databases are covered as well, including MongoDB, Cassandra, DynamoDB, Redis, Neo4j and Elastic Search.

Cloud

Micronaut integrates with a broader set of Cloud services than Spring does with native images. It covers every major cloud provider and its services, such as AWS, GCP, Azure and Oracle Cloud. It also provides Micronaut Object Storage, an abstraction over data buckets that works with the clouds listed above.

Kubernetes has its own integration, limited to service discovery, clients for config maps and secrets, and a layer on top of the official client from the Kubernetes Java SDK.

Micronaut supports Ribbon and Hystrix, two Netflix libraries built for the cloud. Ribbon is a client-side load balancer, while Hystrix offers fault and latency tolerance using mechanisms such as a circuit breaker. I would not recommend Hystrix for new projects, though, since it has been in maintenance-only mode since 2018. Resilience4j is a reasonable replacement and has its own Micronaut integration, although the Micronaut team does not maintain that integration.

The Micronaut Tracing module is yet another abstraction, supporting Jaeger and Zipkin, both of which implement the Open Tracing API. The project also supports OpenTelemetry.

The final cloud abstraction is Micronaut Service discovery, with support for Eureka, Consul and Spring Cloud Config Server. Because of the Spring module integration, we do not need to drop that solution right away; we can build new services in Micronaut and migrate the legacy services gradually.

Summary

Micronaut offers many integrations that work in native images and, in a good way, resembles Spring in many respects. Its team aims to bring reflection down to zero and is willing to build its own solutions when it believes it can do better. Thanks to these efforts, Micronaut provides considerably stronger type safety than its competitors.

Quarkus

Quarkus was created in March 2019 by RedHat, the company known for its Linux products and tools such as Ansible and Openshift, and for its OpenJDK build for Java. It is the newest of the three tools RedHat has built. Its developers promote Quarkus as a Kubernetes-native framework tuned for low memory usage and quick startup. It supports Java, Kotlin and Scala, with Scala requiring a separate module and extra configuration.

GraalVM has a dedicated distribution for Quarkus named Mandrel, created to make maintenance easier. Mandrel is built on standard OpenJDK and Red Hat Enterprise Linux libraries rather than on Labs OpenJDK. It concentrates entirely on native image development and strips out the parts of GraalVM that Quarkus does not need, such as the Truffle Framework and Polyglot.

Webserver

Quarkus supports both reactive and imperative programming, and both are implemented on top of Vert.x, maintained by the Eclipse Foundation. Quarkus also ships an adapted version of Undertow that runs on Vert.x and makes it possible to use Servlets.

For its HTTP layer, Quarkus uses RESTEasy, RedHat’s certified implementation of the JAX-RS specification. RESTEasy also implements the MicroProfile REST Client API, which lets developers build type-safe clients for their APIs. By putting the right JAX-RS annotations on an interface, developers obtain a REST Client instance and can call the controller just as if they were invoking it directly. This simplifies building and maintaining RESTful web services in a Quarkus application.

Serialization

Quarkus does not provide its own JSON serialization mechanism and depends on Jackson and JSON-B instead. XML serialization requires adding the quarkus-resteasy-jaxb and quarkus-resteasy modules to the project.

As an alternative to a traditional REST API, Quarkus lets us use GraphQL through SmallRye GraphQL.

Database

Quarkus works with many JDBC drivers, such as DB2, Derby, H2, MariaDB, Microsoft SQL Server, MySQL, Oracle and PostgreSQL. Connection pooling is handled by Agroal, and there are three ways of talking to the database: plain JDBC, JPA with Hibernate Framework and Panache. Panache offers the Repository Pattern or Active Record, the latter being a fairly uncommon approach in the Java Ecosystem. Reactive Hibernate, optionally combined with Panache, supports SQL databases such as Db2, PostgreSQL, MariaDB/MySQL, Microsoft SQL Server and Oracle.

A number of NoSQL databases are supported, among them MongoDB, Cassandra, Elastic Search, DynamoDB, Neo4j and Redis. Each has its own dedicated Quarkus module. MongoDB additionally works with Panache.

Cloud

Quarkus provides a dedicated extension for generating Kubernetes, OpenShift and Knative resources, configuring them, and using the Kubernetes API. A separate extension for OpenShift wraps Kubernetes and container-image-s2i. Taken together, the OpenShift–Quarkus integration gives you a strong toolset for managing and deploying applications in a Kubernetes environment.

Major cloud providers such as AWS, Azure and GCP have integrations that make their services easier to use; one of them is Funqy. Funqy offers a shared interface for writing functions that can be deployed to different serverless platforms, including AWS Lambda, Azure Functions, Google Cloud Functions, Knative and Knative Events (Cloud Events).

Micrometer is a tool for tracking and measuring how your application performs. It gathers runtime metrics and exposes them via an OpenMetrics-compliant endpoint, which Prometheus can scrape to monitor and analyze them.

SmallRye Strock is the only standalone service discovery option in Quarkus, but on a cloud platform such as Kubernetes you can also rely on the service discovery the platform itself offers. Just as in Micronaut, Spring Cloud Config Server is supported, so you can replace the Spring ecosystem step by step.

For telemetry and tracing, you can pick OpenTelemetry or OpenTracing, but OpenTracing is now deprecated and its developers advise against it.

Summary

Quarkus is a flexible framework that integrates nicely with other RedHat tools. If you already know the RedHat tooling or intend to use it in your project, Quarkus could be a sensible pick. Note also that Mandrel, a Quarkus subproject, may bring extra features or improvements later on that could set it apart from frameworks built on plain GraalVM.

Build Tools

Every framework supports Maven and Gradle as standard build tools, and some add extra plugins for building cloud-native applications. One capability they all share is building native images with Buildpacks. Building native images with Buildpacks requires Docker, but no native tools need to be installed by hand.

Micronaut and Quarkus offer plugins that integrate with native tools, but those tools must be installed manually. Spring Native has no built-in native tools support, although there are samples demonstrating how to use them. Bear in mind that native tools cannot do cross-compilation, so you have to install them on every platform you want to build for.

What impresses me about Quarkus is that it supports two more ways of building containers: Jib and S2i, the latter of which can build containers on an OpenShift cluster.

Project Bootstrapping

Each framework has its own ways of creating a new project, plus extra tools and features that make day-to-day work on it more convenient.

The starter website

Every framework has a simple website that generates a zip file with a project template. On these sites you can set the project name, the framework and Java versions, the programming language, the build tool and any extra modules that extend the framework. None of them stands out here, since they all provide much the same features.

Jetbrains Intellij

In JetBrains’ popular IDE, only the Ultimate edition can create these projects directly, without any extra plugins. It also comes with additional features that speed up navigating and developing the project.

Quarkus is the only one of the frameworks with an official plugin for IntelliJ Community Edition (CE), named “Quarkus Tools”. Built by RedHat, this plugin makes it easy to create new Quarkus projects and offers code assistance. The IntelliJ plugin manager also lists community-made plugins for Spring Boot and Quarkus, but I was unable to find any for Micronaut.

CLI

Besides tooling for creating projects, the frameworks also provide a range of commands for testing and deploying applications. These commands go further than project generation and add extra features for managing and working with your projects.

GraalVM

All of the frameworks rely on GraalVM, so they all share the restrictions of native image compilation. We give up the greatest strength of the JVM (Java Virtual Machine) - write once, run anywhere. In serverless environments, however, this drawback matters little, because images are usually containerized, for example in Docker. The remaining restrictions deserve attention, as they affect much more how we write our native applications.

  • The native image includes only the code reached by static analysis starting from the application’s entry point. Everything else is dropped, unless you specify in hint files which parts of the application have to be kept. The GraalVM Hint Processor can generate such files automatically from annotations. These restrictions mostly concern mechanisms like reflection, dynamic proxy, serialization and dynamic class loading. So before adopting a library, verify that the features you rely on work in native images.
  • The classpath is fixed at build time and cannot be modified afterwards
  • Native Image does not support JVMTI, Java Agents, JMX, JFR or other bytecode-based tools
  • Certain deprecated methods, for example Thread.stop() from Java.lang. Thread, are not supported

This is only a selection of native image limitations – the ones I consider most important. For a fuller description, go straight to the developer’s website. Remember that the limitations evolve as GraalVM develops, so it pays to keep up with them.

Documentation

In my experience, the Spring and Micronaut documentation is the most complete for learning how each framework works and spotting the differences. The Quarkus documentation, I believe, falls well short. What works best for me is detailed documentation that forms a coherent whole, structured and ordered like a book. It reassures me that if I read it all in the sequence the authors intended, I will not overlook anything important. Tutorials with examples should only complement the documentation, not form its core.

Credit goes to Micronaut for an entire section devoted to the private API, explaining what happens under the hood. The mechanisms are also described in enough detail. Because of that, I have never had to dig into the project’s source code to understand more deeply how the framework’s mechanisms work. They also run their own webinar series, which is available on YouTube.

In Quarkus, the documentation is built around tutorials. Moving between them is rather frustrating, and no clear learning path is laid out. The documentation does not explain how the framework works internally, and I am not alone in noticing this: a dedicated issue was opened on GitHub to address it. Sadly, it has remained open for 2 years, and nothing suggests the developers intend to extend the documentation in that direction.

One example of what Quarkus leaves unsaid is the vague explanation of bean scope, part of the core module. Quarkus has a bean scope expressed by the @Singleton annotation. It resembles the @Scope(“singleton”) familiar from Spring, with one key difference: it does not use client-proxy. This made me wonder whether decorators or interceptors work with the @Singleton scope. The documentation did not answer that, so I had to check it myself with a debugger. It turned out that with annotations like @Transactional a proxy is indeed used, yet a subclass proxy is not mentioned anywhere apart from the source code of the AOT generators.

Summary

I find it hard to declare any of these frameworks the clear winner. As with everything in our field, it depends, since every project has its own requirements and priorities. For one team, the particular implementation of a given feature may be decisive; for another, what matters most is confidence that the maintainer will keep developing and supporting the framework for many years. Either way, before picking a framework, list your requirements and use them to decide which option suits you.

That wraps up the introduction to the series. If you think something is missing or would like us to go deeper into a specific topic, tell us in the comments.