Cloud Native 10 min read

How to Cut Spring Boot Serverless Cold‑Start Time with Reserved Instances and Lazy Init

This guide walks through analyzing and optimizing cold‑start latency for a Spring Boot e‑commerce app on Alibaba Cloud Function Compute,{​} covering tracing, reserved instances, lazy initialization, JVM tuning, and instance concurrency settings to improve performance.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
How to Cut Spring Boot Serverless Cold‑Start Time with Reserved Instances and Lazy Init

Spring Boot, a Java framework that bundles many Spring components, enables developers to create standalone applications with minimal configuration. While it runs on various cloud‑native platforms such as VMs and containers, running it in a Serverless environment often suffers from cold‑start delays.

The author analyzes Serverless performance across five dimensions—architecture, deployment, monitoring, performance, and security—using the popular open‑source e‑commerce project mall (over 50k GitHub stars) as a case study. This fourth article in the series focuses on performance tuning.

Understanding Cold‑Start Phases

Cold start occurs when a function instance is reclaimed after idle time and must be re‑initialized for the next request. The author recommends enabling tracing in the Function Compute (FC) console to view detailed latency breakdowns, which include:

PrepareCode : downloading code or image (short if image acceleration is enabled).

RuntimeInitialization : from function start to port readiness, encompassing the Spring Boot startup time (observed via s mall-admin logs).

Initialization : custom logic placed in the FC Initializer interface.

Invocation : request handling latency (typically minimal).

Tracing screenshots show that the runtime initialization step is the main bottleneck.

1. Use Reserved Instances

Because Java applications start slowly and often need to contact external services, eliminating cold starts is crucial. FC offers reserved instances that stay warm regardless of traffic, at the cost of continuous billing. Users can configure minimum and maximum instance counts, schedule reservations, or set metric‑based rules via the console.

2. Speed Up Instance Startup

Lazy Initialization

Enable Spring Boot’s global lazy‑init flag (available from version 2.2) to defer bean creation until first use, reducing startup time at the expense of a longer first request.

SPRING_MAIN_LAZY_INITIALIZATION=true

Disable Optimized JIT Compilation

JVM JIT compilation improves long‑running performance but adds startup overhead. For short‑lived Serverless functions, disable higher‑level JIT stages:

JAVA_TOOL_OPTIONS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"

These environment variables are added to the s.yaml configuration for the function.

Deploy with Updated Settings

After setting the variables, redeploy the function using sudo -E s mall-admin deploy and verify the environment variables via the instance detail page and echo command.

Note: Non‑reserved instances may be reclaimed after idle periods, making the login button unavailable; therefore, inspect the instance before it is recycled.

3. Configure Reasonable Instance Parameters

Choosing appropriate CPU/Memory specs (e.g., 2C4G or 4C8G) requires balancing resource utilization and performance. FC uses Instance Concurrency (the number of simultaneous requests an instance can handle) as the primary scaling metric, which differs from raw QPS.

Advantages of Instance Concurrency:

Fast metric collection enables rapid auto‑scaling.

Provides a stable view of load, avoiding confusion when downstream services become bottlenecks.

To determine a suitable concurrency value, follow these steps:

Set the function’s maximum instance count to 1 to isolate a single instance.

Run load‑testing tools to measure TPS and latency.

Gradually increase the concurrency setting; if performance remains stable, continue increasing, otherwise reduce.

Reference Links

Spring Boot: https://spring.io/projects/spring-boot

Mall repository: https://github.com/macrozheng/mall

Serverless Devs CLI docs: http://serverlessdevs.com/zhcn/docs/installed/cliinstall.html

Alibaba Cloud Function Compute: https://www.aliyun.com/product/fc

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ServerlessSpring Bootjvm-tuningcold startlazy-initializationInstance ConcurrencyReserved Instance
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.