actors: not just for movies anymore

53
Actors: not just for movies anymore Coupling your architecture to physics not fiction

Upload: victorops

Post on 17-Jul-2015

54 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Actors: Not Just for Movies Anymore

Actors: not just for movies anymore

Coupling your architecture to physics not fiction

Page 2: Actors: Not Just for Movies Anymore

@boulderdanh

Page 3: Actors: Not Just for Movies Anymore

@Mtn. basecamp

Big rewrite of a database contended pipeline to an event-sourced system

Page 4: Actors: Not Just for Movies Anymore

"Scaling up" is not a sustainable practice

Page 5: Actors: Not Just for Movies Anymore

Scaling to lots of processes is difficult

Page 6: Actors: Not Just for Movies Anymore

Languages and frameworks favor running on a single machine

Page 7: Actors: Not Just for Movies Anymore

Designing and tooling for concurrency can be the answer

Page 8: Actors: Not Just for Movies Anymore

Concurrent vs. Parallel• Simultaneous

• Implementation

• Independent

• Design

• Asynchronous

Page 9: Actors: Not Just for Movies Anymore

@ Transmogrify Inc.

Page 10: Actors: Not Just for Movies Anymore

Concurrency is planning

The Golder

The Shaper

Page 11: Actors: Not Just for Movies Anymore

Construct pipelines

Page 12: Actors: Not Just for Movies Anymore

Construct pipelines

Page 13: Actors: Not Just for Movies Anymore

Isn't this more work?

VS.

Page 14: Actors: Not Just for Movies Anymore

Isn't this slower?

VS.

Page 15: Actors: Not Just for Movies Anymore

Scalable

Page 16: Actors: Not Just for Movies Anymore

Concurrency Parallelism

Page 17: Actors: Not Just for Movies Anymore

Parallelism scales

"The parallelism in today’s machines is limited by the data dependencies in the program and by memory delays and

resource contention stalls "

Page 18: Actors: Not Just for Movies Anymore

A resource

computation 1 computation 2 computation 3 computation 4 computation 5

Page 19: Actors: Not Just for Movies Anymore

Concurrency in frameworksMonolithic

Rails

LAMP

Distributed

Finagle

Erlang / OTP

Page 20: Actors: Not Just for Movies Anymore

DB

Web Process

Web Process

Web Process

Web Process

Page 21: Actors: Not Just for Movies Anymore

Latent Concurrency

Page 22: Actors: Not Just for Movies Anymore

Latent Concurrency

• Concurrency is unplanned

• Rely's on a subset of the system

Page 23: Actors: Not Just for Movies Anymore

Web Process

Web Process

Web Process

Web Process

Service A

Service B

Service C

Service D

Service E

Worker

Worker

Worker

Worker

Page 24: Actors: Not Just for Movies Anymore

Holistic Concurrency

• Concurrency is planned and constructed

• Concurrency is a property of the system

• Reduction in contention / sharing

Page 25: Actors: Not Just for Movies Anymore

Holistic Concurrency

• Parallelizable / Scalable

• Resource density

• Fine grained scaling

Page 26: Actors: Not Just for Movies Anymore

Plan for concurrent systems

Page 27: Actors: Not Just for Movies Anymore

The fundamental choice

Shared data

or

Message passing

Page 28: Actors: Not Just for Movies Anymore

Concurrency in frameworks

Shared data Message passing

Page 29: Actors: Not Just for Movies Anymore

Shared data concurrency

Locks

Semaphores

https://stackoverflow.com/questions/tagged/thread-safety

Synchronization

CAS

Atomic

Memory barriers

STMJSR-133

Happens before

a tag cloud of pain, in comic sans

Thread safety

Page 30: Actors: Not Just for Movies Anymore

Shared data

lots of primitives

Locks

Semaphores

Atomic

Memory barriers

STM

Volatile

Page 31: Actors: Not Just for Movies Anymore

Shared datacorrectness is elusive

Page 32: Actors: Not Just for Movies Anymore

Shared data

