Introduction to actor model with examples
on Akka.NET
Arthur Shvetsov 1
 Overview and history
 What is an actor model?
 Akka.NET main concepts
 Use-case scenarios
Agenda
2
200 200
300
400
500
1000
1800
2530
3200
3600
2200
2930 3000
3200
3330 3330
3150 3200 3150 3150 3150 3150
1 1 1 1 1 1 1 1 1 1 2 2
4 4
8 8
16 16
32 32
64 64
0
10
20
30
40
50
60
70
0
500
1000
1500
2000
2500
3000
3500
4000
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
CPU clock speed vs number of cores
CPU clock speed (MHz) # of Cores
3
 Make classes and functions
 Find areas where you can do multiple things at once
 Queue work onto a new thread or thread pool
 Use synchronization mechanisms to protect shared memory
 Finish work on one thread
 Consume that work on another
How we write multithreaded code?
4
5
 Statefulness
 Concurrency
 Recovering from failures
 Bottlenecks
 Availability
Distributed programming pains
6
Multithreading
7
8
- If we don’t want to write multithread code?
- Use concurrency abstractions instead
9
We won’t talk about TPL today
10
The Actor Model
11
 1973 - Concept formulated by Carl Hewitt et al.: “A universal
modular ACTOR formalism for artificial intelligence”
 1986 - Gul Agha, doctoral dissertation "Actors: A Model of
Concurrent Computation in Distributed Systems“
 1986 - Joe Armstrong and others in Ericsson - Erlang programming
language and VM.
 2009 - initial release of Akka framework (JVM, Scala)
 2014.02 - initial release of Akka.NET - Aaron Stannard, Roger
Actor model history
12
 The Actor Model provides a higher level of abstraction for writing
concurrent and distributed systems.
 Actor Model alleviates the developer from having to deal with explicit
locking and thread management, making it easier to write correct
concurrent and parallel systems.
 Actor Model defines some general rules for how the system’s
components should behave and interact with each other.
What is an actor model?
13
 No deadlocks
 No shared state
 No global mutable state
 Fault isolation – “let it crash”
 Encapsulation
Actor model benefits
14
 Akka.NET is a toolkit and runtime for building highly concurrent,
distributed, and fault tolerant event-driven applications on .NET &
Mono.
 Akka.NET is a port of the popular Java/Scala framework Akka to
.NET.
What is Akka.NET?
15
Everything is an actor!
From OOP to Actor Model paradigm shift
16
Actor is a code unit organization
What is actor?
OOP Actor
Behavior
State
Synchronous calls
Behavior
State
Asynchronous messages
17
What is actor?
18
 All communication is done via message-passing
 All messages are immutable
 Sender and recipient are decoupled, asynchronous
Actors are fundamental units of work & concurrency
19
 Process messages
 Contain private state, invisible from the outside and changes it upon
reception of a message
 Change behavior between messages (i.e. state machine)
Actors can
20
 One message processed at a time
 Messages processed in FIFO order
 “At Most Once” delivery guarantied:
 at-most-once delivery
 at-least-once delivery
 exactly-once delivery
Actors promise
21
Example
22
Switchable Behaviors
23
Actor exists as hierarchies
• Parent actors supervise child actors
• Actors are resilient to failures
24
One-For-One Strategy vs. All-For-One Strategy
Actor supervision
25
 Communicate with actors through “actor reference”
All actors are identified by a unique address
26
Actor addresses have location transparency
27
28
 Location transparency
 Remote addressing
 Remote messaging
 Remote deployment
Akka.Remote
29
 Thread
 Instance of an object/component
 Publisher/Subscriber
 Singleton or service (e.g. data access layer)
 State machine
 Load balancer or router
Actor use cases
30
 http://getakka.net/
 https://github.com/petabridge/akka-bootcamp
References
31
Thank you!
Arthur Shvetsov
32

Introduction to actor model with examples on Akka.NET

  • 1.
    Introduction to actormodel with examples on Akka.NET Arthur Shvetsov 1
  • 2.
     Overview andhistory  What is an actor model?  Akka.NET main concepts  Use-case scenarios Agenda 2
  • 3.
    200 200 300 400 500 1000 1800 2530 3200 3600 2200 2930 3000 3200 33303330 3150 3200 3150 3150 3150 3150 1 1 1 1 1 1 1 1 1 1 2 2 4 4 8 8 16 16 32 32 64 64 0 10 20 30 40 50 60 70 0 500 1000 1500 2000 2500 3000 3500 4000 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 CPU clock speed vs number of cores CPU clock speed (MHz) # of Cores 3
  • 4.
     Make classesand functions  Find areas where you can do multiple things at once  Queue work onto a new thread or thread pool  Use synchronization mechanisms to protect shared memory  Finish work on one thread  Consume that work on another How we write multithreaded code? 4
  • 5.
  • 6.
     Statefulness  Concurrency Recovering from failures  Bottlenecks  Availability Distributed programming pains 6
  • 7.
  • 8.
  • 9.
    - If wedon’t want to write multithread code? - Use concurrency abstractions instead 9
  • 10.
    We won’t talkabout TPL today 10
  • 11.
  • 12.
     1973 -Concept formulated by Carl Hewitt et al.: “A universal modular ACTOR formalism for artificial intelligence”  1986 - Gul Agha, doctoral dissertation "Actors: A Model of Concurrent Computation in Distributed Systems“  1986 - Joe Armstrong and others in Ericsson - Erlang programming language and VM.  2009 - initial release of Akka framework (JVM, Scala)  2014.02 - initial release of Akka.NET - Aaron Stannard, Roger Actor model history 12
  • 13.
     The ActorModel provides a higher level of abstraction for writing concurrent and distributed systems.  Actor Model alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.  Actor Model defines some general rules for how the system’s components should behave and interact with each other. What is an actor model? 13
  • 14.
     No deadlocks No shared state  No global mutable state  Fault isolation – “let it crash”  Encapsulation Actor model benefits 14
  • 15.
     Akka.NET isa toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on .NET & Mono.  Akka.NET is a port of the popular Java/Scala framework Akka to .NET. What is Akka.NET? 15
  • 16.
    Everything is anactor! From OOP to Actor Model paradigm shift 16
  • 17.
    Actor is acode unit organization What is actor? OOP Actor Behavior State Synchronous calls Behavior State Asynchronous messages 17
  • 18.
  • 19.
     All communicationis done via message-passing  All messages are immutable  Sender and recipient are decoupled, asynchronous Actors are fundamental units of work & concurrency 19
  • 20.
     Process messages Contain private state, invisible from the outside and changes it upon reception of a message  Change behavior between messages (i.e. state machine) Actors can 20
  • 21.
     One messageprocessed at a time  Messages processed in FIFO order  “At Most Once” delivery guarantied:  at-most-once delivery  at-least-once delivery  exactly-once delivery Actors promise 21
  • 22.
  • 23.
  • 24.
    Actor exists ashierarchies • Parent actors supervise child actors • Actors are resilient to failures 24
  • 25.
    One-For-One Strategy vs.All-For-One Strategy Actor supervision 25
  • 26.
     Communicate withactors through “actor reference” All actors are identified by a unique address 26
  • 27.
    Actor addresses havelocation transparency 27
  • 28.
  • 29.
     Location transparency Remote addressing  Remote messaging  Remote deployment Akka.Remote 29
  • 30.
     Thread  Instanceof an object/component  Publisher/Subscriber  Singleton or service (e.g. data access layer)  State machine  Load balancer or router Actor use cases 30
  • 31.
  • 32.