SlideShare a Scribd company logo
Akka in Action
Raymond Roestenburg
@RayRoestenburg
rroestenburg@xebia.com
github.com/RayRoestenburg
manning.com/roestenburg
12mp25 for 42% Off!
Early Akka adopter (v0.7, 2010)
Akka Committer (akka-camel in 2.x)
Co-author of Akka in Action
“The free lunch is over” / TANSTAAFL
Herb Sutter 2005 http://www.gotw.ca/publications/concurrency-ddj.htm
Number
of transistors
Clock Speed
Can‟t I just..
Parallelize
that Sh*t™?
Put that in a box!
Something like this?
Parallel
Version
9
Hey Oracle don‟t sue me, of course it‟s fake
Nope. Parallelize that Sh*t™
Not every algorithm can be parallelized
Dynamic coordination (concurrent)
Amdahl‟s Law
Sequential fraction determines max speedup
Threads?
Shared (mutable) state + locks
Require synchronization
Diminishing returns**
Which primitives to use?
Difficult to predict & maintain
Deadlock
Livelock
Thread starvation
Race condition
*Using threads directly is meant here
**Performance improvement degrades with more threads than cores.
“Life is too short for
Blocking code”*
*Mario Fusco just the other day on twitter
Why Async? Less latency.
Service
Service A
Service B
Service C
4 sec
6 sec
3 sec
Sync takes 13 seconds
Async takes max 6 seconds
Asynchronous „first completed response‟ is easy to do
1. The world is concurrent
2. Things in the world don't share data
3. Things communicate with messages
4. Things fail
Joe Armstrong (creator of Erlang)
We need better tools to build..
Reactive*
Applications
* The latest need-to-know buzz word!
event driven
scalable
asynchronous
resilient
Scale Up and Out
Concurrent
Distributed
Many Nodes
Many ThreadsAkkaA uniform model for both up
and out, embraces
concurrency and failure
Maximize the use of cores
_and_ nodes.
Concurrent(moreCores)
Distributed (more Nodes)
Actors
Futures
Agents
Scale Up
Processes (even less**)
Actors (2.7 million / 1GB)
Threads (4096 / 1GB)
Actors are small*
* Disclaimer: text is not shown to scale, actors are way smaller..
** Especially JVM processes
Reactive Asynchronous Building Blocks
1. CREATE
2. SEND
3. BECOME
4. SUPERVISE*
Actor
Actor
Instance
ActorRef
Mailbox
ActorRef
Actor
Instance
ActorRef
Mailbox
Points to Actor
Delivers messages to Mailbox
Mailbox
Actor
Instance
ActorRef
Mailbox
Processes Messages asynchronously*
Invokes Actor Instance with message
*The Mailbox runs on a Dispatcher that abstracts threading
Actor Instance
Actor
Instance
ActorRef
Mailbox
Receives messages one at a time
Your code runs here.
Actor Instance
Crashed Actor
Instance
Mailbox
An Actor Instance crashes instead of handling
Exceptions. A Supervisor decides it‟s fate.
ActorRef
Restart / Resume / Stop / Escalate
New Actor
Instance
ActorRef
Actor
Instance
Mailbox
Immutable. Only the ActorRef is accessible
From the outside.
ActorRef
ActorRef
Actor
Instance
Mailbox
The ActorRef is used
for both local and remote actors.
ActorRef
ActorRef
Actor
Instance
Mailbox
The ActorRef always points 1 on 1 to the
„same‟ Actor instance. (Alive or Restarted)
ActorRef
Actor
Instance
ActorRef
Dead Actor
Instance
Mailbox
The ActorRef points to deadletters ActorRef
when the actor is dead (terminated).
ActorRef
deadletters
msg
msg
Immutable messages
msg
Location
Transparency
msg Serialized messages
Actor System
Actor System
Actor System
Actor systems
Event
Immutable messages
msg
msg
app@node2
app@node1
app@node3
“app” clustered
actor system
On three nodes
Clustered actor system
msg
msg
“GoTicks.com”
Build a REST API
PUT /events
{
“event” : “RHCP”,
“nrOfTickets”: 300
}
GET /ticket/RHCP
Response:
{
“event”: “RHCP”,
“nr”: 1
}
GET /events
Response:
[
{ “event”: “RHCP”, “nrOfTickets”: 299 }
]
RestApi
Ticket
Seller
Ticket
Seller
BoxOffice
GoTicks.com
Spray
&
Akka
“goticks” ActorSystem
REST Spray-can
GoTicks.com
REST Interface
Handles HTTP calls
Translates to and from
JSON
RestApi
REST Spray-can
GoTicks.com
BoxOffice
REST Spray-can
BoxOffice
Creates Events
Creates TicketSeller
One for every Event
Partition / shard per event
Many sharding strategies possible
GoTicks.com
Ticket
Seller
Ticket
Seller
REST Spray-can
TicketSeller
Contains list of Tickets
Sells Tickets for a Event
until SoldOut
RestApi
Ticket
Seller
Ticket
Seller
BoxOffice
Clustered GoTicks.com
Ticket
Seller
Ticket
Seller
BoxOffice
Consistent
Hashing
Consistent
Hashing
Code
1. CREATE
Inside our Main class…
val system = new ActorSystem(“goticks”)
Create
The system
ActorSystem(“goticks”)
Inside Main…
val system = new ActorSystem(“goticks”)
val api = system.actorOf(
Props[RestInterface],
“httpInterface”)
…
CREATE
RestInterface
ActorSystem(“goticks”)
Top Level Actor
ActorSystem(“goticks”)
REST Interface
Inside Main…
val system = new ActorSystem(“goticks”)
val api = system.actorOf(
Props[RestInterface],
“httpInterface”)
…
CREATE
RestInterface
ActorSystem(“goticks”)
Factory/Creator/ Immutable Configuration object
ActorSystem(“goticks”)
REST Interface
Inside Main…
val system = new ActorSystem(“goticks”)
val api = system.actorOf(
Props[RestInterface],
“httpInterface”)
…
CREATE
RestInterface
ActorSystem(“goticks”)
ActorRef to the Actor
ActorSystem(“goticks”)
REST Interface
ActorSystem(“goticks”)ActorSystem(“goticks”)
Inside RestInterface..
val master = context.actorOf(
Props[BoxOffice],”boxOffice”
)
1. CREATE
BoxOffice
REST Interface
BoxOffice
Child of
REST Interface Actor
ActorSystem(“goticks”)ActorSystem(“goticks”)
Inside RestInterface..
val master = context.actorOf(
Props[BoxOffice],”boxOffice”
)
1. CREATE
BoxOffice
REST Interface
BoxOffice
An Actor has a name, part of an ActorPath
Inside BoxOffice…
val child = context.actorOf(
Props[TicketSeller],
eventName
)
1. CREATE
TicketSeller
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
TicketSeller
Child of
BoxOffice Actor
Inside BoxOffice…
val child = context.actorOf(
Props[TicketSeller],
eventName
)
1. CREATE
TicketSeller
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
TicketSeller
Ticket sellers are named by event
2. SEND/RECEIVE
Receiving messages
class BoxOffice extends Actor {
def receive = {
case TicketRequest(name) =>
…
case GetEvents =>
…
case event: Event =>
…
}
}
Pattern Matching
On Messages
Receiving messages
class TicketSeller extends Actor {
def receive = soldOut
def soldOut:Receive = {
case Tickets(newTickets) =>
.. //store new tickets
become(selling)
}
def selling:Receive = {
case BuyTicket(name) =>
…
}
}
Partial Function
3. BECOME:
You can change th
behavior at
every msg
Create Event
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
Event(“RHCP”, 250)
{ event: “RHCP” nrOfTickets: 250}
Inside RestInterface… (receives JSON, unmarshall to event)
boxOffice ! event
Create Event
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
Event(“RHCP”, 250)
{ event: “RHCP” nrOfTickets: 250}
Inside RestInterface… (receives JSON, unmarshall to event)
boxOffice ! event
Bang Operator
„Tell‟
„One-way‟
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
Event(“RHCP”, 250)
{ event: “RHCP” nrOfTickets: 250}
Inside RestInterface…
boxOffice ! event
Receiver can respond
to „sender‟
Send Event
To BoxOffice
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
EventCreated
Inside BoxOffice…
sender ! EventCreated
Respond with
EventCreated,
results in OK
HTTP OK
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
{ event: “RHCP” nrOfTickets: 250}
BuyTicket
TicketSeller
Inside BoxOffice…
ticketSeller forward BuyTicket
Get a Ticket
Forward keeps sender
(RestInterface)
TicketRequest(“RHCP”)
ActorSystem(“goticks”)ActorSystem(“goticks”)
REST Interface
BoxOffice
{ event: “RHCP”, nr :1}
TicketSeller
Inside TicketSeller…
sender ! ticket
Respond to
original senderBuyTicketTicket(“RHCP”, 1)
CODE
Questions?
@RayRoestenburg
rroestenburg@xebia.com
www.xebia.com
github.com/RayRoestenburg/xebicon
Backup slides
“Async sending subscription
data”
Send
Thread X
Consumer
Consumer
Consumer
Thread 1
Thread 2
Thread 1
Service A
Send
Thread X
Consumer
Consumer
Consumer
Thread 1
Thread 2
Thread 1
Service A
Thread 2 fails
Send
Thread X
Consumer
Consumer
Consumer
Thread 1
Thread 2
Thread 1
Service A
Thread 2 fails
Handle every possible error
inside Thread 2, never fail?
Not completely under your control.
Send
Thread X
Consumer
Consumer
Consumer
Thread 1
Thread 2
Thread 1
Service A
Thread 2 fails
Restart / Stop?
Monitor?
Handle errors
across all subscriptions?
4. Supervise*
Untangle behavior and errors
„Just Reboot‟
Let It Crash ™
Error Kernel
4. Supervise*
Strategies
One for One
(Poison Pill /Seppuku)
All for One
4. Supervise*
val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 5,
withinTimeRange = 1 minute) {
case _: Exception => Restart
}
4. Supervise*
val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 5,
withinTimeRange = 1 minute) {
case _: Exception => Restart
}
Restart,
Resume,
Escalate,
Stop,

