Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Your services must support CRaC checkpoint and restore or fast scale-to-zero. How does the Spring lifecycle model help, and what must service code do?

Spring's lifecycle model fits CRaC well: since Spring Framework 6.1, Spring stops all Lifecycle beans before a checkpoint and starts them again after the JVM is restored, so components that open sockets, files and threads in start() and release them in stop() survive checkpoint and restore with no extra code. Service code must follow that rule strictly, must not rely on @PostConstruct for anything that has to be redone after restore, must refresh state that goes stale such as credentials and clocks, and must test checkpoint and restore in CI because a missed resource only shows up when the checkpoint or restore runs.

Why this question is asked

Scale-to-zero and very fast scale-out are attractive: a service that starts in a few hundred milliseconds instead of several seconds can be stopped when idle and started on the first request. CRaC, short for Coordinated Restore at Checkpoint, is one way to get there on the JVM. It takes a snapshot of a running, warmed-up JVM and restores it later in a fraction of the normal startup time. Interviewers ask this at senior level because it tests whether we understand what state a running service holds and how Spring's lifecycle manages it. In this answer, we will recap how CRaC works, see how Spring's lifecycle support fits in, look at what service code must do, and then cover testing and the practical limits.

1A Quick Recap of CRaC

CRaC is a JDK feature, provided by CRaC-enabled builds such as Azul Zulu and BellSoft Liberica (it is not in every JDK, so check your distribution). We start the application normally, let it warm up, and then ask the JVM to take a checkpoint: it writes the whole process state, including the heap and compiled code, to a directory. Later, we start a new process from that directory, and it continues from the exact moment of the checkpoint.

There is one big constraint. The checkpoint cannot contain operating system resources that do not survive being frozen and moved: open network sockets, open files, and in some cases running threads holding such resources. The JVM refuses to take a checkpoint if such resources are still open, and it reports which ones. So the application must close them before the checkpoint and reopen them after restore. CRaC offers an API for this, the org.crac Resource interface with beforeCheckpoint() and afterRestore() callbacks.

2How Spring's Lifecycle Support Fits In

Spring Framework 6.1 added CRaC support built on the existing Lifecycle model. When the org.crac library is on the classpath and the JVM supports CRaC, Spring registers itself as a CRaC resource. Before a checkpoint, it stops all Lifecycle beans in the normal phase order, exactly as at shutdown but without destroying the singletons. After restore, it starts them again in phase order. Spring Boot 3.2 and later made components such as the embedded web server and connection pools cooperate (check your versions).

Spring also offers an automatic checkpoint at startup: with the system property spring.context.checkpoint=onRefresh, the JVM checkpoints right after the context has refreshed, before lifecycle beans start. That captures the expensive part of startup, bean creation, without any open connections. A manual checkpoint, triggered with jcmd after warm-up, captures more warmed-up state but needs the application to cleanly stop everything first.

Text
# Take a checkpoint after the application has warmed up
java -XX:CRaCCheckpointTo=/opt/app/cr -jar orders.jar
jcmd orders.jar JDK.checkpoint # Spring stops Lifecycle beans, JVM writes /opt/app/cr
# Or checkpoint automatically when the context has refreshed
java -Dspring.context.checkpoint=onRefresh -XX:CRaCCheckpointTo=/opt/app/cr -jar orders.jar
# Restore later, in a fraction of the usual startup time
java -XX:CRaCRestoreFrom=/opt/app/cr # Spring starts Lifecycle beans again
Step throughCheckpoint and restore with Spring
OperatorJVM (CRaC)Spring contextSocket-holding bean
jcmd JDK.checkpoint
beforeCheckpoint()
stop(): close socket, stop threads
write heap and code to disk
java -XX:CRaCRestoreFrom=...
afterRestore()
start(): reopen socket, start threads
serving again

8 messages in order. Play them, or step through them one at a time.

Spring reuses the same stop and start calls it makes for lifecycle beans at shutdown and startup.

3What Service Code Must Do

The rule for our own beans is that anything holding a socket, a file or a thread must acquire it in start() and release it in stop(), as a Lifecycle or SmartLifecycle bean. A bean that opens a TCP connection to a pricing feed in its constructor or in @PostConstruct breaks the checkpoint, because that connection is still open, and even if we close it somewhere, @PostConstruct will not run again on restore, because the bean is not created again. Restore simply continues the existing objects.

