Search code examples
scalaakkaakka-persistence

Akka persistence misuse?


Is this a good idea to use akka persistence as a local database? I need to store data locally in case of the server is inaccessible, and my app is built on top of akka, so using akka-persistence sounds like the easiest approach.

The data is filled with a rate about a record per 10 seconds in avg. Still given that akka-persistence is designed for a bit different things, it sounds like a misuse. Any issues I should be aware of?


Solution

  • First let's have a look at what the purpose of akka-persistence is:

    Akka persistence enables stateful actors to persist their internal state so that it can be recovered when an actor is started, restarted after a JVM crash or by a supervisor, or migrated in a cluster. The key concept behind Akka persistence is that only changes to an actor's internal state are persisted but never its current state directly [...]

    The question if its a 'misuse' becomes if the stored data is part of the actors internal state? From the post this is not totally apparent but simply storing incoming messages, this might be better done is a proper message queue.

    As for the reason why the data should be stored? Akka-persistence requires an underlying data-store which might be the same or dependant on the database that is originally unavailable. However 1 message per 10 seconds does not sound terrible as long as that database is not available for long stretches at a time.

    It is also worth noting that simply introducing a local buffer might not be enough to cope with an unreliable resource (Have a look at the circuit breaker pattern). It is always worth considering to have a mechanism which provides back-pressure to inform the producer of the state down the 'flow'. Then it can make an informed choice about what to do (drop, store.. ).

    Storing data locally when it cannot be processed but the concerns mentioned above should be taken into consideration when doing so as there might be more appropriate solutions.