Opens in a new tab
vmblog logo 2024 wht (updated)

CRaC VS Leyden: Comparison of two approaches to remove slow startup in Java

Share: 

David Marshall | Published: June 19, 2024

Java never stops in its updates and improvements, those enhancements for better performance and easier development practice are initiated and led by the OpenJDK community, with quite a number of exciting projects being active right now. In this article, I want to touch upon the issue of slow startup in cloud-based complex Java applications and two initiatives that are fully aimed at finding the best solution to deal with this.

Let’s start with the issue itself. Despite the widely spread 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 the main hopes are for Leyden and the Coordinated Restore at Checkpoint projects, which I will discuss in this article.

Coordinated Restore at Checkpoint at work

Coordinated Restore at Checkpoint (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 CRIU (Checkpoint and Restore in Userspace). 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 and file handles must be closed before checkpoint, which makes the CRaC procedure in Java more reliable.

In action, CRaC follows the simple routine: you start your application on a CRaC-enabled version of the JDK runtime. At some point of the Java application start, when some 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 significantly reduce startup time and lower memory pressure in this process.

Nowadays, CRaC is gaining popularity as the feature 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 you are out of a slow startup. However, CRaC is not a standard for Java workloads, and not every OpenJDK runtime supports this technology.

There is another way to deal with the issue of slow warmup, represented by Project Leyden. The project deserves close observation as its preliminary achievements look very promising for the community.

Leyden Project

Leyden Project was launched by the OpenJDK community in 2022 to improve the startup time, time to peak performance, and footprint of Java programs. Its goal is similar to the Coordinated Restore at Checkpoint Project, but it uses a different approach – a concept of static images. A static image is a standalone program that runs only an application it is derived from. Static images work under the 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 + AOT on steroids), and as the Spring team informs, “they allow synergy with the Spring Ahead-Of-Time optimizations, already capable of providing a 15% faster startup time on the JVM.”

These results are yet too early to consider, as it is still not a final solution but an experimental phase. Nevertheless, there are great expectations from Project Leyden.

It is capable of bringing extra advancements not available in CRaC solution, 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 into a sustainable one 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 in the best estimation.

##

ABOUT THE AUTHOR

Alex Belokrylov 

Alexander Belokrylov is the CEO and co-founder of BellSoft. Alexander’s Java journey started at Sun Microsystems and was followed by his Oracle experience, which helped to establish Alexander as one of the most influential Java thinkers and a significant contributor to Java’s evolution and enterprise adoption.

Since it was founded in 2017, BellSoft has been among the leading OpenJDK contributors and is focused on optimizing operational costs for organizations relying on Java technologies. The BellSoft flagship started from its Liberica JDK and has always followed modern environment demands, and business and Java community initiatives, helping the company to stay on top of current trends. The most recent BellSoft product, Alpaquita Cloud Native Platform, follows this line precisely while pursuing the goals of reaching better, safer, and cheaper OpenJDK performance in the clouds. Alpaquita Cloud Native Platform is designed to make Java cloud-native and deliver a sustainable model of Java development.

BellSoft continues to receive continuous recognition from the global Java community and from enterprises working on Java. Liberica JDK is the runtime of choice for leading ISVs such as VMWare and JetBrains. BellSoft products help enterprise customers in an extensive range of industries like finance, stock trading, logistics, telecommunications, and more make their development practice sustainable, secure, and modern.

Alexander Belokrylov is a member of the Forbes Technological Council and a contributor to the DZone.