During the talk, we will build a simple web app using Lift and then introduce Akka ( http://akkasource.org) to help scale it. Specifically, we will demonstrate Remote Actors, "Let it crash" fail over, and Dispatcher. Other Scala oriented tools we will use include sbt and ENSIME mode for emacs.
5. Scaling Web Apps with Akka
Range of mountains in Sweden
Open Source Project
Written in Scala with Scala/Java APIs
6. Scaling Web Apps with Akka
Actors Model
Multi-Core and Cloud Infrastructure
Graceful Failure a la Erlang OTP
Transactions a la Clojure
NoSQL and Distributed Data Stores
Flexible Dispatch Strategies
7. Actors Model of Computing
Defined in 1963 by Carl Hewitt
Used by Erlang to support reliability (99.9999999% uptime)
8. Actors Model of Computing
"encapsulates state and behavior into a lightweight
process/thread" - Jonas Bonér
Asynchronous message passing between Actors
Messages are immutable
Synchronized mailbox
Mailbox is read with pattern matching
No shared state
No locks
Safe concurrency
11. Erlang Actors Example
-module(mypackage).
myErlangActor() ->
receive
{msgType, Msg} ->
io:format(" p n", [Msg]),
myErlangActor()
Other ->
io:format("wholy canoli, this is unmatched! n"),
myErlangActor()
end.
c(mypackage).
Pid = spawn(fun mypackage:myErlangActor/0).
Pid ! {msgType, "Erlang is for the discerning programmer"}.
12. Scala Thread Based Actors
import scala.actor.Actor
import scala.actor.Actor._
case class Message(msg: String)
class AnActor extends Actor {
def act() = receive {
case Message(m) => println(m)
case _ => println("unknown message")
}
}
class Director {
val myActor = new AnActor
myActor.start
myActor ! Message("threedz can haz block?")
}
13. Scala Event Based Actors
import scala.actor.Actor
import scala.actor.Actor._
case class Message(msg: String)
class AnActor extends Actor {
def act() = loop {
react {
case Message(m) => println(m)
case _ => println("unknown message")
}}}
class Director {
val myActor = new AnActor
myActor.start
myActor ! Message("eventz iz lightweight?")
}
14. Lift Actors
case class Message(msg: String)
class AnActor extends LiftActor {
def messageHandler = {
case Message(m) => println(m)
case _ => println("unknown message")
}}
class Director {
val myActor = new AnActor
myActor ! Message("Lift Actors is lightest & rulz!")
}
15. Akka Actors
import se.scalablesolutions.akka.actor.Actor
case class Message(msg: String)
class AnActor extends Actor {
def receive = {
case Message(m) => println(m)
case _ => println("unknown message")
}
}
class Director {
val myActor = Actor.actorOf[AnActor]
myActor.start
myActor ! Message("akkastic!")
}
16. Leverage Multi-Core and Distributed
Cloud Infrastructure
Millions of lightweight actors on commodity hardware
Safe Concurrency
Remote Actors
18. Handle Failure Gracefully
"Let it Crash"
Actor exceptions w/out supervision do not behave well
Remote Actors w/out supervision are unmanaged
Linking Actors
Supervisor
Manages linked Actors
Notified when linked Actor crashes
OneForOne
AllForOne
Works with Remote Actors
19. Handle Failure Gracefully: Supervisor
val supervisor = Supervisor(
SupervisorConfig(
RestartStrategy(OneForOne, 3, 1000, List(classOf[Exception])),
Supervise(
actorOf[MyActor],
LifeCycle(Permanent)) ::
Nil))
supervisor.link(actorOf[MyActor])
class MyActor extends Actor {
self.lifeCycle = Some(LifeCycle(Permanent))
override def preRestart(reason: Throwable) {
// clean things up
}
}
20. Manage Transactions
Software Transactional Memory (STM)
Thanks Clojure!
Addresses fundamental weakness of Actors Model
Hello Shared State!
Transactional but still no locks
In memory
Map, Vector, & Ref throw exceptions if modified outside
of transaction
ACI but no D
Transactors
Transactional Actors
Actors + STM
If Actor's writes clash, rollback memory and retried
21. Manage Transactions: Transactors
import se.scalablesolutions.akka.actor.Transactor
import se.scalablesolutions.akka.stm._
class MyActor extends Transactor {
def receive = {
case Increment(counter) => counter.swap(counter.get + 1)
}
}
class Director {
lazy val counter = Ref(0)
//counter.swap(7) // blows up, cannot mutate outside transaction
myActor ! Increment(counter)
}
22. NoSQL & Distributed Data Stores
Pluggable Storage Back Ends
Cassandra
Redis
MongoDB
Others...
Brings the D in ACID
Works with STM and Transactors
28. Alternatives
Before Drinking the Kool-Aid or Flogging Your Fav Tool
Other Actor Impls More Options
scalaz JVM
FunctionalJava Fork/Join
GParallelizer ESB
Kilim Netty
ActorFoundry HawtDispatch
Actors Guild Erlang
Jetlang RabbitMQ
Actorom Hadoop
Fan Actors OpenMP
MPI
29. Questions About Akka?
http://akkasource.org
http://groups.google.com/group/akka-user
Course at Skills Matter Oct 13-14
LSUG talk on Oct 13
Ask me:
@matyjas
matyjas@gmail.com