Java never stops in its updates and improvements. These enhancements for better performance and easier development practice are initiated and led by the OpenJDK community, with quite a few exciting projects active currently. In this article, I want to touch upon the issue of slow startups in cloud-based complex Java applications and two initiatives aimed at finding the best solution to deal with this.
Let’s start with the issue itself. Despite the widely used expression ‘slow Java startup’, behind it we mean not exactly Java, but a delayed JVM warmup in complex frameworks that affects the performance and, as a consequence, increases the costs and resource consumption.
The following characteristics all relate to the slow startup problem: Short runs, frequent restarts, low CPU limits and deployment to multiple replicas.
There are a few solutions available to remove or decrease the effect of a slow startup, but hopes are high for the Leyden and Coordinated Restore at Checkpoint (CraC) projects, which I will discuss in this article.
Coordinated Restore at Checkpoint at Work
CRaC is an OpenJDK project that defines a new Java API to allow you to checkpoint and restore an application on the HotSpot JVM. It is aimed at resolving the slow startup issue.
The CRaC originates from Checkpoint and Restore in Userspace (CRIU). CRIU is a technology for Linux that uses ptrace kernel interface to freeze a running application for a snapshot and later to restore the application from the saved files.
The existing OpenJDK CRaC implementation includes CRIU and adds restrictions on the restore process for Java applications. More precisely, with CRaC, all connections must be closed before checkpoint, which makes the CRaC procedure in Java more reliable.
In action, CRaC follows a simple routine: You start your application on a CRaC-enabled version of the JDK runtime. Currently, two OpenJDK vendors deliver Java runtimes with CRaC support. At some point, when workloads pass and make your JVM hot, a checkpoint based on an API call gets activated. Here, CRaC creates a snapshot of the running JVM, which further allows fast restoration, potentially on another machine with a similar operating system and CPU architecture. The restored process preserves all the competencies of the HotSpot JVM, including further JIT optimizations at runtime.
Using Java runtime with CRaC feature, Java containers with CRaC support reduce startup time significantly and lower memory pressure in this process.
Nowadays, CRaC is gaining popularity as it is already available in ready-made supported OpenJDK runtimes and even Linux-based containers. All that is needed is to download these open-source products and your development practice is complete with an increased level of sustainability, functionality and performance, and just like that you are free from a slow startup.
However, there is another way to deal with the issue of slow startup, represented by Project Leyden.
Leyden Project
Leyden Project was launched by the OpenJDK community in 2022 to improve startup time, peak performance time and the footprint of Java programs. Its goal is similar to the CRaC Project, but it uses a different approach — a concept of static images. A static image is a standalone program that runs only the application it is derived from. Static images work under a closed-world assumption, i.e., they cannot load classes outside an image or generate new bytecode at runtime. These features enable the compilation of Java code to native executables, similar to GraalVM functionality.
Static images are not a universal solution, but they help to minimize startup and execution times and can lower the cost of cold starts, consequently decreasing cloud resource consumption. Project Leyden utilizes a few JDK components: HotSpot JVM, the jaotc ahead-of-time compiler, application class-data sharing (AppCDS) and the jlink utility.
In 2024, Project Leyden delivered ‘premain’ optimizations (basically, Class Data Sharing + Ahead-of-Time (AOT) on steroids), and as the Spring team informs, “they allow synergy with the Spring AOT optimizations, already capable of providing a 15% faster startup time on the JVM.”
These results are yet too early to consider, as it is not the final solution but an experimental phase. Nevertheless, there are great expectations from Project Leyden. It can bring extra advancements that are currently not available in CRaC, namely:
- Cross-platform solution, with no OS dependency. After all, CRaC is a great feature, but it relies on CRIU and Linux
- Stateless implementation and complete transparency for the application (no additional support in frameworks is needed)
- Additional optimizations expected to arrive from the closed world.
The above-stated aspects of Project Leyden with their full successful realization can make Leyden solutions game-changing for the Java world!
Resume
Unlike Project Leyden, the CRaC feature has already been delivered for JDK 21 and 17 and small Java containers. It will gain more popularity in the Java community as a ready-made simple technology to immediately deal with slow startup consequences and turn your Java development practice sustainable as well as cost-effective.
Leyden, though, has all the potential to deliver better results in performance and speed, and the community is following the project closely. Yet, we can expect the working tools provided by Leyden by 2025 as per the best estimation.