Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Actor Model and Akka


Published on

The slides of the speech I given during coscup 2011. The topic is "Programming for the Future, Introduction to Actor Model and Akka"

Introduction to Actor Model and Akka

  1. 1. Introduction to Actor Model and Akka by Yung-Lin Ho
  2. 2. The Challenge• The clock speed stops growing since 2006• The free lunch is over• Moore’s Law still applies but only the number of cores in a single chip is increasing. source:
  3. 3. Concurrency and Parallelism• Concurrency: A condition that exists when at least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism.• Parallelism: A condition that arises when at least two threads are executing simultaneously.• Both of them are hard because of shared mutable state. ref: Suns Multithreaded Programming Guide
  4. 4. Shared Memory Concurrency• Race Condition • do you remember what happened when you implemented your first web counter in php?• Dead Lock• Blocking Calls
  5. 5. The solutions• Functional Programming - Everything is immutable. scala> List(1, 2, 3) + 2) res: List[Int] = List(3, 4, 5)• Actor Model - Keep mutable state internal and communicate with each other through asynchronous messages.
  6. 6. A Brief of the Actor Model• Formalized in 1973 by Carl Hewitt and refined by Gul Agha in mid 80s.• The first major adoption is done by Ericsson in mid 80s. • Invented Erlang and later open-sourced in 90s. • Built a distributed, concurrent, and fault-tolerant telcom system which has 99.9999999% uptime
  7. 7. Actors• Lightweight object. Thread• Running own itself own thread. Actor mailbox• No shared state. behavior• Messages are kept in mailbox and processed in order. State• Massive scalable and lighting fast because of the small call stack.
  8. 8. Actorssrc src msg:(result) src msg:(content, src) actor’ actor’’ actor’’’ msg:(content’, src) msg:(content’’, src)
  9. 9. Fault Tolerance in Actor Model supervisorworker worker worker worker
  10. 10. Fault Tolerance in Actor Model supervisorworker worker worker worker
  11. 11. Fault Tolerance in Actor Model supervisor worker worker worker worker•One-For-One restart strategy•One-For-All restart strategy
  12. 12. Akka• Founded by Jonas Boner and now part of Typesafe stack.• Actor implementation on JVM. • Java API and Scala API• Remote Actor• Software Transactional Memory• Modules: akka-camel, akka-mist, akka-spring, akka-guice.
  13. 13. Define An Actor// define actor protocolcase object Increasecase object GetCount// define actorclass Counter extends Actor { private var counter = 0 def receive = { case: Increase => counter += 1 case: GetCount => self.reply(counter) }}
  14. 14. Create An Actorimport actorRef is the reference to the actor.val counter: ActorRef = Actor.actorOf(new Counter).start// send message to an actor.// send asynchronously. fire and forgetcounter ! Increase// send and wait (until timeout)val valueOpt = (counter !! GetCount).as[Int]
  15. 15. Send !!!// send and returns a futureval future = counter !!! GetCounterfuture.awaitval result = future.get// wait, there is more.// execute closure when future is completed.future.onComplete { f => println(f.get())}
  16. 16. More on Futures.// build a model for a EC site.def doSearch(userId: String, keyword: String) { val sessionFuture = sessionManager !!! GetSession(userId) val adFuture = advertiser !!! GetAdvertisement val resultFuture = searcher !!! Search(keyword) val recommFuture = { session => recommender !!! Get(keyword, session) } val responseFuture = for { ad: Advertisement <- adFuture result: SearchResult <- resultFuture recomm: Recommendation <- recommFuture } yield new Model(ad, result, recomm) return responseFuture.get}
  17. 17. ActorRegistry• Find local actorRef(s) by type Actor.registry.actorsFor[MyActor]• Find local actorRef(s) by id Actor.registry.actorsFor(id)
  18. 18. Supervisor• val supervisor = Supervisor( SupervisorConfig( AllForOneStrategy( List(classOf[Exception]), 3, 1000), Supervise( actorOf[MyActor1], Permanent) :: Supervise( actorOf[MyActor2], Permanent) :: Nil))
  19. 19. Remote Actor• Client initiated and managed Actor• Server initated and managed Actor
  20. 20. Client Managed Actor// client supervised remote actor.spawnLinkRemote[MyActor](host, port) Server Managed Actor// start the server.remote.start(“localhost”, 2552)// start a actor locally.val counter = actorOf(new Counter()).start// made counter available for caller from other machines.remote.register(“counter”, counter)// obtain the counter through remote interface.val counter2 = remote.actorFor(“counter”, “localhost”, 2552)
  21. 21. ActorRef are serializable• You can serialize an ActorRef and then pass it to other clients. • You can push an ActorRef to a central repository so that clients does not need to know the ip address of the service provider. • 0 download time when pushing a new version of a service to production.
  22. 22. Who uses Akka• Rule based system • Trading System• Game System
  23. 23. Q&A