More Related Content

PDF
PDF
Functions, Types, Programs and Effects
PDF
Thirteen ways of looking at a turtle
DOC
EMF Tips n Tricks
PDF
Redux Sagas - React Alicante
PDF
Swift 2
ODP
PDF
Workshop 5: JavaScript testing
Functions, Types, Programs and Effects
Thirteen ways of looking at a turtle
EMF Tips n Tricks
Redux Sagas - React Alicante
Swift 2
Workshop 5: JavaScript testing

What's hot (20)

PDF
Google guava
ODP
Decorators in Python
PDF
The Swift Compiler and Standard Library
PPTX
2. Design patterns. part #2
PDF
JavaScript Functions
PPTX
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
PDF
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
PDF
An Introduction to the World of Testing for Front-End Developers
PDF
Google Guava for cleaner code
PDF
GR8Conf 2011: GPars
PPT
Web Optimization Summit: Coding for Performance
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
Swift Programming Language
PDF
Python decorators
PDF
Zope component architechture
PDF
Google guava - almost everything you need to know
PDF
Swift internals
PDF
Functional Programming Patterns (NDC London 2014)
PDF
Currying and Partial Function Application (PFA)
PPT
Advanced Javascript
Google guava
Decorators in Python
The Swift Compiler and Standard Library
2. Design patterns. part #2
JavaScript Functions
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
An Introduction to the World of Testing for Front-End Developers
Google Guava for cleaner code
GR8Conf 2011: GPars
Web Optimization Summit: Coding for Performance
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Swift Programming Language
Python decorators
Zope component architechture
Google guava - almost everything you need to know
Swift internals
Functional Programming Patterns (NDC London 2014)
Currying and Partial Function Application (PFA)
Advanced Javascript
Ad

