Video in progress

Senior 🌱 Spring

A Spring Boot application takes 90 seconds to start. How do you find the cause and speed it up?

I don't guess, I measure: the startup actuator (BufferingApplicationStartup) or log timestamps show which bean and which step is slow. The usual culprits are network calls at startup, a heavy @PostConstruct, unneeded auto-configurations, a large classpath scan, or Hibernate/Flyway.

📖 Answer

Measure first:

SpringApplication app = new SpringApplication(App.class);
app.setApplicationStartup(new BufferingApplicationStartup(10000));
app.run(args);

Then /actuator/startup shows how long each bean and step took. There's an even simpler way: take a few thread dumps during startup, and you immediately see where the main thread is stuck.

Common causes:

  • an external API call or loading a big cache inside @PostConstruct. Move it to the background or to the first request.
  • DNS or network timeouts (Eureka, config server, Kafka admin client). When one is down, each one waits out its own full timeout.
  • Hibernate: thousands of entities, and ddl-auto=update compares the schema.
  • Flyway: checking hundreds of migrations every time, or one long migration.
  • Unneeded starters, each of which brings its own auto-configuration and beans.

To speed it up:

  • spring.main.lazy-initialization=true: only in dev or with care, because errors move from startup to the first request;
  • a CDS (Class Data Sharing) archive: made convenient in Boot 3.3+, startup gets 30-40% faster;
  • AOT + GraalVM native image: very fast startup, but the build is complicated;
  • CRaC: restore from a snapshot of a running application.

In practice startup time matters mostly for deploys and autoscaling. So first find out how much the business actually needs it, then optimize.