This document introduces Akka, an open-source toolkit for building highly concurrent, distributed, and fault-tolerant event-driven applications on the JVM. It provides a single unified programming model based on actors that simplifies concurrency, scalability, and fault tolerance. Akka supports both Scala and Java APIs and can be deployed on single nodes or in a distributed, clustered environment in the cloud.
Scaling API-first – The story of a global engineering organization
Akka JUGL 2012
1. Above the Clouds:
Introducing Akka
Vojin Jovanovic, EPFL
Philipp Haller, Typesafe
Based on material provided by the Akka team at Typesafe
2. The problem
It is way too hard to build:
1. correct highly concurrent systems
2. truly scalable systems
3. fault-tolerant systems that self-heal
...using “state-of-the-art” tools
8. WHERE IS AKKA USED?
SOME EXAMPLES:
FINANCE TELECOM
• Stock trend Analysis & Simulation
• Streaming media network gateways
• Event-driven messaging systems
SIMULATION
BETTING & GAMING • 3D simulation engines
• Massive multiplayer online gaming
E-COMMERCE
• High throughput and transactional
• Social media community sites
betting
10. ACTOR CONCURRENCY
• Actors = lightweight isolated processes
• No shared state
• Asynchronous message passing (non-blocking)
• Sequential message processing (one message at a
time)
• Actor mailbox: queue of not-yet-processed messages
19. Actors
case object Tick
class Counter extends Actor {
var counter = 0
def receive = {
case Tick =>
counter += 1
println(counter)
}
}
Scala API
20. Actors
class Counter extends UntypedActor {
int counter = 0;
void onReceive(Object msg) {
if (msg.equals(“Tick”)) {
counter += 1;
System.out.println(counter);
}
}
}
Java API
21. ACTOR SYSTEMS
• Actors are created in the context of an actor system
• Actor system = unit for managing shared facilities:
scheduling services, configuration, logging etc.
• Several actor systems with different configurations
may co-exist within the same JVM instance (there is
no global state within Akka itself)
• There may be millions of actors within a single actor
system
22. Create Application
val conf = ConfigFactory.load(“application”)
val system = ActorSystem(“my-app”, conf)
Scala API
23. Create Application
Config conf =
ConfigFactory.load(“application”);
ActorSystem system =
ActorSystem.create(“my-app”, conf);
Java API
24. Create Actors
val counter = system.actorOf(Props[Counter])
counter is an ActorRef
Creates a top-level actor
Scala API
30. Send: ?
import akka.patterns.ask
// returns a future
val future = actor ? message
future onSuccess {
case x => println(x)
}
returns a Future
Scala API
31. Reply
class SomeActor extends Actor {
def receive = {
case User(name) =>
// reply to sender
sender ! (“Hi ” + name)
}
}
Scala API
32. Reply
class SomeActor extends UntypedActor {
void onReceive(Object msg) {
if (msg instanceof User) {
User user = (User) msg;
// reply to sender
getSender().tell(“Hi ” + user.name);
}
}
}
Java API
33. ...or use ask
import akka.patterns.ask
// returns a future
val future = actor ask message
future onSuccess {
case x => println(x)
}
Scala API
34. ...or use ask
import static akka.patterns.Patterns.ask;
Future<Object> future = ask(actor, message, timeout);
future.onSuccess(new OnSuccess<Object>() {
public void onSuccess(String result) {
System.out.println(result);
}
});
Java API
35. Future
val f = Promise[String]()
f onComplete { ... }
onSuccess { ... }
onFailure { ... }
f foreach { ... }
f map { ... }
f flatMap { ... }
f filter { ... }
f zip otherF
f fallbackTo otherF
Await.result(f, 5 seconds)
Scala API
36. Future
firstCompletedOf
fold
reduce
find
traverse
sequence
Combinators for collections of Futures
59. Parental automatic supervision
// from within an actor
val child = context.actorOf(Props[MyActor], “my-actor”)
transparent and automatic fault handling by design
Scala API
60. Parental automatic supervision
// from within an actor
ActorRef child = getContext().actorOf(
new Props(MyActor.class), “my-actor”);
transparent and automatic fault handling by design
Java API
67. Name resolution
Guardian System Actor
Foo Bar
A
A C
E C
B
B
D
68. Name resolution
Guardian System Actor
/Foo
Foo Bar
A
A C
E C
B
B
D
69. Name resolution
Guardian System Actor
/Foo
Foo Bar
/Foo/A
A
A C
E C
B
B
D
70. Name resolution
Guardian System Actor
/Foo
Foo Bar
/Foo/A
A
A C
/Foo/A/B
E C
B
B
D
71. Name resolution
Guardian System Actor
/Foo
Foo Bar
/Foo/A
A
A C
/Foo/A/B
E C
B
B
D
/Foo/A/D
72. Supervision
class MySupervisor extends Actor {
def supervisorStrategy = OneForOneStrategy({
case _: ActorKilledException => Stop
case _: ArithmeticException => Resume
case _: Exception => Restart
case _ => Escalate
},
maxNrOfRetries = None,
withinTimeRange = None)
def receive = {
case NewUser(name) =>
... = context.actorOf[User](name)
}
} Scala API
73. Supervision
class MySupervisor extends Actor {
def supervisorStrategy = AllForOneStrategy
OneForOneStrategy({
case _: ActorKilledException => Stop
case _: ArithmeticException => Resume
case _: Exception => Restart
case _ => Escalate
},
maxNrOfRetries = None,
withinTimeRange = None)
def receive = {
case NewUser(name) =>
... = context.actorOf[User](name)
}
} Scala API
74. Manage failure
class FaultTolerantService extends Actor {
...
override def preRestart(
reason: Throwable, message: Option[Any]) = {
... // clean up before restart
}
override def postRestart(reason: Throwable) = {
... // init after restart
}
}
Scala API
75. watch/unwatch
val buddy: ActorRef = ...
val watcher = system.actorOf(Props(
new Actor {
context.watch(buddy)
def receive = {
case t: Terminated => ...
}
}
))
Scala API
76. ...and much much more
TestKit FSM
HTTP/REST
EventBus Pub/Sub
Durable Mailboxes Camel
IO
Microkernel SLF4J
AMQP
ZeroMQ Dataflow
Transactors
Agents Spring
77. get it and learn more
http://akka.io
http://typesafe.com