liveBook · Manning

Lifecycle

As explained in Akka in Action, Second Edition

A lifecycle is the progression of a system component through defined operational states from startup to removal or termination.

A lifecycle describes how an operational component moves from startup through active work to stopping, replacement, restart, or removal. In actor systems, lifecycle management is a first-class capability rather than custom plumbing around ordinary objects: actors are active components that can be started, stopped, and restarted. section 5.1.1 section 5.1.2

Plain objects have only construction and eventual garbage collection by default. Replacing or rebooting one requires application-built indirection, reconstruction of dependencies, careful ordering when dependencies also fail, and multithreaded safeguards. Direct references make isolation difficult, while exception handling can become tangled with normal processing. section 5.1.1

Recovering a failed log-processing component

Consider a FileWatcher sending logs through a LogProcessor and Writer to a database. If a database connection fails in a directly connected object design, calls can unwind through the stack and lose input context; buffering, retries, and concurrent-failure control must be added manually. With actors, the supervisor can wrap the failing behavior, suspend its mailbox after a crash, then choose a recovery action. It may restart the actor, replace it with a fresh instance recreated from its Behavior, stop it, or use a different replacement that connects to another database. Other actors keep communicating through actor references rather than needing to track the old instance directly. section 5.1.1 section 5.1.2

Figure 5.6 Supervisor wrapping an actor’s behavior and deciding on a recovery action after an exception
CH05_F06_Abraham2

The supervision wrapper separates normal message processing from the recovery decision made after an exception. section 5.1.2

Place lifecycle boundaries deliberately in an actor hierarchy:

  • Keep actors most likely to crash low in the hierarchy, where more actors can handle or monitor their faults. section 5.1.2
  • Avoid failures near the ActorSystem top level, where recovery can restart all top-level actors or shut down the system. section 5.1.2
  • Treat recovery as separate from functional behavior: supervision defines fault handling independently of normal message processing. section 5.1.2

Lifecycle also applies to cluster nodes. A node follows a state machine from Joining to Removed, passing through WeaklyUp, Up, Leaving, Down, and Exiting. When nodes are shut down and new ones are created, actors can run on different nodes while the word-counting application continues counting without lost work. section 8.6

How Akka in Action, Second Edition covers it

How deep it goes
Explains object-management limitations, actor start-stop-restart behavior, mailbox suspension, recovery choices, and cluster membership states.
Examples
Uses a FileWatcher–LogProcessor–Writer database pipeline and a clustered word-counting application with nodes removed and added.
What it assumes
Introduces lifecycle through actor Behaviors and actor references while contrasting them with directly connected plain objects.
What you can do after
Operate/configure it by placing failure-prone actors low in a hierarchy and choosing restart, replacement, or stopping after crashes.
How it connects
Connects lifecycle to supervision, actor hierarchy, ActorSystem, actor references, mailboxes, and cluster membership.

Also explained in

Compare how 6 books explain lifecycle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.