Speeding Up Spring Boot: A Journey from 40-Second cold starts to Sub-2.5 Second startup time with GraalVM

Gajula Meher
4 min readDec 15, 2023

--

Yes, you read it correctly, this isn’t clickbait. The cold start time for our application, initially exceeding 40 seconds, has now been significantly reduced to under 2.5 seconds.

Photo by Saffu on Unsplash

This improvement is for a Java Spring Boot application deployed on Google Cloud Run. Therefore, this article will delve into the specifics of optimizing startup times on Google Cloud.

Here’s how you can do it too.

  1. Quick Fix : Use Startup CPU boost on Google Cloud Run

If you want to quickly bring down the cold start time without changing any line of code in your application, then the first thing to do would be to enable this flag in the Cloud Run configuration.

You can enable this in cloud run’s settings
After enabling CPU Boost on Cloud Run on Sept 8th, the cold start latency was reduced by 50%

We weren’t using the CPU boost feature earlier and since traditional JAVA applications generally have a lot of startup tasks, CPU boost temporarily increases CPU allocation during instance startup in order to reduce startup latency.

2. Optimizing Java applications for Cloud Run

Google provides a comprehensive documentation outlining the steps to optimize your Java applications for the cloud. Although, I have tried the recommendations such as minimizing the container image, using Jib plugin, changing the JVM arguments for a better memory footprint and the other listed below, I haven’t found any massive improvements in the Cold start times like the CPU boost did. Expect anywhere between 10–20% reduction in latency at max.

This is when I have come across GraalVM — Building cloud native images.

3. GraalVM: Native Image

The video below is the best thing I found on the internet which explains what GraalVM is what it does. If you do not have the time to watch it, let me summarise it for you.

GraalVM is a high-performance JDK which includes an advanced optimizing compiler that generates machine code while the program is running JIT to accelerate Java application performance. By compiling ahead-of-time into standalone binaries, the native image starts up fast and uses less memory, making it ideal for cloud native deployment.

If you didn’t fully understand it, watch the video for a more comprehensive understanding of GraalVM and its functionalities.

4. My Journey to the Native Image

The application that I was working on is on Spring Boot 2.5 and I was trying to build a Native Image using GraalVM JDK and the native-maven-plugin. It took me around a month to actually build my first native image.

It should take you less than a week to fully understand what’s happening and build you own image if you avoid the mistakes that I did.

I will create a comprehensive video on building native images for a Spring Boot application, but for this article I will keep it short as to how I was able to bring down the cold start times.

  • I have started off with migrating my application to Java 17 since it’s compartively newer and also a lot of people were building their native images with it (Community Support is important).
  • I had a lot of trouble to make Spring Boot 2.5 work with GraalVM. So I have migrated to Sprint Boot 3.0 and removed all the unwanted dependencies.

As a result of the above two and the recommendations that I used from google. My cold start latency metrics look like this:

The cold start has come down to 16 seconds from 22 seconds.
It’s worth noting that this improvement is likely a result of eliminating unnecessary dependencies rather than upgrading the Spring Boot version.

After dedicating numerous hours to comprehending the packages initialized during both build time and run time, configuring reflection, and dealing with some strange and unexpected issues with dependencies, I’ve finally managed to create my native image.

The startup time now under 2.5 seconds. Which can open up opportunities to have minimum number of instances to 1, which leads to lower cloud operating costs and better user experience.

This is how I was able to reduce the cold start for our JAVA Application. I will be making a detailed video on how to build a native image using GraalVM as that will be more valuable to someone who’s trying to reduce their cold start latency.

You can initiate contact for potential opportunities or further discussions, including hiring inquiries, by reaching out to me. I appreciate your time in reviewing my article, and I am available for any additional information you may need.

--

--