@petabridge Petabridge.com
Concurrency Without the Pain:An Intro to the Actor Model
Andrew Skotzko Co-‐founder — Petabridge
@petabridge Petabridge.com
@petabridge Petabridge.com
Why should we care about concurrency?
@petabridge Petabridge.com
1970 -‐ 2005
@petabridge Petabridge.com
@petabridge Petabridge.com
@petabridge Petabridge.com
@petabridge Petabridge.com
@petabridge Petabridge.com
TANSTAAFL“There ain’t no such thing as a free lunch.” —R. A. Heinlein, The Moon Is a Harsh Mistress
@petabridge Petabridge.com
@petabridge Petabridge.com
WTF are you talking about?
@petabridge Petabridge.com
Concurrency.
Time to make friends.
@petabridge Petabridge.com
So what’s the problem?
@petabridge Petabridge.com
How to Write Multithreaded Code
Shared StateProgramming
Make classes andfunctions
Find areas where youcan do multiple things
at once
Queue work onto a newthread or thread pool
Use synchronizationmechanisms to
protected sharedmemory (what?!?!)
Finish work on onethread
Consume that work onanother
@petabridge Petabridge.com
Shared State Programming Sucks (Illustrated)
Shared StateProgramming
Problems
Synchronizing shared state == pitof doom
Deadlocks
Livelocks
Unsafe state
Coordinating work between threads
Debugging multi-threaded code:not for the faint of heart
Hey, that third party code you'reusing - is it thread safe?
Writing thread-safe code is hard
@petabridge Petabridge.com
Random?
• .NET 4.0: TPL (2010) • .NET 4.5: async/await (2012)
@petabridge Petabridge.com
Is there a better way? THERE MUST BE!THERE. MUST. BE. A. BETTER. WAY!
@petabridge Petabridge.com
Warning! Mind Blowingly Different
Programming Style Ahead!
@petabridge Petabridge.com
Warning! Mind Blowingly Different Programming Style Ahead!
@petabridge Petabridge.com
The Actor Model
@petabridge Petabridge.com
What is the actor model?
@petabridge Petabridge.com
Everything is an actor.
@petabridge Petabridge.com
@petabridge Petabridge.com
@petabridge Petabridge.com
3 Core Abilities
1. Send messages 2. Create other actors 3. Change behavior
@petabridge Petabridge.com
Messages
@petabridge Petabridge.com
Actors work by passing each other messages
@petabridge Petabridge.com
Message passing in action
Send "Hello" to "/user/actor1"
Received "Hello" from"/user/actor2"(do some work)
Send "Sup homedawg" to "/user/actor2"
@petabridge Petabridge.com
BUZZWORD BINGO!
@petabridge Petabridge.com
Example Akka.NET Message
@petabridge Petabridge.com
Actors send messages to addresses
akka.tcp: / /MySystem@localhost:9001/user/actorName1
Protocol
ActorSystem
Address
Path
All parts form an "ActorPath"
@petabridge Petabridge.com
Actor addresses have location transparency
@petabridge Petabridge.com
Which means…
@petabridge Petabridge.com
@petabridge Petabridge.com
@petabridge Petabridge.com
Magical scaling sauce! (more on that later)
@petabridge Petabridge.com
ReactiveManifesto.org
@petabridge Petabridge.com
What is Akka.NET?
@petabridge Petabridge.com
Obligatory logos to impress you
@petabridge Petabridge.com
This is an actor
@petabridge Petabridge.com
These are messages
@petabridge Petabridge.com
Actors run on threads
@petabridge Petabridge.com
Actors are lazy
@petabridge Petabridge.com
Process messages one at a time
@petabridge Petabridge.com
The circle of life
@petabridge Petabridge.com
Actor lifecycle
@petabridge Petabridge.com
Actor lifecycle
@petabridge Petabridge.com
@petabridge Petabridge.com
Hierarchies
@petabridge Petabridge.com
Supervision
@petabridge Petabridge.com
Switchable Behavior
@petabridge Petabridge.com
Magic Scaling Sauce!!!
@petabridge Petabridge.com
(High Availability)
@petabridge Petabridge.com
We Can Deploy Actors and Send Messages Anywhere
Nice hair!
Did you hear?My hairrocks!
Dude, don'tencourage
him.
@petabridge Petabridge.com
Scaling Out
• Actors can be deployed anywhere
• Special "Router" actors distribute work
• Actors can be easily restarted and redeployed
@petabridge Petabridge.com
And Can Form Highly Available Systems
Ready forwork!
@petabridge Petabridge.com
http://LearnAkka.net
@petabridge Petabridge.com
Have questions? Ask us!
[email protected] Intro: http://learnakka.net
@petabridge Petabridge.com
Hierarchies pt. deux
@petabridge Petabridge.com
Error Kernel