Expand description

Akka Persistence

Akka Persistence enables stateful entities to persist their state so that it can be recovered when an entity is either restarted, such as after a process crash, by a supervisor or a manual stop-start, and so forth. The key concept behind Akka Persistence is that only the events that are persisted by the entity are stored, not the actual state of it. The events are persisted by appending to storage (nothing is ever mutated) which allows for very high transaction rates and efficient replication. A stateful entity is recovered by replaying the stored events to it, allowing it to rebuild its state.

An EntityManager is used to manage entities of a specific type. It is effectively an in-memory map of entity identifiers to their instances. When used at the edge, this map tends to contain all entity instances and so it is reasonble to posit why not just use a map? The answer to this is that the EntityManager will reduce the amount code required to manage that map, and facilitate dispatching commands along with sourcing their initial state from having been persisted. Events produced by entities can also be consumed as a stream and then persisted.

Modules

  • Effects express how the state of an entity is to be updated, and how various side-effects can be performed; all in response to an entity’s commands. Effects can be chained with other effects and are guaranteed to be applied (run) before the next command for an entity id is processed.
  • An event sourced entity (also known as a persistent entity) receives a (non-persistent) command which is first validated if it can be applied to the current state. Here validation can mean anything, from simple inspection of a command message’s fields up to a conversation with several external services, for example. If validation succeeds, events are generated from the command, representing the effect of the command. These events are then persisted and, after successful persistence, used to change the entity’s state. When the event sourced actor needs to be recovered, only the persisted events are replayed of which we know that they can be successfully applied. In other words, events cannot fail when being replayed to a persistent entity, in contrast to commands. Event sourced entities may also process commands that do not change application state such as query commands for example.
  • An entity manager task handles the lifecycle and routing of messages for an entity type. One EntityManager per entity type. The EntityManager will generally instantiate the entities on demand, i.e. when first message is sent to a specific entity. It will passivate least used entities to have a bounded number of entities in memory. The entities will recover their state from a stream of events.

Structs

Enums

Constants

  • A slice is deterministically defined based on the persistence id. NUMBER_OF_SLICES is not configurable because changing the value would result in different slice for a persistence id than what was used before, which would result in invalid events_by_slices call on a source provider.

Traits

  • Implemented by structures that can return an offset.
  • Implemented by structures that can return a persistence id.
  • Implemented by structures that can return a sequence number.
  • Implemented by structures that can return a source.
  • Implemented by structures that can return tags.
  • Implemented by structures that can return a timestamp.

Functions

  • Split the total number of slices into ranges by the given number_of_ranges. For example, NUMBER_OF_SLICES is 1024 and given 4 number_of_ranges this method will return ranges (0 to 255), (256 to 511), (512 to 767) and (768 to 1023).

Type Aliases

  • Uniquely identifies an entity, or entity instance.
  • Uniquely identifies the type of an Entity.
  • Tags annotate an entity’s events