Your SlideShare is downloading. ×
0
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Introduction to Actor Model and Akka
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to Actor Model and Akka

9,658

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"

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

1 Comment
19 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,658
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
330
Comments
1
Likes
19
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. Introduction to Actor Model and Akka by Yung-Lin Ho yho@bluetangstudio.com
    • 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: http://en.wikipedia.org/wiki/Amdahls_law
    • 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. Shared Memory Concurrency• Race Condition • do you remember what happened when you implemented your first web counter in php?• Dead Lock• Blocking Calls
    • 5. 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.
    • 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. 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. Actorssrc src msg:(result) src msg:(content, src) actor’ actor’’ actor’’’ msg:(content’, src) msg:(content’’, src)
    • 9. Fault Tolerance in Actor Model supervisorworker worker worker worker
    • 10. Fault Tolerance in Actor Model supervisorworker worker worker worker
    • 11. Fault Tolerance in Actor Model supervisor worker worker worker worker•One-For-One restart strategy•One-For-All restart strategy
    • 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. 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. 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]
    • 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. 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}
    • 17. ActorRegistry• Find local actorRef(s) by type Actor.registry.actorsFor[MyActor]• Find local actorRef(s) by id Actor.registry.actorsFor(id)
    • 18. Supervisor• val supervisor = Supervisor( SupervisorConfig( AllForOneStrategy( List(classOf[Exception]), 3, 1000), Supervise( actorOf[MyActor1], Permanent) :: Supervise( actorOf[MyActor2], Permanent) :: Nil)) supervisor.link(actor3)
    • 19. Remote Actor• Client initiated and managed Actor• Server initated and managed Actor
    • 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. 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. Who uses Akka• Rule based system • Trading System• Game System
    • 23. Q&A

    ×