Java
// Broken for CRaC: the connection is opened once, during bean creation
@Component
class PriceFeedClient {
private Socket socket;
@PostConstruct void connect() throws IOException {
socket = new Socket("feed.internal", 9000); // still open at checkpoint -> checkpoint fails
}
}
// Works: the connection follows the lifecycle
@Component
class PriceFeedClient implements SmartLifecycle {
private volatile Socket socket;
@Override public void start() {
socket = openSocket("feed.internal", 9000); // at startup and again after restore
}
@Override public void stop() {
closeQuietly(socket); // before checkpoint and at shutdown
socket = null;
}
@Override public boolean isRunning() { return socket != null; }
}

The second version also behaves better at normal shutdown, so following the rule improves the service even without CRaC.

Open resources are not the only issue. State captured at checkpoint time can be wrong after restore: a cached access token that has expired, a database password that was rotated, the host name or IP address the service registered with discovery, a random seed shared by every restored copy, or cached time values. Anything like this must be refreshed in start() or in an afterRestore() hook. Secrets in particular should not be baked into a checkpoint image that might be copied around, so we fetch them after restore.

4Testing Checkpoint and Restore in CI

The failures CRaC causes are invisible in ordinary tests. A forgotten socket only shows up when the checkpoint is attempted, and stale state only shows up after restore, often minutes later when a token is used. So the CI pipeline should build the image, start the service, warm it up with a few requests, take a checkpoint, restore it in a fresh container, and run a smoke test against the restored copy, including calls that use every external connection: database, Kafka, downstream HTTP services.

Restoring two copies at once also catches state that must be unique per instance. Then the next library upgrade that opens a file during startup fails the build, not the first scale-out in production.

5When CRaC Is the Right Tool

CRaC shines for services with heavy startup and long-lived state, where restoring a warmed JIT and a full context saves several seconds per start. Its costs are a CRaC-enabled JDK and Linux environment, checkpoint images that must be stored and kept in step with each build, and the discipline described above. The alternatives are a GraalVM native image or keeping a few warm instances. For scale-to-zero, the deciding numbers are usually how long a cold start may take before the first request times out and how often scaling happens.

6Follow-up questions

Does a restored application run @PostConstruct again?

No. Restore continues the existing JVM state, so every bean already exists and no constructor or @PostConstruct runs again. Only Lifecycle start() methods, and CRaC afterRestore() callbacks, run after restore. That is why work that must be repeated has to live in start().

What happens if a bean forgets to close a file before the checkpoint?

The JVM refuses to take the checkpoint and reports an exception naming the open file or socket, so the checkpoint step fails. That failure is actually helpful, which is why the checkpoint step belongs in CI. Problems with stale state, by contrast, do not stop the checkpoint, so they need the restore and smoke test stage to be caught.

How is this different from scaling to zero with a native image?

A native image is compiled ahead of time and starts quickly every time without a stored snapshot, but the build is slower, reflection and dynamic proxies need hints, and peak performance may be lower without a JIT. CRaC keeps the normal JVM and its JIT and restores a warmed process, but needs a CRaC-capable JDK and stored checkpoint images. Both rely on clean lifecycle handling.

7Conclusion

In this answer, we saw that CRaC snapshots a running JVM and restores it quickly, but cannot keep open sockets, files and some threads. Since Spring Framework 6.1, Spring maps checkpoint and restore onto its Lifecycle model, stopping lifecycle beans before a checkpoint and starting them after restore. Service code must therefore acquire such resources in start() and release them in stop(), never rely on @PostConstruct for work that must be redone, refresh stale state like tokens and secrets after restore, and test checkpoint and restore in CI.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring's lifecycle model is exactly what CRaC needs. CRaC takes a snapshot of a warmed JVM and restores it later in a fraction of the startup time, but it cannot snapshot open sockets or files, so they must be closed before the checkpoint and reopened after restore.

Then, if they want more

Since Spring Framework 6.1, when the org.crac library is present and the JDK supports it, Spring stops all Lifecycle beans in phase order before a checkpoint and starts them again after restore, without destroying the singletons. Spring Boot's web server and pools already cooperate. There is also spring.context.checkpoint=onRefresh to checkpoint automatically right after the context refreshes.

For our own code the rule is: anything holding a socket, a file or a thread must open it in start and close it in stop, as a SmartLifecycle bean. Doing it in @PostConstruct is wrong, because restore does not recreate beans, so @PostConstruct never runs again.

We also have to refresh state that goes stale, like tokens, rotated secrets, instance ids and random seeds, and we should not bake secrets into the image. And I would make checkpoint, restore and a smoke test a CI stage, because missed resources only show up when the checkpoint or the restore actually runs.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6, Spring Boot 3.2
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.
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 "Your services must support CRaC checkpoint and restore or fast scale-to-zero. How does...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/ce4ab2280a/