liveBook · Manning

PersistenceId

As explained in Akka in Action, Second Edition

A persistence ID is a stable unique identifier for a persistent entity's durable state and event stream.

A PersistenceId is the unique identity of an event-sourced entity’s stored history. EventSourcedBehavior receives it alongside initial state, command handling, and event handling, so events can be associated with one persistent actor. section 9.2.1

Uniqueness matters especially in a cluster: the same actor ID can otherwise exist on different nodes, and different entity types can reuse the same ID. Combine an EntityTypeKey with the entity ID to distinguish the persistent entity. section 9.2.1 section 9.2.2

A sharded container identity

A container entity can create its behavior with PersistenceId(typeKey.name, containerId). This pairs the container type with its ID before Cluster Sharding initializes and routes the entity. section 9.2.2

Table 9.1 Journal entries in the database table public.journal_events (view table figure)

ordering

persistence_id

sequence _number

deleted

event_ser_id

event_payload

event_ser_id

1

SPContainer -type-key|9

1

false

33

binary data

33

2

SPContainer -type-key|9

2

false

33

binary data

33

3

SPContainer -type-key|11

3

false

33

binary data

33

Journal rows show the resulting persistence_id: containers 9 and 11 have distinct type-key-and-ID values, while sequence numbers track events for each entity. section 9.4

When choosing an identity, keep these operational rules in view:

  • Make the persistence ID unique; persistence alone does not check whether a single-node actor ID collides elsewhere in a cluster. section 9.2.1
  • Use the entity type key plus entity ID for sharded persistent entities. section 9.2.2
  • Expect journal records to store the persistence ID with each event, alongside an entity-specific ascending sequence number. section 9.4

How Akka in Action, Second Edition covers it

How deep it goes
Explains constructor placement, cluster collision cases, type-key composition, and resulting journal rows.
Examples
Uses sharded cargo containers, container IDs 9 and 11, and journal rows containing type-key-and-ID values.
What it assumes
Builds from EventSourcedBehavior’s Command, Event, and State roles while introducing the PersistenceId parameter.
What you can do after
Apply it via Akka Persistence Typed to assign collision-resistant identities to sharded event-sourced entities.
How it connects
Connects PersistenceId to EventSourcedBehavior, Akka Cluster Sharding, EntityTypeKey, event sourcing, and journal sequence numbers.

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.