liveBook · Manning

Persistent actor

As explained in Akka in Action, Second Edition

A persistent actor is an actor whose state is durably recorded so it can be recovered after failures and restarts.

A persistent actor durably records state changes as events instead of replacing one mutable state value. Its journal grows by appending events, allowing the actor to recover its latest state after a restart by replaying that history. section 9.2

A persistent actor processes state changes through four cooperating parts:

  • A unique persistenceId identifies the actor’s durable history. section 9.2.1
  • The command handler validates a command against current state and produces an event effect. section 9.2 section 9.2.1
  • The event handler applies each event to produce the next state. section 9.2.1
  • Recovery reads the journal and replays events until the last known state is reconstructed. section 9.2

Cargo container

A container actor can define AddCargo and GetCargos commands, a CargoAdded event, and State(cargos: List[Cargo] = Nil). AddCargo persists CargoAdded; the event handler adds the cargo to state, while GetCargos reads the recovered list. section 9.2.1

Figure 9.6 Persistent actor recovery
CH09_F06_Abraham2

Recovery flow diagram showing how a persistent actor reconstructs state from its journal. section 9.2

Clustered deployments combine persistence with Akka Sharding. An EntityTypeKey is incorporated into the persistence ID so entities with the same local identifier remain distinguishable across entity types and cluster nodes. section 9.2.2

How Akka in Action, Second Edition covers it

How deep it goes
Descends from the command-to-event flow through handlers, journal replay, and clustered identity, without detailing storage configuration.
Examples
Uses a cargo container with AddCargo, GetCargos, CargoAdded, and a list-based State.
What you can do after
Build it from scratch with EventSourcedBehavior, then deploy uniquely identified persistent entities through Akka Sharding.
How it connects
Ties event sourcing, commands, events, state, journals, Akka Persistence, and Akka Sharding to clustered recovery.

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.