Initialize state from database for the very first access to persistent actor

Hi All,

Let’s say I am migrating a legacy application to Akka persistence based actor system, from the old system customer balance supposed to migrate to new system, I would like to initialize the initial state/balance of the actor to the balance coming from old system.

How could I achieve this scenario?



1 Like

One way would be to transform/rewrite the PersistentActor in to an EventSourcedBehavior but keeping the event and state types, that way you can replay the events written with the classic actor in a new typed one. If you have kept all events you could also just throw away the snapshots and implement a new type of state and replay from start for each actor (and then start using snapshots with the new state). The good part about this is that you don’t lose the history of the original actor and can keep using the same journal config, persistence-ids etc.

I’m reading that as the original application is not using Akka persistence at all, and maybe not event sourcing at all.

I’d recommend storing the initial state/balance as a first initial event for each persistent actor. That could even be done by a migration job and not being part of the real EventSourcedBehavior that is later used in real production.

One difficulty might be if the legacy application is still running and changing the data while the migration job is running or afterwards.

Also, as Johan hinted, I would recommend using EventSourcedBehavior in Akka 2.6 rather than the classic PersistentActor if you are building a new application.


@johanandren and @patriknw thanks for reply.

I think I couldn’t elaborate myself, by persistent actor I meant to EventSourcedBehavior in Akka 2.6.

I was also thinking about a migration job.