What is CRaC?
Tutorial
- In a nutshell
- Where does Java’s cold start time come from?
- How does CRaC solve this problem?
- Should I start using CRaC today?
- References
In a nutshell
The acronym CRaC stands for Coordinated Restore at Checkpoint. It is a technology developed by Azul to reduce Java’s cold start time. It was accepted as an OpenJDK project, and Azul released it as a separate distribution of their Zulu JDK. Currently, CRaC is not widely supported by tools and frameworks yet.
Where does Java’s cold start time come from?
When the JVM starts, the compiler looks for methods to optimize and pre-compile to improve performance. This happens in two stages: one at application start-up and one during the application’s lifetime, which continuously monitors for new hotspots to optimize. As a result, Java applications have a warm-up time before they get to their best performance.
How does CRaC solve this problem?
One popular approach to solve this problem is ahead-of-time compilation. You compile your Java application down to a native operating system binary. Using native binaries drastically improves start-up times, but you lose some JVM flexibility, such as portability, reflection, and dynamic class-loading. In addition, your application’s performance does not improve based on hotspots that the JDK would usually discover at runtime.
GraalVM is the best example of a project trying to implement AOT compilation for Java. Have a look at Marco Behler’s article on GraalVM and AOT compilation for more information.
CRaC takes a different approach. You deploy your application to a pre-production environment, you let the JVM warm up the application, possibly using a stress test, and once the JVM is warm, you create a checkpoint of your application’s optimized state. See the below image for a visual representation of these steps.
Once you have your CRaC-compatible checkpoint, you can deploy both your application and the checkpoint to production. At application start-up, the JDK loads the optimizations from the checkpoint, and you start serving requests without the JVM optimizing your application first. The image below depicts the production process.
Should I start using CRaC today?
CRaC looks promising, but I don’t recommend its use in production yet. Framework and tooling support is still quite limited, and you can’t have any open files or sockets at checkpoint time, making it challenging to create checkpoints in larger applications.
At the end of 2022, Amazon Web Services launched a CRaC-compatible technology called AWS SnapStart as part of AWS Lambda. Before using this technology, I recommend reading the documentation as you must consider some coding constraints to successfully use this technology for production workloads.