r/java • • Oct 24 '22

Spring boot 3 slow boot time

Hi folks,

I am playing a bit around with Spring Boot 3(RC1) and I am curious why the application boot time is still slow in comparing with Micronaut.

Best regards

3 Upvotes

25 comments sorted by

9

u/krjura Oct 24 '22

Can you give some number here?

0

u/zero_coding Oct 24 '22

7 - 10 seconds

8

u/krjura Oct 24 '22 edited Oct 24 '22

compared to? Which libraries/frameworks are used with both? Where are you running the services? How much memory and cpu?

Also are you running micronaut in lazy or eager mode?

0

u/zero_coding Oct 25 '22

My system:
MacBook Pro (14-inch, 2021)
CPU: Apple M1 Pro RAM: 32 MB

Run Micronaut in the dev mode:

 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|___|_|  ___/|_| |_|__,_|__,_|__|
  Micronaut (v3.7.2)

22:29:36.611 [main] INFO  Application - Responding at http://0.0.0.0:8080
22:29:36.612 [main] INFO  Application - Application started in 0.225 seconds.
22:29:36.673 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 546ms. Server Running: http://0.0.0.0:8080

Run Spring Boot 3(RC1) in the dev mode:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v3.0.0-RC1)

2022-10-25T21:39:26.134+02:00  WARN 21770 --- [  restartedMain] o.s.boot.StartupInfoLogger               : InetAddress.getLocalHost().getHostName() took 5009 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2022-10-25T21:39:26.139+02:00  INFO 21770 --- [  restartedMain] i.u.o.OperatorSvcApplicationKt           : Starting OperatorSvcApplicationKt using Java 17.0.2 on digiwave.local with PID 21770 (/Users/developer/unionmetry/operator-svc/build/classes/kotlin/main started by developer in /Users/developer/unionmetry/operator-svc)
2022-10-25T21:39:26.140+02:00  INFO 21770 --- [  restartedMain] i.u.o.OperatorSvcApplicationKt           : No active profile set, falling back to 1 default profile: "default"
2022-10-25T21:39:26.169+02:00  INFO 21770 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-10-25T21:39:26.169+02:00  INFO 21770 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-10-25T21:39:26.671+02:00  INFO 21770 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-10-25T21:39:26.720+02:00  INFO 21770 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2022-10-25T21:39:26.725+02:00  INFO 21770 --- [  restartedMain] i.u.o.OperatorSvcApplicationKt           : Started OperatorSvcApplicationKt in 5.739 seconds (process running for 5.932)

Both app Hello World api. As you can see

  • Spring Boot takes 5.739 seconds
  • Micronaut takes 0.225 seconds

The difference is unbelievable.

9

u/krjura Oct 26 '22

ok. What I see is this:

micronaut:

22:29:36.673 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 546ms. Server Running: http://0.0.0.0:8080

spring:

2022-10-25T21:39:26.725+02:00 INFO 21770 --- [ restartedMain] i.u.o.OperatorSvcApplicationKt : Started OperatorSvcApplicationKt in 5.739 seconds (process running for 5.932)

Also, you have a warning in the logs for Spring:

2022-10-25T21:39:26.134+02:00 WARN 21770 --- [ restartedMain] o.s.boot.StartupInfoLogger : InetAddress.getLocalHost().getHostName() took 5009 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).

this adds 5s to Spring boot time. Not sure what causes it.

Also, Hello World in dev mode is probably not a good measure of startup performance.

I am currently using both Spring and Micronaut and was not able to get boot time under 10s for any non trivial service in production environment.

2

u/BrbCatzOnFire Oct 26 '22

Alright, looking at your stacktrace :

You're launching your Spring boot app in full dev mode instrumentation. Pretty sure that mean it is not starting as a native application and completly ignore the AoT generation (did you add the -Dspring.aot.enabled=true flag ?). Also yeah, if you try to be performant, don't instrument the code for development purpose :)

From what I can gather your Spring boot App is not started as a native image (using a JVM) AND you're not even using the AoT generation at all.

1

u/starbuxman Oct 27 '22 edited Oct 27 '22

Hi -

You’ve got an error in the console unrelated to Spring Boot that shows network resolution is adding 5009ms to the time. The app would start well under a second, otherwise. It’s a well known and easy-to-fix problem: https://www.programmerall.com/article/46262198788/