Similar to Akka in-action (20)

PDF
Buiilding reactive distributed systems with Akka
PDF
Akka Actors: an Introduction
PDF
First glance at Akka 2.0
PDF
Scalaz 8 vs Akka Actors
PDF
PPT
Clojure concurrency
PPT
Exception handling poirting in gcc
PDF
Building Massively Scalable application with Akka 2.0
ODP
Concurrency on the JVM
PDF
Let'swift "Concurrency in swift"
PDF
A gentle introduction into AKKA and the actor model
PDF
Hadoop Summit Europe 2014: Apache Storm Architecture
KEY
Concurrent programming with Celluloid (MWRC 2012)
PDF
Akka and futures
PPTX
Discovering the Service Fabric's actor model
KEY
Introduction to Actor Model and Akka
PDF
Current State of Coroutines
PDF
The dark side of Akka and the remedy
PDF
Advanced akka features
PDF
Testing akka-actors
Buiilding reactive distributed systems with Akka
Akka Actors: an Introduction
First glance at Akka 2.0
Scalaz 8 vs Akka Actors
Clojure concurrency
Exception handling poirting in gcc
Building Massively Scalable application with Akka 2.0
Concurrency on the JVM
Let'swift "Concurrency in swift"
A gentle introduction into AKKA and the actor model
Hadoop Summit Europe 2014: Apache Storm Architecture
Concurrent programming with Celluloid (MWRC 2012)
Akka and futures
Discovering the Service Fabric's actor model
Introduction to Actor Model and Akka
Current State of Coroutines
The dark side of Akka and the remedy
Advanced akka features
Testing akka-actors
Ad

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
The AUB Centre for AI in Media Proposal.docx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Understanding_Digital_Forensics_Presentation.pptx
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Review of recent advances in non-invasive hemoglobin estimation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development

