Java microservices with GraalVM

  • Microservice is a trending and popular architecture to build applications as it allows organizations to be agile and adopt DevOps and continuous testing practices.
  • Spring Boot is a very popular technology choice to build java based microservices, but it consumes a good amount of memory and spring’s runtime reflection approach leads to longer startup times as well
  • Large memory footprint and high start time are not good traits for cloud native applications and therefore in the recent times Go and Node have become a popular option to build microservices because of the limitations of traditional JVM based services
  • GraalVM is a Java VM implemented in Java based on HotSpot/OpenJDK and it allows to write efficient, more secure, and instantly scalable cloud native Java applications.
  • The advantages of using GraalVM to build microservices are
    • Faster startup time: Ahead-of-time compiled applications start in milliseconds and deliver peak performance with no warmup.
    • Low resource usage: Ahead-of-time compiled applications use only a fraction of the resources required by the JVM which means they cost less to run and improve utilization.
    • Small container image: Compact native executables can be packaged in lightweight container images for more secure, faster, and efficient deployments.
    • Minimize vulnerability: Native image reduces attack surface area by removing all unused classes, method, and fields from your application and libraries while making reverse engineering difficult by converting Java bytecode into native machine code.
  • Quarkus, Micronaut, Helidon, and Spring are some cloud native frameworks which have accepted GraalVM Native Image as a platform for their applications
  • Containers are an important element of cloud native technology stack, because packaging all the dependency required to run an application in a container reduces the setup time to scale services
  • Using GraalVM we can now compile the services to native code ahead of time in a way in which the resulting binary does not depend on the JVM for the execution. This executable can be placed as a standalone application in a container and started really, really fast.
  • GraalVM also supports multiple languages and interoperability between these languages, the containers can be running application code that is written in multiple languages.
  • The various scenarios of how containers can be deployed with GraalVM are:
  • Container A: In this case, we have an application compiled as native image. This is by far the most optimal configuration with the smallest footprint and a faster load.
  • Container B: In this case we have a java application and a javascript based application leveraging Truffle for interoperability but both using a common GraalVM platform
  • Container C: Similar to container B, but also has a C,C++ app which uses LLVM feature of GraalVM.
  • Details of GraalVM components are in my previous blog
  • Container A is the most optimal configuration for running cloud-native unless we have application code written in a different programming language that needs to interoperate. Another approach is to compile native images and split them into separate containers and use standard protocols such as REST to interact.

GraalVM native images aims to resolve the issues that we have with traditional JVM based services and provides a great opportunity to run Java applications in containers without loading the Java runtime.

This is a great feature for cloud based applications where where you want to autoscale your services or you have compute and memory constraints, such as in a function as a service (FaaS) environment.

Thanks , Share your feedback and comments !

One thought on “Java microservices with GraalVM

Leave a Reply