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/Amdahl's_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: Sun's 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.
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 protocol
case object Increase
case object GetCount
// define actor
class Counter extends Actor {
private var counter = 0
def receive = {
case: Increase => counter += 1
case: GetCount => self.reply(counter)
}
}
14. Create An Actor
import akka.actor.Actor
import 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 forget
counter ! Increase
// send and wait (until timeout)
val valueOpt = (counter !! GetCount).as[Int]
15. Send !!!
// send and returns a future
val future = counter !!! GetCounter
future.await
val 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)
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