"The first huge barrier to bringing clockless chips to market is the lack of automated

tools to accelerate their design"

Page 33: Actors: Not Just for Movies Anymore
Page 34: Actors: Not Just for Movies Anymore

Actors, abstractly

• create actors

• send messages

• store information for the next message

Page 35: Actors: Not Just for Movies Anymore

Implementation

• mailbox

• similar to an object

• concurrency and distribution

Page 36: Actors: Not Just for Movies Anymore

Coupled with physics

• Actor sends

• Stop

• +1

• +1

• +1

• What state does it end up in?

Actor

Stop

+1

+1

+1

Page 37: Actors: Not Just for Movies Anymore

Actor definitionclass Counter extends Actor { var count = 0 def receive: Receive = { case Increment(by) => count += by case Get => sender ! count } }

Page 38: Actors: Not Just for Movies Anymore

Actor definitionclass Counter extends Actor { def receive = next(0) // initialize base state def next(count: Int): Receive = { case Increment(by) => become(next(count + by)) case Get => sender ! count } }

store information for the next message

Page 39: Actors: Not Just for Movies Anymore

Actors as bank accounts

class BankAccount(name: String) extends Actor { var count = 0 def receive = { case Credit(by) => count += by case Balance => sender ! count case Debit(by) if (count - by) < 0 => sender ! NSF case Debit(by) => count -= by case "whoru?" => sender ! name } }

Page 40: Actors: Not Just for Movies Anymore

Actors can create actors

class Bank(name: String, insured: Boolean) extends Actor { def receive = { case AddAccount(name) => context.actorOf(Props(new BankAccount(name))) }}

Page 41: Actors: Not Just for Movies Anymore

A program using actors

override def main(arg: Array[String]) = { val system = ActorSystem() val counter: ActorRef = system.actorOf(Props[Counter]) counter ! Increment(10) val result = counter ? Get result.onSuccess { case t => println(t) } }

Page 42: Actors: Not Just for Movies Anymore

A key abstractionval counter: ActorRef = system.actorOf(Props[Counter])

• The address for an actor

• Tells you nothing about where the actor is

• Deployment is a runtime/config decision

Page 43: Actors: Not Just for Movies Anymore

Sending messages

• Asynchronous

• Response is optional

acct ! Increment(10) acct.tell(Increment(10))

def !(message: Any): Unit

Page 44: Actors: Not Just for Movies Anymore

• Still Asynchronous

• Implemented with Actors

Asking for information(counter ? Get).onSuccess { case t => println(t) }

Page 45: Actors: Not Just for Movies Anymore

Actors are great at concurrency

• No synchronization

• Communication is asynchronous

• Late binding deployment decisions

Page 46: Actors: Not Just for Movies Anymore

Actors are great at concurrency

• Light weight

• Actors are micro-services

Page 47: Actors: Not Just for Movies Anymore

Surely there are other ways

Page 48: Actors: Not Just for Movies Anymore

Communicating Sequential Processes

Page 49: Actors: Not Just for Movies Anymore

Key distinctions

• The channel is fundamental

• Communication is synchronous

• Channels are anonymous vs. named actors

Page 50: Actors: Not Just for Movies Anymore

Message passing frameworks

• Streams (Reactive Java)

• DataFlow

• CPS (not to be confused with CSP)

Page 51: Actors: Not Just for Movies Anymore

Wrapping up

• Concurrency is inevitable

• Use tools that help you write software to plan for it

• Choose tools that promote message passing

• Scale your systems

Page 52: Actors: Not Just for Movies Anymore

Any Questions?

@boulderdanh

Page 53: Actors: Not Just for Movies Anymore

References

• Everything you wanted to know about the actor model - http://bit.ly/16O4qSP

• A Universal Modular ACTOR Formalism for Artificial Intelligence - Carl Hewitt

• Communicating Sequential Processes - C.A.R. Hoare

• Coming Challenges in Microarchitecture and Architecture - Ronny Ronen

• The Tail at Scale - Jeffrey Dean and Luiz André Barroso