Quick revision
The short answer
SnapStart is usually the first choice: it snapshots the initialized environment and restores it on a cold start, often bringing startup well under a second, but every restored copy shares whatever state was captured, so random seeds, unique ids, credentials, timestamps and open connections must be refreshed with runtime hooks. Provisioned concurrency removes cold starts for a fixed capacity at a constant cost, GraalVM native images start in tens of milliseconds at the price of slower builds and reflection limits, and plain tuning such as more memory and tiered compilation helps but rarely closes a 6-second gap alone.
What to remember
In this answer, we saw that a 6 to 8 second cold start breaks a 1 second p99 as soon as cold starts exceed about one percent of requests. SnapStart restores an initialized snapshot and is the cheapest big win, but shared snapshot state must be refreshed in afterRestore hooks and code should be primed in beforeCheckpoint. Provisioned concurrency buys guaranteed warm capacity at a constant cost, native images give the fastest start at an engineering cost, and memory and compiler tuning help every option.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
SnapStart is usually the first choice: it snapshots the initialized environment and restores it on a cold start, often bringing startup well under a second, but every restored copy shares whatever state was captured, so random seeds, unique ids, credentials, timestamps and open connections must be refreshed with runtime hooks. Provisioned concurrency removes cold starts for a fixed capacity at a constant cost, GraalVM native images start in tens of milliseconds at the price of slower builds and reflection limits, and plain tuning such as more memory and tiered compilation helps but rarely closes a 6-second gap alone.
Why this question is asked
A p99 target of 1 second means at most one request in a hundred may be slower than a second. If 2 percent of requests hit a cold start of 6 to 8 seconds, the target is broken no matter how fast warm requests are. This is a common situation for Spring Boot on Lambda, and the interviewer wants to see that we know the four main levers, what each costs, and especially the subtle correctness risks of SnapStart, which have caused real bugs such as duplicate ids. In this answer, we will see where the 6 to 8 seconds go, how SnapStart works and what can go wrong with it, how provisioned concurrency and native images compare, which tuning flags help, and how we would choose.
1Where the 6 to 8 Seconds Go
A cold start runs the init phase: the JVM starts, Spring Boot scans the classpath, creates the application context, wires beans through reflection, starts Jackson, Hibernate or the SDK clients, and JIT compilation has barely begun. For a typical Spring Boot function at 512 MB, most of those seconds are class loading and context creation, running on a fraction of a CPU.
We measure before choosing. Every invocation writes a REPORT line to CloudWatch Logs, and cold ones include Init Duration. A CloudWatch Logs Insights query over a day gives us how many invocations were cold and how long init took, which tells us whether the problem is 0.5 percent or 5 percent of requests.
2How SnapStart Works
With SnapStart enabled, Lambda runs the init phase once when we publish a function version, then takes a snapshot of the memory and disk of the initialized environment, encrypts it and caches it. When a cold start is needed, Lambda restores an environment from that snapshot instead of starting the JVM again, so the Spring context is already built. Logs then show Restore Duration instead of Init Duration, typically a few hundred milliseconds. It works on published versions and aliases, not on $LATEST, and it is available for the Java managed runtimes; runtime and region support and some limits, for example around very large ephemeral storage, change over time, so we check the current documentation.
- Publish versionLambda runs init once
- beforeCheckpoint hooksclose connections, prime code
- Snapshot taken and cachedmemory and disk of the environment
- Cold start: restore snapshotno JVM start, no Spring init
- afterRestore hooksreseed, refresh credentials, reconnect
- Handle the request
6 steps in order. Play them, or step through them one at a time.
The hooks are where we keep restored copies correct.
3The Correctness Risks of SnapStart
Here is the catch. One snapshot is restored into many environments, so anything computed during init is identical in every copy. A java.util.Random created during init has the same seed everywhere and produces the same sequence in each copy. A UUID or instance id generated during init is no longer unique. Time captured at startup, such as a cached clock value or a token expiry, is stale by hours or days. Temporary credentials fetched during init may expire. And network connections opened during init point to sockets that no longer exist after restore.
AWS documents which generators are safe after restore, and the JDK's SecureRandom in the managed runtime is handled, but libraries that seed their own generators are not. The fix is the CRaC API, Coordinated Restore at Checkpoint, which Lambda supports through the org.crac package: we register a resource whose beforeCheckpoint method runs before the snapshot and whose afterRestore method runs in every restored copy.
Notice that the beforeCheckpoint hook does two jobs. Closing connections avoids broken sockets, and priming, calling the main code paths once with a harmless request, means the snapshot contains loaded classes and some compiled code, so the first real request after restore is faster too.
4Provisioned Concurrency and Native Images
Provisioned concurrency tells Lambda to keep a number of environments initialized at all times. Requests up to that number never see a cold start, which makes it the most predictable option, but we pay for those environments every hour whether they are used or not, and traffic above the provisioned level still gets normal cold starts. It fits an API with steady, known traffic and a strict latency target, especially combined with scheduled scaling for business hours.
GraalVM native images take a different route. Spring Boot's AOT processing generates the bean wiring at build time, and GraalVM compiles the application ahead of time into a native executable, which we deploy on a custom runtime such as provided.al2023. Such functions start in tens of milliseconds and use less memory. The costs are build times of several minutes, a closed-world assumption where reflection, proxies and resources must be known at build time, so some libraries need extra configuration or do not work, and peak throughput that can be lower than a warmed-up JIT. It is the strongest option for startup and the most expensive in engineering effort.
| Option | Cold start | Main cost | Main risk |
|---|---|---|---|
SnapStart | Restore, often 200 to 700 ms | Little extra cost | Shared snapshot state |
| Provisioned concurrency | None up to the provisioned level | Paid even when idle | Spikes above it |
| GraalVM native image | Tens of ms | Build time, config work | Library compatibility |
| Tuning only | Maybe 30 to 50 percent less | Small | Rarely enough alone |
5Tuning That Helps Every Option
Some changes help whatever we choose. Raising memory gives more CPU, and class loading is CPU-bound, so going from 512 MB to 2 GB often cuts init time a lot. Setting JAVA_TOOL_OPTIONS to -XX:+TieredCompilation -XX:TieredStopAtLevel=1 makes the JVM use only the fast C1 compiler, which helps short-lived functions start and warm up, at some cost to long-run peak performance. Removing unused starters and dependencies, enabling lazy bean initialization and avoiding classpath scanning shrink the work done during init.
So how would we choose? For this function, we would first apply the cheap tuning and SnapStart with proper hooks, then measure Restore Duration and the p99. If restore plus the first request is still near the limit, or the business cannot accept any risk on the critical path, we add a small amount of provisioned concurrency for the base load. Native images are the choice when we need very fast scale-out from zero, or when the function is small enough that GraalVM compatibility is easy.
6Follow-up questions
What would you check in a library before enabling SnapStart?
We look for anything computed once at class load or init that must be unique or fresh: its own Random instances and seeds, generated ids, cached timestamps or clocks, cached credentials or tokens with expiry times, and network connections or connection pools opened at startup. We check whether the library already supports CRaC or documents SnapStart compatibility. For risky parts we register an afterRestore hook that resets them, or make the library initialize lazily, and we test by invoking several restored copies and comparing the values they produce.
Can we combine SnapStart and provisioned concurrency?
In general we pick one per function version, because provisioned environments are already initialized and gain nothing from a snapshot, and AWS has documented the two as not usable together. The rules can change, so we check the current documentation, but the design choice stays the same: SnapStart for cheap, good-enough cold starts, provisioned concurrency for guaranteed warm capacity.
Why does TieredStopAtLevel=1 help Lambda functions?
The JVM normally compiles hot code twice, first quickly with C1 and later with the optimizing C2 compiler, which costs CPU early on. A function that lives briefly on a small CPU share benefits more from fast C1 code now than from C2 code later, so stopping at level 1 shortens warm-up. For long-lived, busy functions we compare both settings, because peak throughput may drop.
7Conclusion
In this answer, we saw that a 6 to 8 second cold start breaks a 1 second p99 as soon as cold starts exceed about one percent of requests. SnapStart restores an initialized snapshot and is the cheapest big win, but shared snapshot state must be refreshed in afterRestore hooks and code should be primed in beforeCheckpoint. Provisioned concurrency buys guaranteed warm capacity at a constant cost, native images give the fastest start at an engineering cost, and memory and compiler tuning help every option.
Open with
First I would measure: the REPORT lines in CloudWatch show Init Duration on cold starts, so I know how often they happen and how long they take. Then I would compare four options.
Then, if they want more
SnapStart runs the init once when a version is published, snapshots the environment and restores it on cold starts, which usually brings startup to a few hundred milliseconds with almost no extra cost.
The risk is that every restored copy shares the snapshot's state, so a Random seed, a generated id, cached credentials, a captured timestamp or an open connection is the same, or stale, in every copy.
I would use the CRaC hooks: in beforeCheckpoint I prime the main code path and close connections, and in afterRestore I reseed, regenerate ids, refresh credentials and reconnect. Provisioned concurrency keeps a fixed number of environments warm, so there are no cold starts up to that level, but I pay for it constantly and spikes above it still go cold.
GraalVM native images with Spring Boot AOT on a custom runtime start in tens of milliseconds, but builds are slow and reflection must be declared, so library compatibility is work. Tuning helps all of them: more memory means more CPU, TieredStopAtLevel=1 through JAVA_TOOL_OPTIONS, fewer dependencies and lazy initialization.
My order would be tuning plus SnapStart with hooks, measure, and add a little provisioned concurrency only if the p99 still misses.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- AWS documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Java 21 Spring Boot function on AWS Lambda has cold starts of 6 to 8 seconds, and the...": https://scrivio-interview-library.netlify.app/spring-boot/aws/70c44df4d6/