liveBook · Manning

Event sourcing

As explained in Akka in Action, Second Edition

Event sourcing is a persistence technique that stores state changes as an ordered sequence of events and reconstructs current state by replaying them.

Event sourcing stores state changes as an ordered, append-only sequence of events rather than repeatedly replacing one state value. A journal preserves the event order, and replaying those events reconstructs the current state so a persistent actor can continue after restart. section 1.5.1 section 9.2

A persistent actor follows this command-to-recovery flow:

  • Receive a command expressing an intention to change state. section 9.2
  • Validate the command by executing it against the actor’s current state. section 9.2
  • Generate and append an event when the resulting state is accepted. section 9.2
  • Replay the journal’s events after restart to restore the last known state. section 1.5.1 section 9.2

Recovering a conversation

A Conversation actor receives messages and records MessageAdded events in a journal. On startup, it retrieves those events in write order and replays them into its in-memory conversation, avoiding database operations in the actor’s application logic. section 1.5.1 section 1.5.1

Events are immutable facts: changing the event history would remove the approach’s advantage. When event fields evolve, preserve old entries and deserialize multiple versions, supply defaults for added fields, or use event adapters rather than rewriting the journal. section 9.3.5 section 9.3.5

Figure 9.6 Persistent actor recovery
CH09_F06_Abraham2

Use the recovery diagram to trace how journaled events rebuild a persistent actor after restart. section 9.2

How Akka in Action, Second Edition covers it

How deep it goes
Explains commands, validation, event append, replay, immutability, and versioned deserialization; defers event-adapter details.
Examples
Uses Conversation actors with MessageAdded events and cargo events whose schemas gain or lose fields over time.
What it assumes
Assumes actors, messages, and in-memory state; initially postpones serialization and other persistence details.
What you can do after
Operate persistent actors by validating commands, appending events, replaying journals, and accommodating event-schema versions.
How it connects
Connects persistent actors, journals, commands, event replay, serialization, schema evolution, and sharding.

Also explained in

Compare how 2 books explain event sourcing →

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.