• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to Actor Model and Akka

Introduction to Actor Model and Akka



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

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



Total Views
Views on SlideShare
Embed Views



5 Embeds 26

https://twitter.com 19
http://www.linkedin.com 3
https://www.linkedin.com 2
http://www.pdfok.com 1
http://www.slashdocs.com 1


Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Introduction to Actor Model and Akka Introduction to Actor Model and Akka Presentation Transcript

  • Introduction to Actor Model and Akka by Yung-Lin Ho yho@bluetangstudio.com
  • 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: http://en.wikipedia.org/wiki/Amdahls_law
  • 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
  • Shared Memory Concurrency• Race Condition • do you remember what happened when you implemented your first web counter in php?• Dead Lock• Blocking Calls
  • The solutions• Functional Programming - Everything is immutable. scala> List(1, 2, 3).par.map(_ + 2) res: List[Int] = List(3, 4, 5)• Actor Model - Keep mutable state internal and communicate with each other through asynchronous messages.
  • 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
  • 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.
  • Actorssrc src msg:(result) src msg:(content, src) actor’ actor’’ actor’’’ msg:(content’, src) msg:(content’’, src)
  • Fault Tolerance in Actor Model supervisorworker worker worker worker
  • Fault Tolerance in Actor Model supervisorworker worker worker worker
  • Fault Tolerance in Actor Model supervisor worker worker worker worker•One-For-One restart strategy•One-For-All restart strategy
  • 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.
  • 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) }}
  • Create An Actorimport akka.actor.Actorimport akka.actor.Actor._import akka.actor.ActorRef// 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]
  • 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())}
  • 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 = sessionFuture.map { 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}
  • ActorRegistry• Find local actorRef(s) by type Actor.registry.actorsFor[MyActor]• Find local actorRef(s) by id Actor.registry.actorsFor(id)
  • Supervisor• val supervisor = Supervisor( SupervisorConfig( AllForOneStrategy( List(classOf[Exception]), 3, 1000), Supervise( actorOf[MyActor1], Permanent) :: Supervise( actorOf[MyActor2], Permanent) :: Nil)) supervisor.link(actor3)
  • Remote Actor• Client initiated and managed Actor• Server initated and managed Actor
  • 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)
  • 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.
  • Who uses Akka• Rule based system • Trading System• Game System
  • Q&A