Quick revision
The short answer
I would measure before changing anything: record the startup steps with BufferingApplicationStartup and read them from /actuator/startup, or use JFR's startup events. The usual culprits are slow bean initialization, network calls made during startup and a large Hibernate metamodel, plus auto-configuration the service does not need. Then I would fix those directly, consider lazy initialization with care, use class data sharing or the JDK's AOT cache, or a native image where it fits, and measure each change, because startup time varies a lot with CPU limits.
What to remember
In this answer, we found what makes a 45-second startup slow by recording steps with BufferingApplicationStartup and /actuator/startup, or with JFR. We fixed the usual causes: heavy work in bean initialization, network calls during startup, a large Hibernate metamodel with schema validation, and unneeded auto-configuration. We used lazy initialization selectively, considered CDS, the JDK AOT cache and native images, and measured every change under the production CPU limit, which often explains much of the slowness on its own.
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.
I would measure before changing anything: record the startup steps with BufferingApplicationStartup and read them from /actuator/startup, or use JFR's startup events. The usual culprits are slow bean initialization, network calls made during startup and a large Hibernate metamodel, plus auto-configuration the service does not need. Then I would fix those directly, consider lazy initialization with care, use class data sharing or the JDK's AOT cache, or a native image where it fits, and measure each change, because startup time varies a lot with CPU limits.
Why this question is asked
Forty-five seconds of startup sounds like a nuisance, but it has real costs: deployments of 30 pods take much longer, autoscaling reacts too late to a traffic spike, and a crash loop takes minutes to recover. Interviewers ask this debugging question to see whether we measure first or jump to fashionable fixes. In this answer, we will recap what happens during Spring Boot startup, record where the time goes, look at the usual causes and their fixes, and finish with the JVM-level options and how CPU limits affect all of them.
1A Quick Recap: What Happens During Startup
When a Spring Boot application starts, the JVM loads and verifies thousands of classes, mostly in the interpreter because the JIT compiler has not warmed up yet. Spring then scans for components, evaluates the conditions on every auto-configuration class to decide which ones apply, creates and wires every bean, runs initialization methods, and finally starts the web server.
Any of these steps can be slow, and the cause is often one bean that does something expensive in its constructor or @PostConstruct method. So the first job is to find out which steps take the time.
2Recording Startup Steps
Spring Framework records startup steps through the ApplicationStartup interface. Spring Boot provides BufferingApplicationStartup, which keeps those steps in memory so the Actuator startup endpoint can show them, with the duration of each bean's creation.
Sorting the steps by duration usually shows two or three beans that explain most of the time. Notice that a bean's duration includes the beans it depends on, so we look for the deepest slow step, not just the top. Alternatively, FlightRecorderApplicationStartup sends the same steps to Java Flight Recorder, where we see them next to class loading, GC and CPU activity, which helps when the time is not inside any bean.
3The Usual Causes and How to Fix Them
Slow bean initialization is the first one: a bean that loads a large file, builds a big in-memory index or warms a cache in its constructor. We move that work to after startup, for example on an ApplicationReadyEvent listener or in the background, and report readiness only when the essential parts are ready.
Network calls during startup are the second. A client bean that calls a configuration service, fetches credentials, checks a Kafka topic or waits on a remote host that is slow to answer can add seconds or, if a timeout is missing, much longer. In the recording above, pricingClient takes 12 seconds because it fetches a token from an identity service with a 10-second retry. Short timeouts and lazy fetching fix it.
A large Hibernate metamodel is the third. With hundreds of entities, Hibernate builds its metadata, and with schema validation turned on (ddl-auto=validate) it also reads the database schema. Turning schema validation off in production, after checking it in tests, and making sure no entity scanning covers unnecessary packages helps.
Unneeded auto-configuration is the fourth. Starters pull in auto-configurations for things the service does not use. Running with --debug prints the condition evaluation report, which shows which auto-configurations matched, and we exclude the ones we do not need with spring.autoconfigure.exclude or the exclude attribute of @SpringBootApplication.
- Record steps
BufferingApplicationStartupor JFR - One or two beans dominate?YesInspect those beansNoLook at class loading and CPU in JFR
- Network call inside the bean?YesAdd timeouts, move after startupNoCheck heavy work or Hibernate
- Remove unused auto-configurationcondition report with --debug
- Measure again with the same CPU limit
5 steps in order. Play them, or step through them one at a time.
4Lazy Initialization, With Care
Setting spring.main.lazy-initialization=true makes Spring create beans only when they are first needed. Startup gets faster, sometimes a lot, but the cost moves to the first requests, which are then slow, and configuration errors that used to stop startup now appear at runtime, possibly hours later. For a service behind autoscaling, that first slow request is exactly what we were trying to avoid.
So we use it selectively: mark specific heavy beans that are rarely used with @Lazy, rather than making everything lazy, and keep eager initialization for beans every request needs.
5JVM-Level Options: CDS, the AOT Cache and Native Images
Class data sharing, CDS, stores loaded and parsed classes in an archive file that the next JVM start maps directly, which saves class loading time. Spring Boot supports creating such an archive with a training run that starts the application and exits. Newer JDKs extend this with an ahead-of-time cache that also stores linked classes and profiling information, introduced in JDK 24 and made simpler to create in JDK 25; the flags differ by version, so check yours.
A GraalVM native image goes further: it compiles the application ahead of time into an executable that starts in well under a second. The price is a long build, restrictions on reflection and dynamic proxies that Spring handles through its AOT processing but some libraries do not, and usually lower peak throughput without profile-guided optimisation. It suits small services and functions that scale to zero more than large, long-running services.
6Measure Each Change Under the Real CPU Limit
Startup is CPU-heavy: class loading, verification and the JIT all compete for processor time. A service that starts in 12 seconds on a laptop can take 45 seconds in a pod limited to half a CPU. So we measure in the same environment and with the same CPU limit as production, change one thing at a time and record the result.
Sometimes the cheapest fix is the limit itself: giving pods more CPU during startup, for example a higher limit or a CPU request that is not throttled during the first minute, can halve startup without any code change. Some platforms offer a startup CPU boost for exactly this reason.
7Follow-up questions
Why not just make the readiness probe wait longer?
That hides the symptom without helping deployments or autoscaling. Pods still take 45 seconds to serve traffic, so scale-up still reacts late. A longer startup probe is right to avoid restart loops, but it is not a fix for slow startup.
Does lazy initialization hide configuration errors?
Yes. With eager initialization, a missing property or a broken bean stops the application at startup, before it takes traffic. With global lazy initialization, the error appears on the first request that needs the bean, which may be much later. A test that loads the full context eagerly reduces that risk.
How much does CDS usually save?
It varies with the application, the JDK and the hardware, so measure. For a typical Spring Boot service, savings of a noticeable fraction of class loading time are common, and the newer AOT cache usually saves more. It is cheap to try because it needs no code changes.
8Conclusion
In this answer, we found what makes a 45-second startup slow by recording steps with BufferingApplicationStartup and /actuator/startup, or with JFR. We fixed the usual causes: heavy work in bean initialization, network calls during startup, a large Hibernate metamodel with schema validation, and unneeded auto-configuration. We used lazy initialization selectively, considered CDS, the JDK AOT cache and native images, and measured every change under the production CPU limit, which often explains much of the slowness on its own.
Open with
First I would measure instead of guessing. I would set BufferingApplicationStartup on the SpringApplication, expose the startup actuator endpoint and sort the steps by duration, or use FlightRecorderApplicationStartup to see them in JFR next to class loading and CPU.
Then, if they want more
That usually points to two or three beans.
Typical causes are heavy work in a constructor or PostConstruct, like building an index, which I would move to after ApplicationReadyEvent; network calls at startup, like fetching a token or checking a topic, which need short timeouts or lazy fetching; and a big Hibernate metamodel, especially with ddl-auto validate reading the schema, which I would turn off in production once tests cover it.
Then I would check the condition evaluation report with debug and exclude auto-configurations we do not use. I would only use lazy initialization selectively with Lazy on rare heavy beans, because global lazy init moves the cost to the first requests and hides configuration errors.
At the JVM level, CDS or the JDK's AOT cache are cheap wins, and a native image is an option for small services. And I would measure each change with the same CPU limit as production, because a half-CPU pod can easily triple startup time, and sometimes more CPU at startup is the simplest fix.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 24, Spring Boot 3.3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "A Spring Boot application takes 45 seconds to start, which slows deployments and...": https://scrivio-interview-library.netlify.app/spring-boot/service-performance/ea7b94b01c/