actors: not just for movies anymore
TRANSCRIPT
![Page 1: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/1.jpg)
Actors: not just for movies anymore
Coupling your architecture to physics not fiction
![Page 2: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/2.jpg)
@boulderdanh
![Page 3: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/3.jpg)
@Mtn. basecamp
Big rewrite of a database contended pipeline to an event-sourced system
![Page 4: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/4.jpg)
"Scaling up" is not a sustainable practice
![Page 5: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/5.jpg)
Scaling to lots of processes is difficult
![Page 6: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/6.jpg)
Languages and frameworks favor running on a single machine
![Page 7: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/7.jpg)
Designing and tooling for concurrency can be the answer
![Page 8: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/8.jpg)
Concurrent vs. Parallel• Simultaneous
• Implementation
• Independent
• Design
• Asynchronous
![Page 9: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/9.jpg)
@ Transmogrify Inc.
![Page 10: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/10.jpg)
Concurrency is planning
The Golder
The Shaper
![Page 11: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/11.jpg)
Construct pipelines
![Page 12: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/12.jpg)
Construct pipelines
![Page 13: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/13.jpg)
Isn't this more work?
VS.
![Page 14: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/14.jpg)
Isn't this slower?
VS.
![Page 15: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/15.jpg)
Scalable
![Page 16: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/16.jpg)
Concurrency Parallelism
![Page 17: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/18.jpg)
A resource
computation 1 computation 2 computation 3 computation 4 computation 5
![Page 19: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/19.jpg)
Concurrency in frameworksMonolithic
Rails
LAMP
Distributed
Finagle
Erlang / OTP
![Page 20: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/20.jpg)
DB
Web Process
Web Process
Web Process
Web Process
![Page 21: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/21.jpg)
Latent Concurrency
![Page 22: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/22.jpg)
Latent Concurrency
• Concurrency is unplanned
• Rely's on a subset of the system
![Page 23: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/25.jpg)
Holistic Concurrency
• Parallelizable / Scalable
• Resource density
• Fine grained scaling
![Page 26: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/26.jpg)
Plan for concurrent systems
![Page 27: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/27.jpg)
The fundamental choice
Shared data
or
Message passing
![Page 28: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/28.jpg)
Concurrency in frameworks
Shared data Message passing
![Page 29: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/30.jpg)
Shared data
lots of primitives
Locks
Semaphores
Atomic
Memory barriers
STM
Volatile
![Page 31: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/31.jpg)
Shared datacorrectness is elusive
![Page 32: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/33.jpg)
![Page 34: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/34.jpg)
Actors, abstractly
• create actors
• send messages
• store information for the next message
![Page 35: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/35.jpg)
Implementation
• mailbox
• similar to an object
• concurrency and distribution
![Page 36: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/44.jpg)
• Still Asynchronous
• Implemented with Actors
Asking for information(counter ? Get).onSuccess { case t => println(t) }
![Page 45: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/45.jpg)
Actors are great at concurrency
• No synchronization
• Communication is asynchronous
• Late binding deployment decisions
![Page 46: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/46.jpg)
Actors are great at concurrency
• Light weight
• Actors are micro-services
![Page 47: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/47.jpg)
Surely there are other ways
![Page 48: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/48.jpg)
Communicating Sequential Processes
![Page 49: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/49.jpg)
Key distinctions
• The channel is fundamental
• Communication is synchronous
• Channels are anonymous vs. named actors
![Page 50: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/50.jpg)
Message passing frameworks
• Streams (Reactive Java)
• DataFlow
• CPS (not to be confused with CSP)
![Page 51: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/52.jpg)
Any Questions?
@boulderdanh
![Page 53: Actors: Not Just for Movies Anymore](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a92a031a28abbd768b48ad/html5/thumbnails/53.jpg)
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