63
Actor Model in Ruby Celso Crivelaro @celsocrivelaro http://crivelaro.me 1

Actor model in Ruby

Embed Size (px)

Citation preview

Page 1: Actor model in Ruby

Actor Model in Ruby

Celso Crivelaro@celsocrivelaro

http://crivelaro.me1

Page 2: Actor model in Ruby

DEVELOPER @

PROFESSOR @

Page 3: Actor model in Ruby
Page 4: Actor model in Ruby

My problem

Page 5: Actor model in Ruby

Authorization

APIs

SMTP Protocol

Strange ProtocolStrange Lib

5

Page 6: Actor model in Ruby

Strange Protocol / Strange Lib => Unmaintained

Bad documentation

Troubleshooting was impossible

Page 7: Actor model in Ruby

Authorization

APIs

TCP/IP

Custom Protocol

SMTP Protocol

Page 8: Actor model in Ruby

High Concurrency ~ 1000 connections

Fault Tolerant

Easy TCP Server API

Page 9: Actor model in Ruby
Page 10: Actor model in Ruby

Actors Reactor

10

Page 11: Actor model in Ruby

Actor Model Theory

Page 12: Actor model in Ruby

Actor 1Mailbox

Actor 2Mailbox

Actor 3Mailbox

Page 13: Actor model in Ruby

Actors are similar to Objects

Page 14: Actor model in Ruby

Send messages to each other

Asynchronous

Actors Objects

Synchronous

Create new ones

Isolated Share memory

Keep state

Page 15: Actor model in Ruby

Actors can be distributed

15

Page 16: Actor model in Ruby

Actor 1Mailbox

Actor 2Mailbox

Actor 3Mailbox

Page 17: Actor model in Ruby

Fault Tolerant

Page 18: Actor model in Ruby

Let it crash philosophy

Page 19: Actor model in Ruby

Actor 1Mailbox

Actor 2Mailbox

Actor 3Mailbox

Actor 4Mailbox

A4 is linked to A2

A2 is warned when A4 crashes

A2 takes an action (restart A4)

Avoid defensive programming

Page 20: Actor model in Ruby

Who implements Actors

20

Page 21: Actor model in Ruby

Pykka

Page 22: Actor model in Ruby
Page 23: Actor model in Ruby

Celluloid ActorsRuby Objects

Same structure from classes

Runs over threads / fibers

Futures and Pools

Sync / Async Methods

Page 24: Actor model in Ruby
Page 25: Actor model in Ruby

25

Page 26: Actor model in Ruby

When Actor crashes…

Page 27: Actor model in Ruby
Page 28: Actor model in Ruby

What if another actor could do

something?

Page 29: Actor model in Ruby

Supervisors

Page 30: Actor model in Ruby

30

Page 31: Actor model in Ruby
Page 32: Actor model in Ruby

Even if an Actor just terminates

Page 33: Actor model in Ruby
Page 34: Actor model in Ruby

ActorSupervisor

Restart Actor

Warn other Supervisor

Callbacks

Avoid begin… rescue... / if

Actions:

Page 35: Actor model in Ruby

Must be finished to be CG’ed

35

Page 36: Actor model in Ruby
Page 37: Actor model in Ruby

Futures

Page 38: Actor model in Ruby
Page 39: Actor model in Ruby

Pool

Page 40: Actor model in Ruby

Res 1 Res 2 Res 3

PoolRes 1

40

Page 41: Actor model in Ruby
Page 42: Actor model in Ruby

http://www.mikeperham.com/2015/10/14/should-you-use-celluloid/

Page 43: Actor model in Ruby
Page 44: Actor model in Ruby

Actors Reactor

Page 45: Actor model in Ruby

Reactor Pattern

45

Page 46: Actor model in Ruby

Handler 1

Handler 2

Dispatcher Handler 3

Handles service request

Delivered concurrently to handlers

Demultiplexing

Event

Event

Event

Page 47: Actor model in Ruby

https://github.com/celluloid/nio4r

Page 48: Actor model in Ruby
Page 49: Actor model in Ruby
Page 50: Actor model in Ruby

Server Celluloid:IOSupervisor

50

Page 51: Actor model in Ruby
Page 52: Actor model in Ruby
Page 53: Actor model in Ruby

Important

Page 54: Actor model in Ruby

Mutant Data Structure

Page 55: Actor model in Ruby

Actor 1Mailbox

Actor 2Mailbox

Actor 3Mailbox

Microservices

55

Page 56: Actor model in Ruby

Evaluate the best concurrency model

Celluloid Documentation

Page 57: Actor model in Ruby

References

Page 58: Actor model in Ruby

http://www.brianstorti.com/the-actor-model/

Page 59: Actor model in Ruby
Page 60: Actor model in Ruby

https://www.sitepoint.com/an-introduction-to-celluloid-part-i/

Page 61: Actor model in Ruby

https://github.com/celluloid/celluloid-io

Page 62: Actor model in Ruby

http://queroserdevlocaweb.com.br/

Page 63: Actor model in Ruby

Thank you!

@celsocrivelaroSlides at http://crivelaro.me