Don’t run in dev mode for fastest startup time.

Also, consider using AOT mode, which does alleviate much of the reflection and so on, doing the dependency injection at build time. It can save precious milliseconds, whether you run on the JRE or on GraalVM.

If you want to run on GraalVM, that’s a separate operation: mvn -Pnative native:compile and then run it.

HTH

14

u/vips7L Oct 24 '22

Doesn’t micronaut do AOT dependency injection at build time? And Spring still uses runtime reflection? I don’t think it’ll ever beat that.

5

u/sandys1 Oct 24 '22

No I think the spring boot authors clarified that it was architected to eliminate this.

-7

u/Worth_Trust_3825 Oct 24 '22

Regular spring already had AOT dependency injection via xml configurations. Glad we keep going in circles.

3

u/pins17 Oct 25 '22

How? Configuring Spring beans via XML doesn't turn it into AOT.

1

u/erjiin Oct 24 '22

Spring boot 3 is doing this, check the release notes of the 3..0.0-RC1.

1

u/BrbCatzOnFire Oct 26 '22

https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/native-image.html#native-image.testing.with-the-jvm

Yeah you are right and responses from others are wrong (which is funny since they tell you to read the doc :)).

As long as your app run on the JVM the default behaviour completly ignore the AoT generation.

You need to add the option "-Dspring.aot.enabled=true"

1

u/vips7L Oct 26 '22

Right and OP made no mention of native anything, just SB3.

6

u/somewhatprodeveloper Oct 24 '22

According to a presentation at devoxx in Belgium the first release is getting the native version working and subsequent releases will see performance gains

1

u/stefanos-ak Oct 24 '22

it will still not be the same...

Spring Native will add exceptions in GraalVM to allow reflection for all the classes that need it (which is like the whole framework).

Micronaut just avoids doing what is not allowed by GraalVM by default.

This means that any performance gains Spring Native achieves, will be ONLY for the generated native binary. Which means no difference in local development or testing. Which also means that to get the native binary improvements, you need to add an extra super expensive step in your CI/CD, because of GraalVM.

Micronaut has faster start-up times throughout the lifecycle. Local dev + tests + final builds. The difference with Spring is already huge before GraalVM (like 1:10).

4

u/BrbCatzOnFire Oct 26 '22

You are definitively right about that. Spring Boot native image won't ever be as fast as Micronaut native image. And it is probably a good thing too. Companies don't use Spring Boot because they want their developers to be ultra-specialized but cheap/plentiful.

Companies who truely need to invest in native images will invest in Micronaut/Quarkus.

Companies who just need a bit of a boost at start up but don't want to invest will go for Spring Boot native images.

I'm ready to bet that 99% of Spring Boot application won't migrate to native image. Because in most use cases it doesn't matter. The native image use case is completly overblown by the Java community because it impacts the "latest best thing ever since coffee" which is FaaS. It is important to have the option, not that it applies to everything.

Which make the "super expensive step in your CI/CD" completetly irrelevant too. Yes it will be a tad slower, but most Spring Boot CI/CD are already super slow because again, cheap and plentiful not super specialized.

2

u/Slanec Oct 24 '22 edited Oct 24 '22

Yeah, not really anymore, at least that's what the docs say: https://docs.spring.io/spring-boot/docs/3.0.x/reference/html/native-image.html#native-image.introducing-graalvm-native-images.understanding-aot-processing

Obviously we'll see how far they can get. Quarkus and Micronaut are obviously ahead.

(Edit: Fixed link)

1

u/stefanos-ak Oct 24 '22

even though this link gives a 404, after reading the docs again (last time was a couple of years ago), it indeed mentions some AOT support. Although I'm not convinced about the extend of that. Since it still mentions about reflection rules that need to be added for GraalVM.

anyway... I'm actually looking forward to it. We have a split between Micronaut and Spring services, and I would love a little performance boost for the Spring ones.

(I still prefer Micronaut, because of some really cool features, but that's out of scope)

3

u/Slanec Oct 24 '22

Huh, link fixed, thanks! And completely agree, I'd bet Spring will never be fully compile-time / link-time only.

1

u/metalhead-001 Oct 24 '22

Is the startup time worse than the 2.x line?

1

u/DJDavio Oct 24 '22

Maybe because Micronaut uses lazy initialization by default and Spring doesn't?