Enable Spring Boot ApplicationStartup Metrics to Diagnose Slow Startup

Overview

During an application startup process, Spring Boot performs a lot of work in the background. This work involves creating Spring Application Context, creating various beans, auto-wiring, and auto-configuration of various components, and finally, starting the application. When a Spring Boot Application has a slow startup, it can be one or more beans and related dependencies taking longer to initialise and slowing down the entire process.

Profiling Spring Boot application doesn’t often help in diagnosing the startup issues. This is because there are a number of beans getting initialised and it is really difficult to figure out which ones are causing the latency. Spring Boot Application Startup Metrics are useful for such cases.

A Simple State Machine for Spring Boot Projects

There are many articles and open-source projects on state machines (Ref#1) that can be searched on Google or GitHub. The Spring team itself provides a state machine framework (Ref#2). However, I found these frameworks not easy to customize. Furthermore, it was not easy to add logs and throw custom exceptions where I needed. So I created a simple implementation of the state machine that can be easily integrated into Spring Boot applications. 

The idea of a state machine is to define a set of state transitions where each transition is affected by an event. For instance, Wikipedia has an example for a turnstile that has the Locked and Unlocked states, which are affected by the events "coin" and "push". The turnstile's state transitions from Locked to Unlocked when the "coin" event happens. It transitions from Unlocked to Locked when a "push" event happens. The state machine enforces that when the turnstile is in the Locked state the "push" event has no effect. Similarly, when the turnstile is in the Unlocked state the "coin" event has no effect.