Akka in-action

Editor's Notes

  • #2: TODOMake specific to Rabo -> Scala Java comparison.Show basic powerful features of Scala anyone would love. (REPL, case classes, pattern matching, collectionsVery simple example of Futures. Very simple example of Actor. Very simple example of Agent?
  • #4: The free lunch is over. (Actually it has been over for a while now!)This graph is from an article by Herb Sutter of C++ fame.While the number of transistors per chip still increases, the clock speed has leveled out over the last coupld of years.Chip manufacturers focus on multi core abilities instead of higher clock speeds.So new chips will not immediately increase the speed of sequential programs.This means that concurrent programs are necessary to make full use of multi-core CPU’s
  • #5: Can’t I just get these really smart compiler and runtime guys to do parallelization behind the scenes?Gimme that shiny box!Released in 2018? I kid.
  • #6: Some part of your application is going to be sequential, which limits the improvement that can be achieved by parallel execution.Algorithms that require very low sequential execution can benefit the most from parallel execution.Example: Map Reduce style (batch) processing
  • #7: ThreadsAn standalone execution path within a process - Has its own timeline- Call Stack- Shares Address space (Heap) with other Threads within the same process Performance improvement degrades with more threads than cores.Knowing up front which locks need to be locked Breaks encapsulationAll locks need to be taken before modification - in the correct orderAll locks must be unlocked Thread are expensive and their lifecycle management is error prone Traditional problem: Dining Philosophers
  • #8: ThreadsAn standalone execution path within a process - Has its own timeline- Call Stack- Shares Address space (Heap) with other Threads within the same process Performance improvement degrades with more threads than cores.Knowing up front which locks need to be locked Breaks encapsulationAll locks need to be taken before modification - in the correct orderAll locks must be unlocked Thread are expensive and their lifecycle management is error prone Traditional problem: Dining Philosophers
  • #10: Threads do not compose, or interact using exceptions, they all have their own stacktrace.Communicating between threadgroups is not possible using traditional exceptions. Roll your own.You will need to use concurrent data structures to communicate with the threads, like queues for instance.
  • #11: Can’t I just get these really smart compiler and runtime guys to do parallelization behind the scenes?Gimme that shiny box!
  • #12: Event driven – The world is event drivenScalable – use both more cores and nodesAsynchronous – Waiting is a bad way to use processing cycle. Life is too short to block.Resilient – More robust and easier failure handling of concurrent and distributed systems
  • #13: Single Node, Single ThreadLMAX Disruptor, runs on one thread, achieves 100.000 tpsNode.Js (Javascript is singlethreaded) But what if that one thread blocks, or fails..Scale up by just running more processes, but how do you coordinate concurrency?Processes are heavier than threads, and maybe there is even something lighter…Many Nodes, Single ThreadedCombine the single thread processes with some kind of network communication, Like 0MQ, RabbitMQ, ..MQ. Distributed Coordination can be done with a distributed database, or something like Apache Zookeeper.Which are all technologies that live on a diffferent planet, which you will have to master. If need be OK, but this is getting complex.Single Node, Many ThreadsWhat if we just use java.util.concurrent? Concurrent data structures, directly using Threads, locks, semaphores? How hard can deadlocks, livelocks, thread starvation, race conditions really be to debug?Many Nodes, Many Threads.A combination of even more technologies?
  • #14: Today we are only going to talk about actors, unless we have time left.
  • #20: One at a time. Sequential in the small. Concurrent and Async in large numbers.
  • #21: At Restart the new actor instance gets to handle next messages.There is an option to handle the message that was being processed during the crash.
  • #34: Today we are only going to talk about actors, unless we have time left.
  • #53: Today we are only going to talk about actors, unless we have time left.
  • #55: Today we are only going to talk about actors, unless we have time left.
  • #56: A Service sends subscription based messages using traditional Network I/O. Async because one consumer must not block others. Below Subscription X type messages are sent to consumers.Threads do not compose, or interact using exceptions, they all have their own stacktrace.Communicating between threadgroups is not possible using traditional exceptions. Roll your own.You will need to use concurrent data structures to communicate with the threads, like queues for instance.You can’t guarantee that a thread will be able to handle all possible errors itself, without knowing information about the bigger system or larger context.(network usage policy, behavior of consumer across subsystems could mean the difference between cutting the consumer off, or restarting transmissions To a different endpoint)
  • #57: A Service sends subscription based messages using traditional Network I/O. Async because one consumer must not block others. Below Subscription X type messages are sent to consumers.Threads do not compose, or interact using exceptions, they all have their own stacktrace.Communicating between threadgroups is not possible using traditional exceptions. Roll your own.You will need to use concurrent data structures to communicate with the threads, like queues for instance.You can’t guarantee that a thread will be able to handle all possible errors itself, without knowing information about the bigger system or larger context.(network usage policy, behavior of consumer across subsystems could mean the difference between cutting the consumer off, or restarting transmissions To a different endpoint)
  • #58: A Service sends subscription based messages using traditional Network I/O. Async because one consumer must not block others. Below Subscription X type messages are sent to consumers.Threads do not compose, or interact using exceptions, they all have their own stacktrace.Communicating between threadgroups is not possible using traditional exceptions. Roll your own.You will need to use concurrent data structures to communicate with the threads, like queues for instance.You can’t guarantee that a thread will be able to handle all possible errors itself, without knowing information about the bigger system or larger context.(network usage policy, behavior of consumer across subsystems could mean the difference between cutting the consumer off, or restarting transmissions To a different endpoint)
  • #59: A Service sends subscription based messages using traditional Network I/O. Async because one consumer must not block others. Below Subscription X type messages are sent to consumers.Threads do not compose, or interact using exceptions, they all have their own stacktrace.Communicating between threadgroups is not possible using traditional exceptions. Roll your own.You will need to use concurrent data structures to communicate with the threads, like queues for instance.You can’t guarantee that a thread will be able to handle all possible errors itself, without knowing information about the bigger system or larger context.(network usage policy, behavior of consumer across subsystems could mean the difference between cutting the consumer off, or restarting transmissions To a different endpoint)