If you are running MacOS, add hostname (output of
hostname) to the IPv4 & IPv6 loopback entries in
/etc/hosts to cut 10 seconds from Spring Boot startup time.
Spring Boot has ALWAYS started slow for me. Almost embarrassingly slow. How slow? Start with an empty Spring Boot Web application with Spring Initializer.
Now launch the server using
./gradlew bootRun and take a look at that launch time.
12.184 seconds!! To load nothing but framework. Yuk!
It turns out as part of Spring Boot startup it makes several calls to
InetAddress.getLocalHost().getHostName(), and this is known to be slow.
InetAddress.getLocalHost().getHostName() call took over 5 seconds.
A solution exists, and that is to explicitly add your hostname into the IPv4 and IPv6 loopback interface entries in the hosts file.
Before, My standard-ish
/etc/hosts file used to look like this:
Here is what my
/etc/hosts looks like now:
Now with this change (and no reboot or no terminal restarts), inetTester outputs this:
9 ms! Down from 5011 ms.
What effect does this have on Spring Boot's startup time? Take a look for yourself.
2.264 seconds. Down from 12.184 seconds.