User Avatar

Cora Iberkleid 🚢

🍃Spring

1y ago

Java Startup: Not From Scratch Anymore, with CRaC

Java 21 introduces a feature that lets your applications start up and reach peak performance instantly, and Spring makes this feature very easy to use. (follow thread --> )

1/11

User Avatar

Cora Iberkleid 🚢

1y ago

Imagine if every time you opened your computer, you had to reboot it. Wait for the OS, open programs & files one by one, position windows just so. If it were me, I'd never close my laptop.

And yet, this is precisely what we do with our Java apps.

2/11

User Avatar

Cora Iberkleid 🚢

1y ago

For every start and every restart, we load all classes and warm up the app until we achieve peak performance, again. We have no choice.

Or rather, we had no choice, until now.

3/11

User Avatar

Cora Iberkleid 🚢

1y ago

Introducing CRaC:

Coordinated Restore at Checkpoint—or after some getting-used-to, CRaC—is a feature developed by Azul Systems that enables the JVM to start up from a warmed-up memory snapshot.

4/11

User Avatar

Cora Iberkleid 🚢

1y ago

The checkpoint is a serialized memory representation of your app, in a safe state, of course, with files, sockets and threads closed/stopped.

At restore, the application will have the same level of "warmness" as the checkpoint snapshot.

5/11

User Avatar

Cora Iberkleid 🚢

1y ago

Persisting CRaC:

The checkpoint is stored to disk. Local, Docker volume, Kubernetes volume. But perhaps the most interesting is embedding the snapshot as a layer into the app's Docker image itself. The app and the snapshot travel together, inseparably, all the way to production.

6/11

User Avatar

Cora Iberkleid 🚢

1y ago

Scaling CRaC (to Zero!):

Imagine: one slow start to create a snapshot, then every start thereafter is instantaneous with immediate peak performance. If starting is that efficient, why not scale to zero and start as needed? Ka-ching!

7/11

User Avatar

Cora Iberkleid 🚢

1y ago

CRaC with Spring:

With Spring Boot 3.2+, CRaC is mapped to the Spring Lifecycle. Checkpoints can be captured automatically (just before bean startup), or on-demand (after warmup).

8/11

User Avatar

Cora Iberkleid 🚢

1y ago

CRaC with Spring (by example):

# Example of automatic CRaC checkpoint capture with Spring

java -Dspring.context.checkpoint=onRefresh \

-XX:CRaCCheckpointTo=$CRAC_FILES_DIR \

-jar target/app.jar

# Restore

java -XX:CRaCCheckpointFrom=$CRAC_FILES_DIR

9/11

User Avatar

Cora Iberkleid 🚢

1y ago

Caveats:

- Only works with Linux.

- Checkpoint after warmup depends on adoption of CRaC by 3rd party libraries.

- Beware of storing in-memory secrets in snapshot. Use Spring's @RefreshScope to load different secrets on restore.

10/11

User Avatar

Cora Iberkleid 🚢

1y ago

Get CraC-ing!

In short, CRaC provides some serious motivation to upgrade to Spring Boot 3.2 and Java 21! Faster startups requiring less memory to start, instantly achieve optimal performance, portable snapshots, and ability to scale to zero. What are you waiting for?

11/11

The all-in-one writing platform.

Write, publish everywhere, see what works, and become a better writer - all in one place.

Trusted by 80,000+ writers