32
Introduction to actor model with examples on Akka.NET Arthur Shvetsov 1

Introduction to actor model with examples on Akka.NET

Embed Size (px)

Citation preview

Page 1: Introduction to actor model with examples on Akka.NET

Introduction to actor model with examples on Akka.NET

Arthur Shvetsov 1

Page 2: Introduction to actor model with examples on Akka.NET

Overview and history What is an actor model? Akka.NET main concepts Use-case scenarios

Agenda

2

Page 3: Introduction to actor model with examples on Akka.NET

1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 20160

500

1000

1500

2000

2500

3000

3500

4000

0

10

20

30

40

50

60

70

200 200 300 400 500

1000

1800

2530

3200

3600

2200

2930 30003200

3330 33303150 3200 3150 3150 3150 3150

1 1 1 1 1 1 1 1 1 1 2 2 4 48 8

16 16

32 32

64 64

CPU clock speed vs number of cores

CPU clock speed (MHz) # of Cores3

Page 4: Introduction to actor model with examples on Akka.NET

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

Page 5: Introduction to actor model with examples on Akka.NET

5

Page 6: Introduction to actor model with examples on Akka.NET

Statefulness Concurrency Recovering from failures Bottlenecks Availability

Distributed programming pains

6

Page 7: Introduction to actor model with examples on Akka.NET

Multithreading

7

Page 8: Introduction to actor model with examples on Akka.NET

8

Page 9: Introduction to actor model with examples on Akka.NET

- If we don’t want to write multithread code?- Use concurrency abstractions instead 

9

Page 10: Introduction to actor model with examples on Akka.NET

We won’t talk about TPL today 

10

Page 11: Introduction to actor model with examples on Akka.NET

The Actor Model

11

Page 12: Introduction to actor model with examples on Akka.NET

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

Page 13: Introduction to actor model with examples on Akka.NET

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

Page 14: Introduction to actor model with examples on Akka.NET

No deadlocks No shared state No global mutable state Fault isolation – “let it crash” Encapsulation

Actor model benefits

14

Page 15: Introduction to actor model with examples on Akka.NET

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

Page 16: Introduction to actor model with examples on Akka.NET

Everything is an actor!

From OOP to Actor Model paradigm shift

16

Page 17: Introduction to actor model with examples on Akka.NET

Actor is a code unit organization

What is actor?

OOP Actor

BehaviorState

Synchronous calls

BehaviorState

Asynchronous messages

17

Page 18: Introduction to actor model with examples on Akka.NET

What is actor?

18

Page 19: Introduction to actor model with examples on Akka.NET

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

Page 20: Introduction to actor model with examples on Akka.NET

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

Page 21: Introduction to actor model with examples on Akka.NET

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

Page 22: Introduction to actor model with examples on Akka.NET

Example

22

Page 23: Introduction to actor model with examples on Akka.NET

Switchable Behaviors

23

Page 24: Introduction to actor model with examples on Akka.NET

Actor exists as hierarchies• Parent actors supervise child actors• Actors are resilient to failures

24

Page 25: Introduction to actor model with examples on Akka.NET

One-For-One Strategy vs. All-For-One Strategy

Actor supervision

25

Page 26: Introduction to actor model with examples on Akka.NET

Communicate with actors through “actor reference”

All actors are identified by a unique address

26

Page 27: Introduction to actor model with examples on Akka.NET

Actor addresses have location transparency

27

Page 28: Introduction to actor model with examples on Akka.NET

28

Page 29: Introduction to actor model with examples on Akka.NET

Location transparency Remote addressing Remote messaging Remote deployment

Akka.Remote

29

Page 30: Introduction to actor model with examples on Akka.NET

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

Page 31: Introduction to actor model with examples on Akka.NET

http://getakka.net/ https://github.com/petabridge/akka-bootcamp

References

31

Page 32: Introduction to actor model with examples on Akka.NET

Thank you!

Arthur Shvetsov

32