Query in AKKA.NET
Asked Answered
F

2

9

I'm trying to understand the Query side of CQRS in a Catalog Management application built with AKKA.NET. I have a UI which needs to show all the products in a grid that follows a criteria. The criteria can be All Products that came from Vendor A and this criteria is entered on a form on the UI.

My question is how this query is being done? Where should I go to run the query? Should I load all the product actors to memory to do the query? Or should I go to the persistent media where the state is saved? After I get all the product ids that succeeded with the query, I should use those product ids to activate all the actors?

Please advise.

Frogfish answered 11/5, 2015 at 2:29 Comment(0)
B
5

Your question doesn't sound like strictly actor related, since it's more CQRS vs. CRUD characteristic. In standard CQRS scenario you want to have some kind of persistent storage with schema adjusted to your read model. Then you can run your queries on it.

One of the things that comes to my mind using Akka.NET in CQRS-based but not yet event-sourcing scenario, is to utilize Akka event bus. When you start an application, create actors responsible for updating your read model and immediately subscribe them to Akka event bus for listening messages denoting, that read model must be updated. Then in any actor which may provoke read-side changes, publish a related event/message.

If you're using event sourcing, you would probably like to take a look at Akka.Peristence. There you have a concept of event journal created by so called persistent actors. It describes the stream of all operations made by an actor, which should have been stored. Second thing is another kind of actor called persistent view, which may be associated with particular stream of events and build read model from them. While persistent view is usually correlated with a single persistent actor, you may again use event bus + listeners to create a global read models. One of the advantages of this approach is that all events are stored in persistent storage, making your system more durable and easier to recover in case of failure.

Beabeach answered 12/5, 2015 at 9:57 Comment(6)
You answered my question about where the query should be run. As I understand now, it should be against the persistent state and not the in-memory state.Frogfish
Am I correct in my understanding that if I am using Akka.Persistence then I shouldn't need another event sourcing system like NEventStore or getEventStore? Is Akka.Persistence sufficient for implementing Event sourcing? I don't want to throw extra components at my system if they are not needed?Graeae
@Graeae I believe you are talking about Akka.NET Persistence and not AKKA (Java or Scala version). I did not see any example of Akka.NET persistence being used as event sourcing. NEventStore and getEventStore are matured products and NEventStore can use many persistent stores like MongoDB, RavenDB, Azure Blob. This may not be the case with Akka.NET persistence. I also have questions like what store it is going to support etc...Frogfish
@Graeae Akka.Persistence can be used for event sourcing, when we're talking in the scope of actors. You'll also need some integration plugin for persistence provider engine - currently I know about SQL Server, PostgreSQL, Cassandra and EventStore. If you want to have ES applied to some application components not modeled as persistent actors, then you'll need some extra dependencies.Beabeach
@Horusiath Can you point me to any good examples of using Akka.Persistence for EventSourcing?Graeae
@Graeae I've created an example, but it's not finished.Beabeach
I
4

Or you could flip your thinking on its head and create an Actor that represents a query. If you are querying the "Products" objects looking for all products from vendor A, then each Product object would send your query actor an update of its current state, if its from Vendor A your Query Actor processes the object adding it to its collection etc. Your query actor would filter out objects that don't fit with in the query parameters. Now you have an actor that contains a list of products that is getting updated in real time as new products are created, changed etc.

Intellectuality answered 21/8, 2015 at 7:15 Comment(2)
but how to get data out of this actor... in a request response scenario? rest-like-interface?Suicide
Its an actor so you have to send it a message requesting the data. It will manage its own state but you would expect the current state to be in memory at the time of request. The request will go into its message queue, so its possible that it is behind other updates to the query object, but that's the expected and generally preferred behaviorIntellectuality

© 2022 - 2024 — McMap. All rights reserved.