liveBook · Manning

Persistence Query

As explained in Akka in Action, Second Edition

PersistenceQuery is the Akka API component that provides access to a configured ReadJournal implementation.

Persistence Query provides access to a configured ReadJournal so journal events can feed queryable views. It reads event-sourced state changes as Akka Streams, separating the write-side journal from stores optimized for reading. section 10.0 section 10.2

Select a plugin implementing the Persistence Query API, then obtain its ReadJournal through PersistenceQuery. Akka-maintained choices include Cassandra, Couchbase, and JDBC; a JDBC journal can use PostgreSQL for journal events and snapshots. section 10.2

ReadJournal queries expose infinite streams; after connecting, events may arrive at any time.

  • Use persistenceIds or currentPersistenceIds to obtain persistence IDs. section 10.2
  • Use eventsByPersistenceId or currentEventsByPersistenceId to read EventEnvelopes for one persistence ID over a sequence range. section 10.2
  • Use eventsByTag or currentEventsByTag with a tag and offset. Pass Offset.noOffset to start at the beginning; retain offsets to mark the latest sequence number read. section 10.2
  • Choose standard queries to read journal events from the beginning, or current queries to read only events appearing after execution. section 10.2

Print persistence IDs

Create a JdbcReadJournal with PersistenceQuery(system).readJournalFor using JdbcReadJournal.Identifier. Its persistenceIds Source can runForeach(println), producing IDs such as spcontainer-type-key|9 and spcontainer-type-key|11. section 10.2

A query stream can aggregate AddCargo events into totalWeight per container and write that view to another database or topic. This decouples reading from writing, supports scalable processing, and can filter hazardous cargo before enriching it through an external service. section 10.2.1

Table 10.1 Methods available to consume the journal: inputs and outputs (view table figure)

Interface methods

Input

Output

eventsByTag

currentEventsByTag

tag: String

offset: Int

List[EventEnvelope]

persistenceIds

currentPersistenceIds

List[String]

eventsByPersistenceId

currentEventsByPersistenceId

id: String

from: Int

to: Int

List[EventEnvelope]

Compare ReadJournal methods by their inputs and emitted values when choosing ID-, tag-, or persistence-ID-based consumption. section 10.2

How Akka in Action, Second Edition covers it

How deep it goes
Defines ReadJournal query types, offsets, plugins, and stream behavior without detailing plugin internals.
Examples
Uses JDBC/PostgreSQL journal data, persistence-ID printing, tagged CargoAdded events, and cargo-weight aggregation.
What it assumes
Assumes persistent entities and a configured journal containing event-sourced state changes.
What you can do after
Apply it via Akka Streams to consume journal events and populate separate query stores.
How it connects
Connects Akka Streams, event sourcing, ReadJournal, offsets, projections, ETL, and clustering.

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.