Let’s assume an application implemented using Akka Typed has a persistent actor. This persistent actor as part of its operations creates transient (or non-persistent) children actors, each child has a unique ID and these IDs are part of the persisted state. The persistent actor also needs some way of communicating with its children, but we don’t want to persist children’s ActorRef
s as they aren’t really part of the state. On recovery the persistent actor should recreate its children based on the recovered state. It doesn’t sound like a very unusual use case, I’m trying to figure out what’s the cleanest way of implementing it. I could create the children actors inside the andThen
Effect
in my command handler which is meant for side effects, but then there’s no way to save the child’s ActorRef from there. That seems to be a more general characteristic of the typed Persistence API - it’s very hard to have non-persistent state in persistent actors (which could be used for storing the transient children ActorRefs in this case). One solution I came up with is having a sort of “proxy” actor for creating children, keeping a map of IDs and ActorRefs, and forwarding messages based on IDs. The persistent actor holds a reference to that proxy actor and contacts it every time it needs to create a new child or send something to one of the existing children. I have mixed feelings about it though and would appreciate if somebody can point me to a better solution.
Sorry for delay in answering due to summer vacation. This feedback is important and we are currently about to do another iteration of the APIs for Persistence Typed.
Do you use scaladsl or javadsl?
You can have an enclosing class that holds transient state like the children actor refs. You create a new instance of that enclosing class when creating the PersistentBehavior via Behavior.setup. All of the actor’s state doesn’t have to be in the persistent State class.
(It doesn’t even have to be an class, it can be just the code block of Behavior.setup.)
Hi, thanks for the suggestion. I’m using scaladsl. I think I’ve considered something like this, but I didn’t like it because the transient state created in Behavior.setup
(e.g., a map to keep children actor refs) would need to be mutable. It’s common in Akka untyped but somehow doesn’t feel right in Akka typed :)