2. What we do
●
Machine log analytics company
●
Scala shop
●
Akka is used extensively
●
We are a bunch of passionate hackers who solve
concurrency, race conditions, memory overcommit issues
●
We process 1 TB worth of logs everyday
●
Akka, Cassandra, Solr, H2
3. Akka
●
Pronounced as 'Ahkka', part of Typesafe stack
●
Framework for:
–
Distributed
–
Asynchronous
–
Concurrent
–
Non-blocking
–
Event driven
–
Message passing
5. Actor Model
●
3 axioms – When an Actor receives a message
it can:
–
Create new Actors
–
Send messages to other Actors
–
Decide how it should handle the next message it
receives
6. Akka Actors
●
●
High level abstraction
No need to worry about thread locking and
synchronization
●
Fault tolerant
●
Location transparent
●
Support for Java and Scala APIs
7. Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
case HelloWorld(person) ⇒ log.info("Hello " + person)
}
}
object TestHelloWorld {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello")
helloWorldActorRef ! HelloWorld("Mr.X")
}
}
8. Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
case HelloWorld(person) ⇒ log.info("Hello " + person)
}
}
Create an
object TestHelloWorld {Actor System
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello")
helloWorldActorRef ! HelloWorld("Mr.X")
}
}
9. Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
case HelloWorld(person) ⇒ log.info("Hello " + person)
}
}
Create an
object TestHelloWorld {Actor System
Create an Actor
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello")
helloWorldActorRef ! HelloWorld("Mr.X")
}
}
10. Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
case HelloWorld(person) ⇒ log.info("Hello " + person)
}
}
Create an
object TestHelloWorld {Actor System
Create an Actor
def main(args: Array[String]): Unit = {
Send message
val system = ActorSystem("MySystem")
val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello")
helloWorldActorRef ! HelloWorld("Mr.X")
}
}
14. File system like name resolution
Guardian System Actor
/HelloWorldActor
HelloWorldActor
system.actorOf(Props[HelloWorldActor], name = "hello")
B
/HelloWorldActor/A
A
/HelloWorldActor/B
17. Actors != Threads
●
General confusion that actors are threads. They
are not!
●
Actors are perceived as processes
●
Actors are assigned to threads by 'Dispatcher'
●
Many different kinds of dispatchers:
–
Default dispatcher
–
Pinned dispatcher
–
Balancing dispatcher (not covered in this talk)
–
CallingThreadDispatcher (not covered in this talk)
18. Routers
●
●
Router is an actor that first receives messages and
routes them to other actors
Can be configured to be any one of the routers:
–
Round Robin
–
Random Router
–
Smallest Mailbox
–
Broadcast Router
–
Scatter Gather First Completed
–
Consistent Hashing
19. Create a router
val router1 =
system.actorOf(Props[ExampleActor1]
.withRouter(RoundRobinRouter(nrOfInstances = 5)))
26. Best practices
●
●
●
●
Do not block inside an Actor Eg: a big while
loop, network socket etc
Prefer immutable messages between actors
instead of mutable objects
Do not create too many “ActorSystem”s.
Instead create as many actors as you want.
Do not send behaviour as messages. Might
accidently share state.