Actors – Actor Communication – Actor Use-Cases



Actors – Actor Communication – Actor Use-Cases

0 4


actor-based-concurrency-presentation

A presentation on using actors for your model of concurrency

On Github JohnMurray / actor-based-concurrency-presentation

Actor Model

The Plan

  • Theory
  • Practical Usage
  • Example and Further Study

Theory

Actor Model

A method of concurrency in which the universal primitive is an actor

Other Concurrency Models

  • Process-based concurrency
  • Thread-based concurrency

Actors

Properties of Actors

  • Actors are persistent
  • Encapsulate internal state
  • Actors are asynchronous

What Can Actors

  • Create new actors
  • Receive messages and in response:
    • make local decisions (e.g. alter local state)
    • perform arbitrary, side-effecting action
    • send messages
    • respond to the sender 0 or more times
  • Process exactly one message at a time

Actors

Do not communicate by sharing memory; instead, share memory by communicating. ‐ Effective Go

Actor Communication

Properties of Communication

  • No channels or intermediaries (such as in CSP)
  • "Best Effort" delivery
  • At-most-once delivery
  • Messages can take arbitrary long to be delivered
  • No message ordering guarantees

Address

  • Identifies an Actor
  • May also represent a proxy / forwarder to an Actor
  • Contains location and transport information
  • Location transparency

Address

  • Many to many relationship between actors and addresses
  • One address may represent many actors (pool)
  • One actor may have many addresses

Handling Failure

Supervision

The running state of an actor is monitored and managed by another actor (the Supervisor)

Properties of Supervision

  • Constantly monitors running state of actor
  • Can perform actions based on the state of the actor (e.g. unhandled error)
  • Can stop/kill or restart supervised actors

Supervision Trees

Transparent Lifecycle Management

  • Addresses do not change during restarts
  • Mailboxes are persisted outside the actor instances

Practical Usage

Checking Account

  • A shared account balance
  • Multiple, simultaneous withdrawls
  • Requires coordination / locking to ensure account is not overdrawn

Checking Account

  • Current balance of $80
  • Person A wishes to withdrawl $60
  • Person B wishes to withdrawl $50

Checking Account

01|  struct Checking {
02|      balance int
03|  }
04|  
05|  currentBal = Checking.balance;
06|  if( currentBal > withdrawlAmt ) {
07|      Checking.balance -= withdrawlAmt;
08|      return true;
09|  } else {
10|      return false;
11|  }

Checking Account

01|  struct Checking {
02|      balance int
03|      lock    Mutex
04|  }
05|  
06|  Checking.lock.Lock();
07|  currentBal = Checking.balance;
08|  if( currentBal > withdrawlAmt ) {
09|      Checking.balance -= withdrawlAmt;
10|      return true;
11|  } else {
12|      return false;
13|  }
14|  Checking.lock.Free();

Checking Account

01|  actor Checking {
02|      var balance = 80
03|  
04|      def receive = {
05|          case Withdrawl(amt) =>
06|              if( balance > amt ) {
07|                  balance -= amt
08|                  sender sendMsg true
09|              } else {
10|                  sender sendMsg false
11|              }
12|      }
13|  }

Checking Account

01|  // send message to withdrawl 60 dollars
02|  Checking sendMsg Withdrawl(60)
03|
04|  // send message to withdrawl 50 dollars
05|  Checking sendMsg Withdrawl(50)

Actor Use-Cases

  • Processing pipeline
  • Streaming data
  • Multi-user concurrency
  • Systems high uptime requirements (Ericsson)
  • Concurrent Applications

Frameworks / Languages

  • Erlang / Elixer
  • Akka (JVM)
  • Orleans (.NET)
  • Celluloid (Ruby)
  • Pulsar (Python)

Additional Resources

John Murray

Actor Model