introduction to actor programming

13
Introduction to Actor Programming Wilson de Carvalho

Upload: wilson-de-carvalho

Post on 14-Aug-2015

16 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Actor Programming

Introduction to Actor Programming

Wilson de Carvalho

Page 2: Introduction to Actor Programming

History

● The history of Actors goes back to the 70s, being originally proposed for Erlang;

● Emerged as a way to get scores of machines/CPUs to work together to solve large problems;

● Erlang + Actors used in AXD301, an Ericsson product that achieves nine nines of reliability.

Page 3: Introduction to Actor Programming

Pitfalls in multithreading

● Thread starvation: all threads busy or all deadlocked. User may think system is ok.

● Race conditions: shared data changed by multiple threads simultaneously. Internal logic can be subverted.

● Deadlock: thread 1 locks resource A, thread 2 resource B, then as 2 attempts to lock A and 1 lock B, we have a deadlock.

Page 4: Introduction to Actor Programming

Main idea

● Message passing;● Actors do not share state;● Actors only communicate through immutable

messages;● Actors do not talk to each other directly but

through actor references.

Page 5: Introduction to Actor Programming

Why better?

● Not necessary:○ Manage locks;○ Think about how to protect shared data;○ Scale shared mutable state solution (tough job!).

● More protected from deadlocks, race conditions and starvation;

● Inside an actor we're safe.

Page 6: Introduction to Actor Programming

Example

Page 7: Introduction to Actor Programming

Example: sharing state

Page 8: Introduction to Actor Programming

Example: message passing

1. Immutable messages

2. Asynchronous requests

3. Addresses instead of references

AddressTo(printOffice)

AddressTo(ticketAgent)

Page 9: Introduction to Actor Programming

Message passing x Function calling

Page 10: Introduction to Actor Programming

Fault tolerance

Page 11: Introduction to Actor Programming

Scaling up

● 4096 threads per GB in Linux x64 (stack size is 256kB)

● ~2.7 million actors per GB in Akka

Page 12: Introduction to Actor Programming

Scaling out

Page 13: Introduction to Actor Programming

References

- Books:- Actors in Scala- Akka In Action- Programming in Scala 2ed