Java CRaC
Java is not a language which is famous for its fast startup-time when using a framework like Spring. Sometimes you need this when running time-sensitive applications or for example functions as a service which build on top of fast start-up times.
Luckily there’s some work regarding this like GraalVM or CDS.
Another feature which promises to solve this problem is built on top of Linux’ CRIU and is called Coordinated Restore at Checkpoint or abbreviated CRaC.
It’s only available for Linux machines and you’ll need a special JDK developed by Azul for that. So things are pretty experimental.
But: Major Java frameworks like Spring Boot or Quarkus are supporting it!
Here I take a look on first steps with CRaC and in a later post I try to show some further experiments with it.
What you need
What you need to use CRaC:
- A JVM which supports CRaC. Currently that feature works only for Linux!
- The
craclibrary in the classpath. For maven you can use:
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>
- Running your application with the specific CRaC commands.
Example usage
- Create a CRac compatible project. Or download one like the Spring Boot Example and build the jar.
- Download Azul’s JDK with CRaC support. You need a JDK which is supporting CRaC, as it’s right now only in the development stage.
- Unzip the JDK and set your
JAVA_HOMEvariable to it. You don’t need to add it to your.bashrcif you don’t want to use this special version every time.
export JAVA_HOME=/home/dennis/jdk/zulu21.30.23-ca-crac-jdk21.0.1-linux_x64
export PATH=$JAVA_HOME/bin:$PATH
- Give the CRIU lib the needed access rights. Otherwise it may fail to (re)store checkpoints.
sudo chown root:root $JAVA_HOME/lib/criu
sudo chmod u+s $JAVA_HOME/lib/criu
- Create a directory to store the CRaC files:
mkdir crac-store
- Run the application
Execute the application by using the downloaded JDK. Note the used arguments.
Without the -XX:CRaCCheckpointTo flag the checkpoint creation fails!
java -XX:CRaCCheckpointTo=/home/dennis/code/java/crac-store -jar example-spring-boot-0.0.1-SNAPSHOT.jar
- Create a checkpoint:
$JAVA_HOME/bin/jcmd target/example-spring-boot-0.0.1-SNAPSHOT.jar JDK.checkpoint
This will create a lot of files in the storage directory.
- Restore from the checkpoint.
java -XX:CRaCRestoreFrom=/home/dennis/code/java/crac-store
When you do this, you’ll see that the application start ups faster.
Things to consider
Be aware that any sensitive information of your application can be stored in the store. CRaC creates a snapshot of your application and to restore it in a correct way it needs all information. So that’s potentially also access keys etc.!