Cloud native Java microservices with GraalVM native image Oleg Šelajev @shelajev

Developer advocate atomicjar.com testcontainers.org

What is GraalVM?

Increased performance in Real-World Application Benchmarks https://renaissance.dev

Startup Speed Peak Throughput JIT AOT Reduced Max Latency Low Memory Footprint Small Packaging

Getting started in 5 easy steps 1. Download the archive 2. Unpack: tar -zxf … 3. export GRAALVM_HOME=… 4.export PATH=$GRAALVM_HOME/bin:$PATH 5.java -jar your-application.jar 6.gu install native-image

Native Image fundamentals java –jar … Run time

Native Image fundamentals native-image –jar … Build time ./application Run time

Native Image Build Process Input: All classes from application, libraries, and VM Application Output: Native executable Points-to Analysis Ahead-of-Tim Compilation Libraries Code in Text Section Run Initializations JDK Heap Snapshotting Substrate VM

e

Iterative analysis until fixed point is reached Image Heap Writing Image Heap in Data Section

Configuring Native Image build: Reflection, Proxies, JNI, Resources, etc https://medium.com/graalvm/working-with-native-image-efficiently-c512ccdcd61b

Configuring Native Image build: Reflection, Proxies, JNI, Resources, etc java -agentlib:native-image-agent= config-output-dir=META-INF/native-image -jar App.jar

https://medium.com/graalvm/working-with-native-image-efficiently-c512ccdcd61b

Class initialization! native-image –jar … Build time ./application Run time https://www.graalvm.org/reference-manual/native-image/ClassInitialization/

Class initialization! native-image –jar … ./application Build time Run time -H:+PrintClassInitializatio —initialize-at-build-time= —initialize-at-run-time=p.C1 n p https://www.graalvm.org/reference-manual/native-image/ClassInitialization/

Working on making native executables smaller -H:DashboardDump=dumpfileoversized -H:+DashboardAll https://medium.com/graalvm/making-sense-of-native-image-contents-741a688dab4d

Optimizing the binary size even further upx —best -k mynativeimage medium.com/graalvm/compressed-graalvm-native-images-4d233766a214 Copyright © 2021, Oracle and/or its affiliates

Optimizing Docker images

Optimizing Docker images • Use slim base images • Use Distroless base images • https://github.com/GoogleContainerTools/distroless • -H:+StaticExecutableWithDynamicLib • Use FROM scratch Docker image C • —static —libc=musl

Configuring memory usage for Native Image • • • • • -Xmx - maximum heap size in bytes -Xms - minimum heap size in bytes -Xmn - the size of the young generation in bytes -XX:+PrintGC - print basic information for every garbage collection -XX:+VerboseGC - can be added to print further garbage collection details At build time: • -R:MaxHeapSize - maximum heap size in bytes

Addressing Peak Throughput & Max Latency • G1 Garbage Collector option for native image for better max latenc • Profile-guided optimizations (PGO) for better peak performance Workloads Instrumented Binary native-image —pgo Profiles y native-image —pgo-instrument Optimized Binary

https://medium.com/graalvm/graalvm-21-3-is-here-java-17-native-image-performance-updates-and-more-ac4cbafcfc05

Startup Speed Peak Throughput JIT AOT Reduced Max Latency Low Memory Footprint Small Packaging

fi Binary le

Testing GraalVM native image applications

How we test Java applications? java –jar … Run time Testcontainers

The honeycomb https://engineering.atspotify.com/2018/01/11/testing-of-microservices/

The honeycomb 🤔 Testcontainers https://engineering.atspotify.com/2018/01/11/testing-of-microservices/

How can we test Native Image apps? native-image –jar … Build time ./application Run time

Native Build Tools https://github.com/graalvm/native-build-tools

Native Build Tools https://github.com/graalvm/native-build-tools

Testcontainers 🐋 Database Test Application code Kafka API

Testcontainers 🐋 Database API tests UI tests App Kafka API

🐋 https://medium.com/@bsideup/testing-your-docker-containers-with-testcontainers-and-groovy-3b9ef97ad1c2

Monitoring GraalVM native image applications

Micrometer https://micrometer.io/

JFR recording (custom events mostly)

Container based monitoring https://github.com/bcicen/ctop

Build tools • sbt native clien • • • mvnd https://github.com/mvndaemon/mvnd t •

https://www.scala-sbt.org/1.x/docs/sbt-1.4-Release-Notes.html https://asciinema.org/a/wAAYjiO0TNW0V7dCSebMQB5wa

GraalVM Native Image Support in the AWS SDK for Java 2.x The test application creates an S3Client with ApacheHttpClient and sends a simple GetObjectRequest. In our testing, we measured the SDK 2.x startup time and the first request latency as well as the memory usage. https://aws.amazon.com/blogs/developer/graalvm-native-image-support-in-the-aws-sdk-for-java-2-x/

Writing Kubectl plugins with Scala or Java, with Fabric8 Kubernetes client on GraalVM Rewriting a kubectl plugin from a standard Go stack to a more comfortable approach with Scala has been challenging and rewarding. We are really happy with the result, and we arguably improved it in several ways. https://www.lightbend.com/blog/writing-kubectl-plugins-with-scala-or-java-with-fabric8-kubernetes-client-on-graalvm

AOT native-image MyMainClas ./mymainclass JIT Java on Truffle java MyMainClass s java -truffle MyMainClass

JIT AOT Java cod Java code AOT + JIT Java code (AOT + VM Java HotSpot VM VM

  • Java on Truffl + Truffle + GraalVM JI (all of which i Java code) ) e s T e Java code (JIT)

Mixing AOT and JIT in Java programs