Cloud native Java microservices with GraalVM native image

A presentation at Devoxx UK in November 2021 in London, UK by Oleg Šelajev

Slide 1

Slide 1

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

Slide 2

Slide 2

Developer advocate atomicjar.com testcontainers.org

Slide 3

Slide 3

What is GraalVM?

Slide 4

Slide 4

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

Slide 5

Slide 5

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

Slide 6

Slide 6

Slide 7

Slide 7

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

Slide 8

Slide 8

Native Image fundamentals java –jar … Run time

Slide 9

Slide 9

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

Slide 10

Slide 10

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

Slide 11

Slide 11

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

Slide 12

Slide 12

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

Slide 13

Slide 13

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

Slide 14

Slide 14

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/

Slide 15

Slide 15

Slide 16

Slide 16

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

Slide 17

Slide 17

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

Slide 18

Slide 18

Optimizing Docker images

Slide 19

Slide 19

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

Slide 20

Slide 20

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

Slide 21

Slide 21

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

Slide 22

Slide 22

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

Slide 23

Slide 23

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

Slide 24

Slide 24

fi Binary le

Slide 25

Slide 25

Testing GraalVM native image applications

Slide 26

Slide 26

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

Slide 27

Slide 27

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

Slide 28

Slide 28

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

Slide 29

Slide 29

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

Slide 30

Slide 30

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

Slide 31

Slide 31

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

Slide 32

Slide 32

Testcontainers 🐋 Database Test Application code Kafka API

Slide 33

Slide 33

Testcontainers 🐋 Database API tests UI tests App Kafka API

Slide 34

Slide 34

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

Slide 35

Slide 35

Monitoring GraalVM native image applications

Slide 36

Slide 36

Micrometer https://micrometer.io/

Slide 37

Slide 37

JFR recording (custom events mostly)

Slide 38

Slide 38

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

Slide 39

Slide 39

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

Slide 40

Slide 40

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/

Slide 41

Slide 41

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

Slide 42

Slide 42

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

Slide 43

Slide 43

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

Slide 44

Slide 44