Akka Persistence Serializer lifecycles

Hi everyone.

I’m currently trying to implement encryption of persisted events in my application. I’ve read the docs on creating custom serializers here: https://doc.akka.io/docs/akka/current/serialization.html but there is no information on how or what creates new serializer instances or how transient they are (does an actor system only ever create one instance per type or does it have a pool of them?).

I’m currently working on the assumption that a custom serializer implementation can not be passed anything other than a ExtendedActorSystem parameter (though if I could pass in other parameters it would be a big help).

So my current implementation required the serializer to look-up an actor ref and message an actor to retrieve the correct encryption key. This though leads me to have to use the Await construct as the toBinary and fromBinary methods are synchronous.

Does anyone have/know a ‘better way’ of providing persistence serializers with encryption keys? I have seen that Lightbend have a GdprEncryption module as part of their ‘Akka Enhancements’ (https://doc.akka.io/docs/akka-enhancements/current/gdpr/encryption.html#gdprencryption) but alas I don’t have access to those so its DIY time.

Thanks,

Tim

Hi Tim

A serializer is creatred per binding in akka.actor.serializers and is kept around for the duration of the ActorSystem.

I’m currently working on the assumption that a custom serializer implementation can not be passed anything other than a ExtendedActorSystem parameter

Either that or an empty ctr.

This though leads me to have to use the Await construct as the toBinary and fromBinary methods are synchronous.

You can instead implement an AsyncSerializer: https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/serialization/AsyncSerializer.scala

However as AsyncSerializers were added after the fact, code that uses the serializer needs to expect
it and then act accordingly. The default implementation if used via the Serializer interface does block. E.g. the Cassandra persistence plugin supports async serializers without blocking: https://github.com/akka/akka-persistence-cassandra/blob/c6e78226c7a30a7b121f73b1fbf984440bcd1f71/core/src/main/scala/akka/persistence/cassandra/journal/CassandraJournal.scala#L786 but remoting currently does not.

1 Like

Thanks Christopher, that’s really useful information.

I’m using the akka-peristence-dynamo plugin, no support in main branch for AsyncSerializer atm but there is an active pull request for an implementation https://github.com/akka/akka-persistence-dynamodb/pull/80 so there is still hope for me!

Cheers,

Tim