TL;DR

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.

The Problem

Spring Boot has ALWAYS started slow for me. Almost embarrassingly slow. How slow? Start with an empty Spring Boot Web application with Spring Initializer.

Spring Initializer

Now launch the server using ./gradlew bootRun and take a look at that launch time.

Slow bootRun

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.

Antonio Troina released an awesome post and code sample that highlights the issue. Running inetTester produced this output:

Slow inetTester

The InetAddress.getLocalHost().getHostName() call took over 5 seconds.

The Solution

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:

Slow etc hosts

Here is what my /etc/hosts looks like now:

Fast etc hosts

Now with this change (and no reboot or no terminal restarts), inetTester outputs this:

Fast inetTester

9 ms! Down from 5011 ms.

What effect does this have on Spring Boot’s startup time? Take a look for yourself.

Fast bootRun

2.264 seconds. Down from 12.184 seconds.