haufe #msaday - the actor model: an alternative approach to concurrency by lorenzo nicora

23
The Actor Model 1 An alternative approach to concurrency Lorenzo Nicora Senior Consultant @ OpenCredo @nicusX https://opencredo.com/author/lorenzo/

Upload: opencredo

Post on 16-Apr-2017

143 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

The Actor Model

1

An alternative approach to concurrency

Lorenzo Nicora Senior Consultant @ OpenCredo

@nicusX https://opencredo.com/author/lorenzo/

Page 2: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

✓ (1973) Carl Hewitt: “A Universal Modular Actor Formalism for Artificial Intelligence”

2

A bit of History…

1973

2009

✓ (2009) Akka toolkit, Scala/Java✓ (1986) Ericsson: Erlang

Lorenzo Nicora Actor Model

Page 3: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

According to Alan Kay, author of Smalltalk and inventor of the “Object-Orientation”:

3

The original idea of Object

An “Object”

✓ Is the basic unit of object-oriented system

✓ Has its own state

✓ Communicate with others

by sending and receiving messages*

* in Java, C++, C# (derived from Simula, not Smalltalk) Objects interact by invoking methods

1980

Lorenzo Nicora Actor Model

Page 4: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

An “Actor” is a computational entity that, in response to a message it receives, can concurrently:

a) Send a finite number of messages to other actors

b) Create a finite number of new actors

c) Designate the behaviour to be used for the next message it receives. [implies a state]

4

The Actor

Actor

Actor

Actor

Mailbox Mailbox

Mailbox

Lorenzo Nicora Actor Model

Page 5: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

Actors interacts only by messaging

Actors react to messages

5

Actors are Reactive

Lorenzo Nicora Actor Model

Page 6: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

An Actor handles one message a timeNever concurrently

An Actor has a state State cannot be modified from outside the actor

6

Thread-safe

An Actor is inherently thread-safeNo Lock/Synchronisation on State

Lorenzo Nicora Actor Model

Page 7: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

7

Supervision

Actors are created by Actors

Parent -> Child

Lorenzo Nicora Actor Model

Page 8: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

8

Supervision

Failure Handling • If a Child throws an Exception, the Supervisor get notified • Supervision Strategy: e.g. relaunch, abandon…

Failure is always handled consistently

Parent = Supervisor

Lorenzo Nicora Actor Model

Page 9: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

9

Lightweight

Actor is lightweight

Creating an Actor has little overhead beyond creating an Object

Lorenzo Nicora Actor Model

Page 10: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

Non-blocking implementation of Request/Response pattern

with multiple downstream collaborators

10

Example: Non-blocking Request/Response

• One-Actor-per-Request pattern

• Enterprise Integration Patterns: ✴ Request/Reply ✴ Return Address ✴ Scatter-Gather, Aggregators ✴ Round-robin Message Router

Lorenzo Nicora Actor Model

Page 11: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

11

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 12: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

12

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 13: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

13

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 14: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

14

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 15: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

15

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 16: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

16

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 17: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

17

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 18: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

18

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 19: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

19

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 20: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

20

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 21: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

21

Example: Non-blocking Request/Response

Lorenzo Nicora Actor Model

Page 22: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

22

Conclusions

Actor model: an alternative approach to concurrency

Actors, Threads

+ Business logic implementation + inherently thread-safe

- A new programming paradigm - to learn

Lorenzo Nicora Actor Model

Page 23: Haufe #msaday - The Actor model: an alternative approach to concurrency By Lorenzo Nicora

23Lorenzo Nicora Actor Model