GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)

alinayurenko 468 views 37 slides Jun 04, 2024
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

This session covers GraalVM & Spring Boot, Spring AOT, performance optimizations and using libraries


Slide Content

Going AOT: Everything You Need To Know About GraalVM for Java Applications Alina Yurenko

Alina Yurenko / @alina_yurenko Developer Advocate for GraalVM at Oracle Love open source and communities 🤝 Love both programming 👩 ‍ 💻 & natural languages 🗣 About me @alina_yurenko

GRAALVM

An advanced JDK with ahead-of-time Native Image compilation

native-image MyMainClass ./mymainclass JIT AOT java MyMainClass

2017 Year 2018 2019 2020 2021 2022 2023 2005 2011 ... ... Sun Labs’ Maxine Java JVM initial release December Native Image open sourced April Twitter uses Graal JIT for core microservices October Micronaut 1.0 with Native Image support April GraalVM 1.0 released April Thomas Wuerthinger joins Oracle and starts Graal compiler project March Spring Native goes beta November Quarkus 1.0 with Native Image support May GraalVM goes GA (19.0 release) September MicroDoc announces GraalVM for embedded July Alibaba deploys Native Image in production July Facebook deploys GraalVM in production May AWS SDK support for Native Image November Spring Boot 3 with Native Image support December OpenJDK Galahad project proposed July Google Cloud Platform SDK support for Native Image June GraalVM Free Terms and Conditions license September GraalOS & Layered Native Image announced March Azure SDK support for Native Image September Oracle Cloud runs services on GraalVM Graal History 🎉 🎉 🎉 🎉 @alina_yurenko

GraalVM Native Image AOT Compilation Ahead-of-Time Compilation Application Libraries JDK Substrate VM Points-to Analysis Run Initializations Heap Snapshotting Input: All classes from application, libraries, and VM Code in Text Section Image Heap in Data Section Image Heap Writing Output: Native executable @alina_yurenko

DEMO 🚀

GraalVM Native Image—Fast Start Up and Much More Improved Security Fast Start & Scale Lower Resource Usage Compact Packaging Predictable Performance Supported Azure AWS GCP OCI @alina_yurenko

SPRING AOT

NATIVE IMAGE BUILD-TIME DISCOVERY AND CONFIGURATION SPRING BOOT RUN-TIME DISCOVERY AND CONFIGURATION

SPRING BOOT NATIVE IMAGE SPRING AOT

SPRING AOT

SPRING AOT JAVA SOURCE FILES BYTECODE HINT FILES

Libraries, dynamic Java features, and Native Image Libraries might be Native-Image friendly out of the box twitter.com/YunaMorgenstern/status/1729039787351536084 Libraries might include config for Native Image: github.com/h2database/h2database/blob/master/h2/src/main/META-INF/native-image/ Libraries might contain config in the Reachability Metadata Repository github.com/oracle/graalvm-reachability-metadata/tree/master/metadata/io.netty You can use framework support to produce custom “hints” for Native Image runtimeHints.resources().registerPattern(“config/app.properties”); You can use the Tracing Agent to produce the necessary config automatically graalvm.org/latest/reference-manual/native-image/metadata/AutomaticMetadataCollection/ You can provide/extend config for reflection, JNI, resources, serialization, and predefined classes manually in JSON: graalvm.org/latest/reference-manual/native-image/metadata/#specifying-metadata-with-json Happy path; most of the cases Custom code/ libraries @alina_yurenko

MORE DEMOS:)

Ready for GraalVM Native Image graalvm.org/native-image/libraries-and-frameworks @alina_yurenko

GraalVM Native Image & JUnit @EnabledInNativeImage used to signal that the annotated test class or test method is only  enabled  when executing within GraalVM native images when applied at the class level, all test methods within that class will be enabled within a native image @DisabledInNativeImage  used to signal that the annotated test class or test method is only  disabled  when executing within a GraalVM native image.

PERFORMANCE

Spring PetClinic: Peak throughput @alina_yurenko

Spring PetClinic: Memory Efficiency @alina_yurenko

