How to use CRaC with Spring Boot in a Docker Container

Transcript

Coordinated Restore at Checkpoint (CRaC) is an OpenJDK project designed to reduce the startup and warmup times of Java applications to just milliseconds. This tutorial demonstrates how to use CRaC with Spring Boot applications running in a Docker container. To follow along, you’ll need Docker (ensure it’s up and running) and a Spring Boot application (version 3.2 or later) since CRaC is supported in these versions. For this example, we’ll use the Spring Boot Petclinic application, which can be found on GitHub. Additionally, we’ll use the Liberica Runtime Container with CRaC support, available on Docker Hub and tagged with “CRaC.”
Open the application in your preferred IDE (e.g., IntelliJ IDEA). No modifications to the application code are necessary—simply add the CRaC project dependency to the pom.xml file (assuming you’re using Maven). You’ll also need a Dockerfile for containerizing the application. In the first stage of the Dockerfile, we use the Liberica Runtime Container based on Liberica JDK Lite 21 and Alpaquita Linux with musl support. This container supports CRaC (indicated by the CRaC tag). Here, we copy the project into the image and package it as a JAR file. In the second stage, we use the Liberica Runtime Container based on JRE to create a layered JAR (recommended by Spring). Finally, we assemble all the layers into a new image using Liberica JRE with CRaC support and define the ENTRYPOINT to create a snapshot when the application exits.

Next, we containerize the application using the docker build command and run it in a container. To create a checkpoint, enable the CAP_CHECKPOINT_RESTORE and SYS_PTRACE capabilities using the --cap-add flag. If your Linux kernel version is older than 5.9, you can use the --privileged flag, though it’s not recommended for production. Once checkpointed, the application logs its activity using the CRaC API. Create a fresh image with a checkpoint using the docker commit command and update the ENTRYPOINT to restore the application from the snapshot. Remove the previous container (docker rm -f) and start the application from the snapshot, specifying the port and required capabilities. The application will start in milliseconds. You can launch multiple Petclinic instances in separate containers by specifying unique ports and container names. To verify the application, open it in a browser—it should run without issues. For copy-and-paste code snippets, refer to the dedicated guide linked in the description box of the video.

Summary

CRaC (Coordinated Restore at Checkpoint) is an OpenJDK project designed to significantly reduce startup and warmup times of Java applications to milliseconds. This tutorial demonstrates using CRaC with a Spring Boot application running in a Docker container, specifically the Spring Boot Petclinic app (version 3.2 or later).

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Dec 17, 2024
Master Java Profiling: Tools, Techniques, and Real-World Tips

Java profiling allows to rapidly identify and fix performance bottlenecks in your program. In this video we explain what is profiling, introduce popular profiling tools, list their pros and cons, and provide useful tips and code examples.

Videos
card image
Nov 29, 2024
OpenJDK Projects That We Anticipate

OpenJDK is actively evolving, with projects like Leyden, Valhalla, Babylon, and Lilliput aiming to enhance Java's performance and capabilities. Leyden focuses on faster startup and warmup by reusing precompiled code, while Valhalla introduces value objects, primitive classes, and specialized generics for better memory and runtime efficiency.

Further watching

Videos
card image
Jan 22, 2025
JEP 483: Ahead-of-Time Class Loading & Linking. Project Leyden in JDK 24

JEP 483 introduces Ahead-of-Time (AOT) Class Loading and Linking in JDK 24, which enhances Java application startup times by loading and linking classes ahead of time and storing them in a reusable AOT cache. This feature, part of Project Leyden, reduces the JVM's workload during startup without requiring changes to application code, though a training run mimicking production is needed to create an efficient cache. Early tests with a Spring Boot app showed significant improvements, cutting startup time from two seconds to just one second.

Videos
card image
Jan 14, 2025
How to use AppCDS with Spring Boot

This tutorial demonstrates how to use Application Class Data Sharing (AppCDS) and Ahead-of-Time (AOT) processing with Spring Boot applications to reduce startup time by 40–50%. AppCDS creates an archive of parsed classes for faster loading, requiring no code changes, and works both locally and in containers. The tutorial covers building optimized Docker images using Dockerfiles or Buildpacks for efficient deployment and improved performance.

Videos
card image
Dec 28, 2024
JDK 24: The New Features in Java 24

JDK 24 is in Rampdown Phase One, which means, we know all the JEPs targeted to this release. And there are a lot of them, so it is time to discuss this new Java release!