Quick revision
The short answer
Lazy initialization did not fix the misconfigured bean; it moved its failure from startup, where a deploy fails safely, to runtime, where a user hits it. I would keep eager initialization in production and use lazy initialization mainly for development and tests, then fix the real 40 second startup cost by profiling with the startup endpoint or JFR. If the team keeps lazy initialization anyway, they need smoke tests after each deploy that touch every feature, plus a test that starts the full context eagerly.
What to remember
In this answer, we saw that lazy initialization moved the misconfigured bean's failure from startup, where a deploy fails safely, to runtime, where a user found it a week later. The recommendation is eager initialization in production and lazy initialization mainly for development and tests, fixing the real startup cost by profiling with the startup endpoint or JFR, and, if lazy initialization must stay, adding post-deploy smoke tests that touch each feature and an eager context test in CI.
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.
Lazy initialization did not fix the misconfigured bean; it moved its failure from startup, where a deploy fails safely, to runtime, where a user hits it. I would keep eager initialization in production and use lazy initialization mainly for development and tests, then fix the real 40 second startup cost by profiling with the startup endpoint or JFR. If the team keeps lazy initialization anyway, they need smoke tests after each deploy that touch every feature, plus a test that starts the full context eagerly.
Why this question is asked
This scenario is the classic lazy initialization story. The team got a great number, 40 seconds down to 15, and a week later paid for it with an error in production that the old setup would have caught before the deploy. Interviewers ask it to see whether we can name the trade that was made and propose a better way to get the speed. In this answer, we will reconstruct what happened, explain why the error moved, recommend where lazy initialization belongs, show how to find the real startup cost, and cover the safeguards if the team keeps it.
1What Actually Happened
With eager initialization, the default, every singleton bean is created during startup. The misconfigured bean, say a client for a tax service with a malformed URL property, would have thrown in its constructor, startup would have failed, and the deploy would have been rejected while the old version kept serving.
With spring.main.lazy-initialization=true, that bean is only created when first needed. The application started, passed its readiness probe and took traffic. The bean was not needed until a user opened the tax feature a week later, and that user got the error.
8 messages in order. Play them, or step through them one at a time.
Same bug, same code; only the moment of failure changed.
2Why Moving the Error Is a Bad Trade
A startup failure is cheap. It happens during a deploy, in front of the person deploying, the rollout stops, and no user notices. A runtime failure is expensive. It happens at a random time, to a real user, far from the change that caused it, and someone has to connect a week-old configuration change with today's error.
So the 25 seconds saved per startup were paid for with a loss of the startup check that protects every deploy. Lazy initialization also slows the first request to each feature and hides memory growth until later, but the moved failure is the part that hurts most in this scenario.
3Recommendation: Eager in Production, Lazy Where It Helps
I would switch production back to eager initialization. Lazy initialization is a good tool where fast restarts matter more than early failure: a developer's machine, where restarts happen thirty times a day, and some test suites. A profile makes that explicit.
Because the misconfigured bean would then fail during the deploy, we should also make the startup failure clear: a validated @ConfigurationProperties class for the URL would stop startup with a message naming the property, which is better than a constructor exception.
4Finding the Real Startup Cost
The 40 seconds still need fixing, but with evidence. Spring records each startup step when the application uses a BufferingApplicationStartup, and the startup actuator endpoint returns the steps with durations. Sorting the bean instantiation steps shows where the time goes.
- Switch production back to eagerlazy only in dev profile
- Record startup stepsstartup endpoint or JFR
- Fix the slowest beansschema validation, migrations, remote calls
- Measure againtarget the same 15 s
- Keep lazy in production?YesAdd post-deploy smoke tests and an eager context testNoDone
5 steps in order. Play them, or step through them one at a time.
With a breakdown like this, the fixes are targeted. Hibernate's schema validation can be turned off in production, migrations can move to a separate job before the rollout, and the pricing client can stop calling a remote service in its constructor. Alternatively, FlightRecorderApplicationStartup records the same steps into JFR for a recording taken in production. Fixes like these often reach the same 15 seconds while keeping the startup check.
5If Lazy Initialization Stays
Sometimes a team keeps lazy initialization in production, for example for a service that scales out often and needs fast starts. Then the startup check has to be replaced by other checks. First, a test in CI that starts the full context eagerly with production-like configuration, so every bean is created at least once before release. Second, smoke tests after each deploy that touch every feature, so each lazy bean is created right away, while the deploy can still be rolled back, instead of by the first user.
A cheap complement is to make critical beans eager with @Lazy(false), for example clients that depend on environment-specific configuration. That brings back the startup check exactly where misconfiguration is most likely.
6Follow-up questions
Could the team have caught this error without eager initialization?
Yes, with a test in CI that starts the full context eagerly using production-like configuration. Validated configuration properties alone would not be enough under lazy initialization, because the properties bean is itself created lazily, so its validation also waits for first use. Marking such beans @Lazy(false) brings the check back to startup.
Is 40 seconds actually a problem?
It depends on how often the service starts. For a service deployed twice a week with steady traffic, it is a mild annoyance. For one that scales out during spikes, it delays new capacity, which is a real problem worth fixing with profiling.
Do smoke tests after deploy make lazy initialization safe?
Safer, not safe. They only touch what they cover, so a rarely used path may still be missed. Combined with an eager full-context test in CI, they close most of the gap.
7Conclusion
In this answer, we saw that lazy initialization moved the misconfigured bean's failure from startup, where a deploy fails safely, to runtime, where a user found it a week later. The recommendation is eager initialization in production and lazy initialization mainly for development and tests, fixing the real startup cost by profiling with the startup endpoint or JFR, and, if lazy initialization must stay, adding post-deploy smoke tests that touch each feature and an eager context test in CI.
Open with
I would start by naming what happened: lazy initialization did not fix anything, it moved the failure. With eager init, that misconfigured bean would have thrown during startup, the deploy would have failed and the old version would have kept serving.
Then, if they want more
With lazy init the app started in fifteen seconds, passed readiness, and the bean was only created when a user first used the feature a week later. That is a bad trade, because a failed deploy is cheap and a user-facing error is expensive.
So I would recommend eager initialization in production and keep lazy init for development, in a dev profile, and maybe some tests. Then I would fix the forty seconds properly. I would set a BufferingApplicationStartup, read the startup endpoint or a JFR recording, and sort bean instantiation by duration.
Usually it is a few things, like Hibernate schema validation, Flyway migrations, or clients that make remote calls at init, and fixing those can often get close to the same fifteen seconds.
If the team still wants lazy init in production, I would insist on a CI test that starts the full context eagerly with production-like config, and smoke tests after each deploy that touch every feature.
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
- Spring Boot reference
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 team enables lazy initialization in production to cut startup from 40 to 15 seconds. A...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/960932df22/