Search code examples
akkasystem.reactiveakka-streamevent-sourcingakka-persistence

What is a typical use-case for PersistentView in Akka?


Here http://doc.akka.io/docs/akka/current/scala/persistence.html is written:

PersistentView: A view is a persistent, stateful actor that receives journaled messages that have been written by another persistent actor. A view itself does not journal new messages, instead, it updates internal state only from a persistent actor's replicated message stream.

What is a typical use-case for PersistentView ?

How is it different from Akka Streams / RX?

It looks pretty similar to me.


Solution

  • You might find it useful if you're designing your application based on CQRS. CQRS fits well with event-sourced systems as can be seen in the blog post.

    While you might use PersistentActor actors to update the state of your application - this is the command side / model, PersistentView actors come in handy when you want to display the data, so on the query side.

    An example would be storing data about bank accounts. In a traditional model you would probably have all CRUD operations under some repository. A really basic transformation in this model would be to use the PersistentActor for the CUD bit and PersistentView for the R bit. The nice thing is that you decouple the command part which updates the state from the query side.

    As for the last question, akka-persistence and akka-streams are made for different purposes. As a matter of fact akka-persistence-query-experimental actually is built using akka-streams to provide different sources. An example is actually here. Note that PersistentView is deprecated in the latest version and it's recommended to use PersistenceQuery.