By default, actors in Akka.NET lose their internal state when they are restarted, for example due to an internal actor error or a system crash.
Akka.Persistence adds additional functionality to enable the storing and restoring of actor state.
There are 2 main persistence concepts: journal storage and snapshot storage.
As messages are received by an actor, they may cause some change in the internal actor state. Using Akka Persistence, these messages can be saved in one of a number of configurable journal stores such as SQL Server. When the actor restarts, the messages from the configured journal store are retrieved and “replayed” by the actor. The messages from the journal store will be replayed in the same order as they were originally received by the actor. This in effect rebuilds the actor state one message at a time, each replayed message changing actor state. Once all messages from the journal store have been replayed, the actor state will be equal to what it was before it restarted; it can now process new messages sent to it.
Snapshots are an optional addition on top of the journal store. If an actor has too many messages being replayed from the journal store, and this is causing a performance problem, snapshots can be implemented.
A snapshot, as its name suggests, is a picture of the actor’s state at a given point in time. Snapshots can be created in actor code whenever it is deemed appropriate, for example after every 50 messages have been processed.
Snapshots can improve actor recovery speeds because only those messages stored in the journal since the last snapshot was created need to be replayed. For example, an actor has received 60 messages and a snapshot was created on the 50th message. On restart, Akka.NET notices that a snapshot was created after the 50th message; this snapshot will be loaded into the actor’s state. Now only 10 messages need to be individually replayed (messages 51-60), rather than all 60 messages individually.
To learn more about Akka.Persistence, check out my Akka.NET Persistence Fundamentals Pluralsight course or the Akka.NET documentation.
You can start watching with a Pluralsight free trial.