Spring PetClinic Performance on Oracle GraalVM GraalVM CE with C2 JIT Oracle GraalVM Native Image Memory Usage (max RSS) 1,029 MB 641 MB Peak throughput 11,066 req/s 11,902 req/s Throughput per memory 12,488 req/(GB*s) 18,569 req/(GB*s) Tail latency (P99) 7.2ms 5.15ms Startup 7,090ms 210ms -38% lower +8% higher +49% better -28% lower 34x faster @alina_yurenko

BEST PRACTICES

95.5% container image size reduction JDK’s Simple Web Server GraalVM Native Image JVM Compact Packaging @alina_yurenko

Copyright © 2024, Oracle and/or its affiliates Native Linking and Containerization Options Application Code Fully Dynamic OS must include all dynamically linked libs libc stdc++ Distroless Java base ... z Mostly Static OS only need provide libc. Application Code stdc++ ... z libc Distroless base Fully Static No libs provided by OS Application Code musl libc stdc++ ... z scratch ~0 MB 20 MB 33 MB

Cross-Platform Builds on GitHub Actions Windows Executable macOS Executable Linux Executable .class .jar .class .jar GraalVM GitHub Action   🏗️ @alina_yurenko

Build container images with GraalVM and Buildpacks Add to your pom.xml: <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <!-- ... --> <buildpacks> <buildpack>docker.io/paketobuildpacks/oracle</buildpack> <buildpack>urn:cnb:builder:paketo-buildpacks/java-native-image</buildpack> </buildpacks> </image> </configuration> </plugin> Build the image: ./mvnw -Pnative spring-boot:build-image @alina_yurenko

Reduced attack surface area due to dead code removal—unused classes, methods, and fields not included in executable Improved Security Not vulnerable to JIT compiler attacks all code is AOT compiled SBOM supporting industry standards Embedded in executables CycloneDX format Not vulnerable to deserialization attacks via class loading—executable includes only required and specified classes Reduced Attack Surface 🛡️ @alina_yurenko

Migrate 🚀 Add Native Build Tools Alternatively, use recent versions of frameworks Evaluate libraries: graalvm.org/native-image/libraries-and-frameworks Build and deploy 👷‍♀️ Build and test on GraalVM as the JVM, build with Native Image closer to the deployment Quick build mode with `-Ob` Use CI/CD systems (e.g. GitHub actions) for deployment and cross-platform builds Run faster 🚀 PGO ML-enabled PGO G1 GC `-march=native` Recommendations @alina_yurenko

WHAT’S NEXT?

Java 22 features The fastest GraalVM yet :) Developer experience improvements Learn more: medium.com/graalvm GraalVM for JDK 22 🚀 @alina_yurenko

Demo 🚀

Layered Native Images Micronaut base Micronaut extensions (Web, Data, Test) Spring base App 1 App 2 App 3  Deployment: resources sharing ☁️ JDK base Application code Development: fast recompilation 🚀 JDK base + Micronaut base+ all extensions App 4 @alina_yurenko

GraalOS—Advanced cloud native application deployment platform Fast Start GraalOS applications start fast with virtually no cold start cost Low Latency Excellent 99th percentile latency makes GraalOS applications highly responsive Run On Demand GraalOS applications are automatically suspended and resumed on demand—with no idle cost Applications, not Containers GraalOS uses the latest advances in x86 and AArch64 processor architectures for hardware enforced application isolation without containers Reduced Memory GraalOS applications require significantly less memory resulting in reduced operating costs Cloud Native With support for stateful and stateless services and functions, GraalOS is ideal for cloud native applications Runs applications as small, fast GraalVM Native Image compiled machine executables @alina_yurenko

Compact Packaging GraalVM Native Image—Ideal for Cloud Native Applications Improved Security Fast Start & Scale Lower Resource Usage Predictable Performance Supported Azure AWS GCP OCI @alina_yurenko

graalvm.org sdk install java 22.0.1-graal Get started with GraalVM 🚀 github.com/graalvm/graalvm-demos docker pull container-registry.oracle.com/graalvm/jdk:22 @alina_yurenko

Questions & let’s connect! Demos 👩‍💻 @alina_yurenko