hvidgaard
hvidgaard

Reputation: 495

Akka.net persistence as text

I'm playing around with persistent actors in akka.net using the sqlserver as a backend.

Is it possible to configure Akka.net with sqlserver persistence such that it will persist with a text payload rather than the varbinary it does now?

Upvotes: 2

Views: 400

Answers (2)

Yoav Kaplan
Yoav Kaplan

Reputation: 41

The simple answer no, the true answer, yes you can. You will need to write your own extension, persistence plugin. (I wrote one for Redis). I haven'y published it on GitHub since it is propriety of the company I am working at but you can look here for example:

Redis plugin at github

Take Look here for extra classes you need to implement

YourPersistenceExtension: IExtension{ ... }

YourExtensionProvider: ExtensionIdProvider<YourPersistenceExtension>{ ... }

I will give you a very short road-map but before:

Akka.Persistence contains 2 types of storage's:

  1. Journal - used to store events in continues fashion.
  2. Snapshot - used to store snapshots at a point of time you decide in order to speed up recover time.

You can implement just the Journal if this suit your needs, you don't have to use snapshots (but recommended that you do but for the purpose of this answer it is not significant).

In order to implement a journal you need to derive from Akka.Persistence.AsyncWriteJournal

This is where you can implement your requirements for write/read your events to SQL Server (or any other data store of your choice), you just read/write whatever you want and manage it. For example in redis I used sorted sets since my actors contained unique Id, so I am able to group all the persistence events related to a specific actor in a single value (sorted set) correspondent to that Id. This is important since you want to restore all the events of an actor fast and simple. In addition the base class AsyncWriteJournal contains methods that works with sequence order notations so it is natural to use sorted set. Those considerations should be used by you upon implementing your persistence extension.

Documentation can be found here: AsyncWriteJournal

You will need to configure the usage of your journal in the hocon file by using your type, namespace as you do with other types of data store configurations.

Once you configured your Journal Akka will use your plugin to persist state/events upon calling Persist/PersistAsync from your PersistentActor (or derived types).

I know it sounds overkill but it is really not, especially with the road-map I explained here and references I gave to you, it is up to you if you really need this or not.

Upvotes: 0

alexvaluyskiy
alexvaluyskiy

Reputation: 65

You could not persist your payload as a text instead of VarBinary because Akka.Persistence.SqlServer uses akka serialization mechanism, which trying to find the concrete serializer for your payload's type, and this serializer will serialize your message to a bytearray and will store it to a database

var serializer = Serialization.FindSerializerFor(e.Payload);
var binary = serializer.ToBinary(e.Payload);
AddParameter(command, "@Payload", DbType.Binary, binary);

Upvotes: 3

Related Questions