Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrency with STM vs AKKA

I have data which need to be updated in concurrent manner. This data lives in PlayFramework as a Singleton Object. Would using AKKA to update this data better in this situation or should I be using STM? They both seems to be doing the same thing so I was wondering which is better.

What's the difference between these two solutions in terms of shared state concurrency.

Thanks

like image 626
user_1357 Avatar asked Dec 04 '25 14:12

user_1357


2 Answers

STM allows exclusive writes but many reads where Actors only allows single reads or writes at a time. So when you're in the situation where you want multiple threads to read the shared state concurrently and this operation is rapid, STM is better suited than the Actors.

like image 140
user_1357 Avatar answered Dec 07 '25 06:12

user_1357


Play is built on Akka so you've got the advantage that you can start using it immediately. There is no shared data between actors. Instead data (aka messages) are passed from one Actor to another. Message data should be immutable to further achieve the goal of preventing shared mutable state.

You could have a singleton actor that retains internally any state and any access to it achieved with an "ask" request (based on a Future). Therefore the Actor becomes the gate keeper to that singleton data and enforces serial access to it via the messaging system.

I cannot comment about STM and therefore may sound a little biased!

Cheers,

like image 20
reggoodwin Avatar answered Dec 07 '25 06:12

reggoodwin