Table of contents
1.
Introduction📑
2.
Shutdown Sequence📚
3.
Shutdown Triggers🚀
3.1.
Limitations🔖
4.
Gracefully Shutdown the Server🧑‍💻
5.
Coordinated Shutdown 🆚 Application Life Cycle 
6.
Frequently Asked Questions
6.1.
Describe the Akka cluster.
6.2.
How does one shut down an actor system?
6.3.
What are the actors in Akka?
6.4.
What is a JVM application?
6.5.
Which statement terminates the JVM?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Coordinated Shutdown

Author Ishita mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📑

The Coordinated Shutdown ActorSystem module allows particular Akka.NET actors and services to be suspended while tasks are completed during shutdown in a predetermined order. While ApplicationLifecycle API still exists and Play is in charge of exiting the JVM, Coordinated Termination is in charge of Play's entire shutdown.

Coordinated Shutdown

Keep reading the article to understand the process of Coordinated Shutdown, the Shutdown Triggers, and its limitations, followed by essential differences between Coordinated Shutdown and the Application Life Cycle.

Shutdown Sequence📚

When a Coordinated Shutdown is launched, its default phases are arranged in a directed acyclic graph (DAG). Current phases can rely on your new phases by creating new ones and overwriting the default settings. The most important phases included in Akka and used by Play are listed below:

before-service-unbind
 service-unbind
 service-requests-done
 service-stop
 // Only a few cluster-related phases are intended for internal use.
 before-actor-system-terminate
 actor-system-terminate

 

The essential phases are listed above in the order they will typically run. To alter this behavior, follow the Akka documentation.

NOTE📝

✍️In the service-stop phase, the ApplicationLifecycle#stopHooks that you do not migrate to Coordinated Shutdown tasks will continue to run in reverse order of creation inside CoordinatedShutdown. To put it differently, Coordinated Shutdown treats each ApplicationLifecycle#stopHook as a separate task.

✍️You have the choice to execute a shutdown task at a different phase due to a Coordinated Shutdown.

✍️ApplicationLifecycle#stopHooks should work well in your existing code; however, you might want to consider changing how and when it is called. For instance, the actor needs a database connection if it performs database operations regularly.

Your database connection pool may be stopped in an ApplicationLifecycle#stopHook that occurs in the service-stop phase, depending on how the two were built. At the same time, your actor may now be closed on the actor-system-terminate phase that occurs later.

If running your cleanup code at the service-stop phase is consistent with your usage, keep using ApplicationLifecycle#stopHooks.

You must inject a CoordinatedShutdown instance and use addTask as in the example below to choose to employ Coordinated Shutdown tasks:

SCALA

class ResourceAllocatingScalaClass @Inject() (cs: CoordinatedShutdown) {
  // Here, some resource distribution takes place: a relationship
  // pool is made, and a client library is launched, ...
  val resources = Resources.allocate()

  // Immediately register a shutdown job.
  cs.addTask(CoordinatedShutdown.PhaseServiceUnbind, "free-some-resource") { () =>
    resources.release()
  }
  // ... additional code
}

 

JAVA

public class ResourceAllocatingJavaClass {
  private final Resources resources;
  @Inject
  public ResourceAllocatingJavaClass(CoordinatedShutdown cs) {
    // Here, some resource distribution takes place: a relationship
    /// pool is made, and a client library is launched, ...
    resources = Resources.allocate();

    // Immediately register a shutdown job.
    cs.addTask(
        CoordinatedShutdown.PhaseServiceUnbind(), "free-some-resource", () -> resources.release());
  }
  // ...  additional code
}
You can also try this code with Online Java Compiler
Run Code

Shutdown Triggers🚀

Shutdown Triggers

SIGTERM signal is typically used to end a Play process. A JVM shutdown hook is activated when the Play process receives the signal, which causes the server to shut down by using Coordinated Shutdown.

Other potential triggers vary slightly from SIGTERM. SIGTERM is handled from the outside; however, you can start a shutdown within your code (or a library may detect a cause to trigger the shutdown). For instance, you are adding an API endpoint that would enable an administrator or orchestrator to initiate a programmatic shutdown when running your Play process as a component of an Akka Cluster. 

