Shutdown Spring Boot Applications Gracefully

What is a Graceful Shutdown?

The Graceful and the Hard (or Abrupt) shutdown are the two methods of stopping an application. When an application is running, it performs certain tasks or processes client requests. While doing so, it consumes various resources, makes connections, persists data, and/or handles transactions, etc. We may want to stop a running application, in order to take it out of the system or to release a different version of the application, as part of the deployment. However, it is important to analyze and understand the consequences of abruptly stopping an application, and such consequences are purely based on the application functionality and its role in the overall system.

In order to explain in detail, the Graceful vs Hard or abrupt stopping of an application is similar to stopping a computer. When we use the shutdown function of an operating system it prompts for any unsaved work or closing of some important applications. In contrast, when we do a Hard shutdown, we lose any unsaved files or unfinished works. Interestingly, the same logic applies to applications. Some applications are capable of resuming any unfinished tasks when they are restarted. Thus, for such applications, abrupt stops don’t cause any harm. On the other hand, for some applications, an abrupt shutdown may result in unwanted outcomes. For example, failure of very large transactions, or opened resources, etc. Thus, while writing an application we should also pay attention to its shutdown procedure.