How to achieve concurrency in akka and handle global state

222 views
Skip to first unread message

Atin Sood

unread,
Aug 18, 2015, 4:16:36 AM8/18/15
to Akka User List

Hi 


I am new to learning akka and I am looking into a problem of trying to create a movie hall reservation system. I am trying to wrap my head around how to achieve concurrency with akka. If I end up creating multiple instances of akka actors then where/how should I share the global state of the total number of seats booked and how many are still available.


I believe it should be in the same code that starts the actor system. Or am I misunderstanding the whole concept.


In understand this is an extremely basic question but I wasn't able to find any good resources explaining this, so will really appreciate if you can point me in the right direction.


Regards

Atin Sood

unread,
Aug 18, 2015, 11:05:44 AM8/18/15
to Akka User List
Re posting since I wasn't able to get any feedback on the last post. 

Johan Andrén

unread,
Aug 18, 2015, 11:46:10 AM8/18/15
to Akka User List
Hi Atin,

A very central idea when working with actors is to not share any mutable state, instead mutable state is encapsulated inside of the actors. If one actor needs to know something about the state of another actor it will ask for it using immutable messages and get an immutable reply.

This way there is no need to use regular threading primitives. It does require a mind shift though. I would recommend reading one of the Akka books to get deeper into the concept. Here is a list of books covering akka and actors: http://doc.akka.io/docs/akka/snapshot/additional/books.html

--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

Brian Thibault

unread,
Aug 18, 2015, 1:39:07 PM8/18/15
to Akka User List


Whole idea is no global state... remember actors are an abstraction that allow you to build distributed, concurrent systems. Concurrency in general is bad with shared state. 

Each Actor can maintain its own state. One easy idea might be to create an actor to encapsulate some state you want to preserve. Like TicketActor ... then query that actor when you need to know how many tix are available. But as Johan said, sounds like you should read up some more. 

- BT

Lance Arlaus

unread,
Aug 20, 2015, 5:18:07 PM8/20/15
to Akka User List
Atin-

Though it's tough to recommend an approach without knowing your specific requirements, you may want to look into Agents if they fit your use case.
Sharing an Agent among Actors is perfectly safe and they provide well-defined semantics for concurrent updates.
Reading the current value from an Agent is dead simple without the need to bounce messages around. You can either get the current value directly, or obtain a future that'll complete once all currently outstanding updates have been applied. Which you choose will depend on your requirements, of course.


I wouldn't overuse Agents in an architecture, but they're a very handy tool to have in your toolbox.
That said, other comments on this thread still apply. The direct use of mutable shared state is verboten in concurrent applications! Agents safely wrap this state for you.

Regards,
Lance
Reply all
Reply to author
Forward
0 new messages