⭐In some cases, the shutdown is carried out from the inside out: the Actor System will shut down before the JVM shutdown hook runs, despite the Coordinated Shutdown list's phases running in the correct order. You should consider all of the termination triggers and the actions and sequencing of those actions while creating your Play application.

Limitations🔖

🎯The default settings for Akka Coordinated Shutdown make it fairly programmable. Still, some of these parameters become invalid when Akka Coordinated Shutdown is used throughout the Play lifecycle. 

🎯A configuration like this is called akka.coordinated-shutdown.exit-jvm. In a Play project, enabling akka.coordinated-shutdown.exit-jvm will result in a shutdown impasse that will prevent your process from ever finishing. 

🎯In both Production, Development, and Test modes, the default settings for Akka Coordinated Shutdown should work correctly in the Production, Development, and Test modes.

Gracefully Shutdown the Server🧑‍💻

When using the Akka HTTP server backend, the server shutdown occurs smoothly and goes according to the documentation for Akka HTTP. To sum it up:

First, no new connections will be accepted because the server port is unbound.

A request is assigned a deadline to finish if it is "in-flight" (being handled by user code). With play.server.akka.terminationTimeout, the deadline for Akka HTTP can be set. 

A connection is promptly ended if there are no "in-flight" requests.

If user code generates a response before the timeout expires, the connection is closed, and the response is transmitted to the client with a Connection: close header.

If the response is a streaming one, it must also finish by the deadline; if it doesn't, the connection will be cut off, regardless of how the streaming response is progressing.

An automatic response is issued with a status set by akka.http.server.termination-deadline-exceeded-response if user code does not respond within the deadline. A genuine HTTP status code must be used as the value.

Coordinated Shutdown 🆚 Application Life Cycle 

Coordinated Shutdown  vs  Application Life Cycle

Coordinated Shutdown 

Application Life Cycle 

There are numerous phases in Coordinated Shutdown where you can register tasks. ApplicationLifecycle provides only one method for registering stop hooks.
The Coordinated Shutdown method organizes each phase into a DAG, where all tasks are carried out concurrently. The classic ApplicationLifecycle stop hooks were a series of actions whose execution order was determined by Play.
If you require precise control over the shutdown order of seemingly unconnected components of the program, you could utilize Coordinated Shutdown. ApplicationLifecycle cannot be utilized to regulate the program's shutdown sequence of ostensibly unrelated components.

Play's DI exposes a CoordinatedShutdown instance. Wherever you initially asked for an instance of ApplicationLifecycle to be injected, you may now ask for an instance of CoordinatedShutdown if you want to switch from ApplicationLifecycle to it.

An ActorSystem instance is linked to a CoordinatedShutdown instance. Both will be halted when one of the Server or Application's ActorSystems is stopped in contexts where both are shared.

Frequently Asked Questions

Describe the Akka cluster.

A fault-tolerant decentralized peer-to-peer Cluster Membership Service without a single point of failure or bottleneck is offered by Akka Cluster.

How does one shut down an actor system?

ActorSystem should be used to shut off the system. When an actor is finished, end it by calling context.system.terminate() from within the actor.

What are the actors in Akka?

Actors are objects that receive messages and respond to them by taking appropriate action.

What is a JVM application?

The JVM provides a portable execution environment for Java-based applications while managing system memory.

Which statement terminates the JVM?

The JVM is shut down (by sending SIGTERM) Runtime. exit() or System. exit() is called by one of the threads.

Conclusion

This blog has extensively covered the topic of Coordinated Shutdown used to shut down Play completely. We also discussed the shutdown sequence, triggers, and its limitations and finally concluded with the differences between the Coordinated Shutdown and Application Life Cycle.

Be Curious Image

Visit our Guided Path in  Coding Ninjas Studio to learn about its related topics. If you are preparing for an interview, visit our Interview Experience Section and interview bundle for placement preparations. Upskill yourself in PythonKivyBackend Web TechnologiesSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design, and much more!

You may consider our paid courses to give your career an edge over others!

Do upvote our blogs to help other ninjas grow!

Happy Learning, ninjas!!

Thankyou Image
Live masterclass