7

This question isn't as philosophical as the title might suggest. Consider the following approach to persistence:

Commands to perform Operations come in from various Clients. I represent both Operations and Clients as persistent actors. The Client's state is the lastOperationId to pass through. The Operation's state is pretty much an FSM of the Operation's progress (it's effectively a Saga, as it then needs to reach out to other systems external to the ActorSystem in order to move through it's states).

A Reception actor receives the operation command, which contains the client id and operation id. The Reception actor creates or retrieves the Client actor and forwards it the command. The Client actor reads and validates the operation command, persists it, creates an OperationReceived event, updates its own state with the this operation id. Now it needs to create a new Operation actor to manage the new long-running operation. But here is where I get lost and all the nice examples in the documentation and on the various blogs don't help. Most commentators say that a PersistentActor converts commands to events, and then updates their state. They may also have side effects as long as they are not invoked during replay. So I have two areas of confusion:

  1. Is the creation of an Operation actor in this context equivalent to creating state, or performing a side effect? It doesn't seem like a side effect, but at the same time it's not changing its own state, but causing a state change in a new child.
  2. Am I supposed to construct a Command to send to the new Operation actor or will I simply forward it the OperationReceived event?

If I go with my assumption that creating a child actor is not a side effect, it means I must also create the child when replaying. This in turn would cause the state of the child to be recovered.

I hope the underlying question is clear. I feel it's a general question, but the best way I can formulate it is by giving a specific example.

Edit: On reflection, I think that the creation of one persistent actor from another is an act of creating state, albeit outsourced. That means that the event that triggers the creation will trigger that creation on a subsequent replay (which will lead to the retrieval of the child's own persisted state). This makes me think that passing the event (rather than a wrapping command) might be the cleanest thing to do as the same event can be applied to update the state in both parent and child. There should be no need to persist the event as it comes into the child - it has already been persisted in the parent and will replay.

4
  • I will add the above edit as an answer as the question itself seems to have a number of upvotes.
    – Brendan
    Dec 21, 2015 at 13:28
  • Do you have a concept on how to combine this architecture with a snapshot journal? Since the state of the child actors is not reflected in the parent actor, how would you go about this?
    – thwiegan
    May 2, 2016 at 19:53
  • @thwiegan sorry for the delay in replying - your question (also made below by flare) is very pertinant. Given the reality of snapshots, I think it's unavoidable to say that any persistant actor instance can only be responsible for its own state. My concerns were around the question of 'what will cause a child actor to be recovered from persistence when its parent is recovered?' I think the answer is that there is no need. A child actor will only be recovered when it is directly referenced. It seems to be a category error to try to treat the graph of actors as a data structure in its own right.
    – Brendan
    Apr 4, 2017 at 13:57
  • I'll update the answer accordingly.
    – Brendan
    Apr 4, 2017 at 13:57

1 Answer 1

2

On reflection, I think that the creation of one persistent actor from another is an act of creating state, albeit outsourced. That means that the event that triggers the creation will trigger that same creation on a subsequent replay. This makes me think that passing the event (rather than a wrapping command) might be the cleanest thing to do as the same event can be applied to update the state in both parent and child. There should be no need to persist the event as it comes into the child - it has already been persisted in the parent and will replay.

2
  • will this work strategy (creating child actors via replay) in case of snapshots? i.e. when application is restarted and if snapshots are used, not all messages will be replayed.
    – flare
    Mar 30, 2017 at 11:23
  • @flare that's an excellent point, and I think the answer is a resounding 'no, this will not work'. I'm coming back to Akka after a gap and I think I'll have to get my thinking straight before proceeding. Thanks!
    – Brendan
    Apr 4, 2017 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.