Slide 1

Slide 1 text

by João Esperancinha 2024/11/19 Start from the package! Spring CDS Class Data Sharing

Slide 2

Slide 2 text

About me João Esperancinha he/him/his ● Java ● Kotlin ● Groovy ● Scala ● Software Engineer 10+ years ● JESPROTECH YouTube Channel Kong Champion/Java Professional/Spring Professional

Slide 3

Slide 3 text

Today’s agenda ● The problem ● The solutions ● What is Class Data Sharing ● How the JVM works with CDS ● A bit of history ● Benefits of CDS in Spring Applications ● Configuring CDS in Spring Projects ● Real-world use cases and benchmarks ● Best Practices

Slide 4

Slide 4 text

What problems do we face today? ● Startup times! ● Auto-Scaling ● Deploy Times ● Constant Initializations

Slide 5

Slide 5 text

The Solution? Spring CDS! Class Data Sharing

Slide 6

Slide 6 text

Other solutions available ● GraalVM ● CRaC ● CDS

Slide 7

Slide 7 text

When and how CDS started? ● Java 1.5 Yes! That long time ago! Franz Ferdinand ● 2004 Green Day American Idiot Eternal Sunshine of a spotless mind The day after tomorrow ● 2018 – AppCDS (Application Class Data Sharing) ● 2021 – Dynamic CDS Archiving Billie Eilish Happier Than Ever Ariana Grande Sweetener Taylor Swift Red (Taylor's Version) Florence and the machine High as Hope The Guilty Red Sparrow

Slide 8

Slide 8 text

What is Class Data Sharing? CDS is a JVM feature that allows sharing of preloaded class metadata among multiple JVMs, reducing startup time and memory footprint. Class metadata is stored in a shared archive file, a .jsa file Enabled by default

Slide 9

Slide 9 text

Types of CDS Shared Archive with default JDK Classes App CDS where custom classes are used in specific applications

Slide 10

Slide 10 text

JVM and CDS Performance ● JVM skips loading classes into memory by reusing preloaded data. ● Reduces the Just-In-Time (JIT) compilation overhead during startup. ● Multiple JVMs can share the same metadata archive. ● Decreases memory usage on systems running multiple JVM processes. ● Faster context and dependency injection processing.

Slide 11

Slide 11 text

Benefits of CDS in Spring Applications ● Faster application startup for microservices and serverless functions. ● Lower memory usage on resource-constrained environments like Kubernetes. ● Supports efficient scaling of Spring applications with multiple JVM instances

Slide 12

Slide 12 text

Configuring CDS in Spring Projects ● Default for JDK classes in Java 12+. (2019) ● Use java -Xshare:on to explicitly enable. ● Use -XX:DumpLoadedClassList to generate a class list. ● Use -XX:SharedArchiveFile= to store or load the shared archive. Billie Eilish When We All Fall Asleep, Where Do We Go? Harry Styles Fine Line The Good Liar

Slide 13

Slide 13 text

Demo! Property Java Application Spring Application Startup wo/CDS (ms) 23.11 930 Startup wo/CDS unpacked (ms) NA 808 Startup w/CDS (ms) 29.27 430 Startup w/CDS + AOT (ms) NA 297 Committed memory wo/CDS 12.952 12.952 Committed memory wo/CDS unpacked(Mb) NA 12.952 Committed memory w/CDS (Mb) 8.080 55.640 Committed memory w/CDS + AOT (Mb) NA 53.880 Total classes wo/CDS 815 9209 Total classes wo/CDS unpacked NA 9096 Total classes w/CDS 818 8975 Total classes w/CDS + AOT NA 8704

Slide 14

Slide 14 text

Problem solved! ● Memory usage of Native Class Shared Space changes ● Faster application startup for microservices and serverless functions. ● Total memory of application usage can decrease with CDS ● Easy to use especially with Project Leyden and Buildpacks

Slide 15

Slide 15 text

● Kubernetes: ○ Reduce cold-start time for Spring Boot applications in containers. ● Serverless: ○ Improve function invocation times in environments like AWS Lambda. ● Microservices: ○ Optimize startup for clustered services in distributed systems. Real-World Use Cases

Slide 16

Slide 16 text

Best Practices ● Keep Archives Updated: ○ Recreate AppCDS archives after significant codebase changes. ● Containerized Applications: ○ Embed the shared archive in container images for portability.

Slide 17

Slide 17 text

Conclusion ● Key Takeaways: ○ CDS is a powerful tool to enhance JVM-based application performance. ○ Spring applications can significantly benefit from faster startups and lower memory usage. ○ Implementing AppCDS is straightforward and highly impactful.

Slide 18

Slide 18 text

Questions? I am an inquisitive cat

Slide 19

Slide 19 text

Resources ● https://blogs.vmware.com/tanzu/revolutionize-jvm-startup-times/ ● https://spring.io/blog/2024/08/29/spring-boot-cds-support-and-project-ley den-anticipation ● https://docs.spring.io/spring-framework/reference/integration/cds.html ● https://halilural5.medium.com/incredible-performance-increase-in-spring-b oot-applications-with-cds-5022ff81948f

Slide 20

Slide 20 text

Source code https://github.com/jesperancinha/jeorg-spring-master-test-drives/

Slide 21

Slide 21 text

About me ● Homepage - https://joaofilipesabinoesperancinha.nl ● Threads - https://www.threads.net/@joaofisaes ● LinkedIn - https://www.linkedin.com/in/joaoesperancinha/ ● YouTube - https://www.youtube.com/@jesprotech ● Bluesky - https://bsky.app/profile/jesperancinha.bsky.social ● Mastodon - https://masto.ai/@jesperancinha ● GitHub - https://github.com/jesperancinha ● Hackernoon - https://hackernoon.com/u/jesperancinha ● DevTO - https://dev.to/jofisaes ● Medium - https://medium.com/@jofisaes

Slide 22

Slide 22 text

Thank you!