Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actors: How to efficiently handle read-mostly data

Tags:

scala

akka

actor

Suppose I have an actor that has a single field. 99 out of every 100 messages to an actor read the value, and the 100th updates the value. In this case, I would like to process reads in parallel. In other words, how can I achieve the performance of Read/Write locks using Actors? Is this practical with Scala-standard actors or Akka? Or am I missing the point of actors :)

Update: fixed confusing language, sorry

like image 724
Adam Rabung Avatar asked Dec 04 '10 18:12

Adam Rabung


3 Answers

[Disclaimer: I'm the PO of Akka]

I suggest using Agents instead, the read can be done at any point, but only one write at a time.

http://doc.akkasource.org/agents-scala

EDIT: http://doc.akka.io/docs/akka/2.1.0/scala/agents.html (try this link)

like image 200
Viktor Klang Avatar answered Nov 14 '22 06:11

Viktor Klang


You're quite possibly missing the point of actors. I'm assuming you want an actor to which you send a query, and then it sends back a response. There's a fair amount of machinery involved in sending an actor a message, processing it, and sending a response back. The actual generation of the response message will be created as a task and submitted to a thread pool. Between the message queue and the thread pool there are multiple places that require locks or at best CAS operations. Usually the point is that the actor will do some work off in a separate thread based on that message.

If you just want to read and rarely write data (such as incrementing a counter, or access in a value in a map) you'll be much better off using an appropriate class from java.util.concurrent.

like image 35
Erik Engbrecht Avatar answered Nov 14 '22 07:11

Erik Engbrecht


I assume you mean that all messages are immutable and that almost all messages do not mutate the state of the actor. In this case, actors are probably not the best choice of design. Actors allow you to manage any mutable state as if you were dealing with single threaded code.

Effectively, each actor has a mailbox to which messages are sent and then processed one at a time. In your scenario, this would be quite wasteful. As suggested, using something from java.util.concurrent would be most effective.

like image 29
denis phillips Avatar answered Nov 14 '22 06:11

denis phillips