ANNA + HANNAH #ABOUTUS...3/18/2019 Spectacle Boilerplate 1/ 87 Anna Neyzberg @Aneyzb she/her Hannah...

Preview:

Citation preview

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 1/87

AnnaNeyzberg @Aneyzb she/her

Hannah Howard @techgirlwonder

she/her

anna@carbon�ve.com

hannah@carbon�ve.co

m

ANNA + HANNAH #ABOUTUS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 2/87

GO VS. ELIXIR

A Concurrency Comparison

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 3/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 4/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 5/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 6/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 7/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 8/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 9/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 10/87

BUT WE ALSOHAVE

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 11/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 12/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 13/87

SO HOW ISTHIS

RELEVANT?

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 14/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 15/87

“Concurrency isthe composition of

independentlyexecuting

processes, whileparallelism is the

simultaneousexecution of

computations“-

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 16/87

PARALLELISMIS ABOUT

EXECUTINGMANY THINGSAT ONCE. IT'S

FOCUS ISEXECUTION

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 17/87

CONCURRENCYIS ABOUT

DEALING WITHMANY THINGSAT ONCE. IT'S

FOCUS ISSTRUCTURE

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 18/87

THIS REQUIRESCOORDINATION

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 19/87

COORDINATIONINTRODUCES

COMPLEXITY...

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 20/87

HOW DO WE

COORDINATEBETWEEN

TASKSWORKING

TOGETHER?

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 21/87

THE PROBLEM

Concurrency Without Coordination

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 22/87

THREADS COORDINATE BYSHARING DATA

Use same memory spaceUse mutation on shared state to

communicateUse various locks to prevent

overwriting each others work

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 23/87

THREADS

Old School Concurrency

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 24/87

MOST WIDELY

USEDPROGRAMMING

LANGUAGESSUPPORTTHREADS.

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 25/87

MO THREADS

Mo Problems

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 26/87

CHALLENGES WITHTHREADS

1. Lots of shared mutable state2. Dead locks / unpredictabiity3. Expontential complexity to manage

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 27/87

THE ACTORMODEL

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 28/87

INVENTED BYCARL HEWITT

1973

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 29/87

ERLANGCREATED IN

1986

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 30/87

CONCEPTUALMODEL TODEAL WITH

CONCURRENTCOMPUTATION

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 31/87

DEFINESSOME

GENERALRULES FOR

HOW THESYSTEM’S

COMPONENTSSHOULD

BEHAVE ANDINTERACT

WITH EACHOTHER

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 32/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 33/87

One ant is no ant.- https://www.brianstorti.com/the-actor-model/

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 34/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 35/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 36/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 37/87

PROCCESSES1. Are not OS processes2. Light weight3. Do not share memory4. Have a unique ID

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 38/87

THE ACTORMODEL IS

PHYSICALLYBASED

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 39/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 40/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 41/87

DISTRIBUTEDELIXIR

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 42/87

LET IT CRASH

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 43/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 44/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 45/87

THE BEAM

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 46/87

PREEMPTIVESCHEDULING

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 47/87

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 48/87

ELIXIR PRIORITIES1. Scalable2. Fault Tolerant (Telco Strong)3. Fast

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 49/87

GO AND CSP

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 50/87

CSP:Communicating Sequential Processes

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 51/87

PROCESS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 52/87

CSP PROCESS != OS PROCESS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 53/87

CHANNELS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 54/87

PROCESSESKNOW

CHANNELS,NOT OTHERPROCESSES

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 55/87

Processes choose behavior by listeningon multiple channels

CHOICE:

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 56/87

LOOKING AT CSP IN GO

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 57/87

1. package main 2. 3. import "fmt" 4. 5. func read(incoming <-chan string, done chan<- struct{}) { 6. readValue := <-incoming 7. fmt.Printf("%s\n", readValue) 8. done <- struct{}{} 9. } 10. 11. func write(outgoing chan<- string, done chan<- struct{}) {12. outgoing <- "bananas"

Wait for both to complete

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 58/87

SYNCHRONOUSCHANNELS =

COORDINATION

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 59/87

Choice in Go with select!

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 60/87

HOW GO IMPLEMENTS CSP

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 61/87

A goroutine is alightweight thread

managed by the Goruntime.

- Of�cial Tour Of Go

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 62/87

*stares Britnely*

GOROUTINES SHARE MEMORY

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 63/87

BUT WAIT THERE'S MORE...

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 64/87

Sync Package:

ALL THETHREAD SYNCPRIMITIVES, IN

GO!

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 65/87

Do notcommunicate bysharing memory;

instead, sharememory by

communicating.- Go Docs

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 66/87

This approach can be taken toofar. Reference counts may bebest done by putting a mutex

around an integer variable, forinstance. But as a high-levelapproach, using channels to

control access makes it easierto write clear, correct programs.

- Also Go Docs

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 67/87

Other Go Fun Fact:GO ROUTINES ARE SCHEDULED COOPERATIVELY!

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 68/87

WTF GO?

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 69/87

GO COMPILES TO NATIVE CODE

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 70/87

GO RUN-TIME IS SMALL (2MB)

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 71/87

GO IS (ALMOST) A SYSTEMS PROGRAMMING LANGUAGE

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 72/87

GO PRIORITIES1. Ease of adoption2. Speed3. Flexibility

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 73/87

CSP VS ACTOR MODEL

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 74/87

CSP AND ACTOR MODELSIMILARITIES

1. Abstract 'processes' managed byruntime2. Share data bycommunicating/message passing

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 75/87

Erlang's syntaxderived from

Prolog and washeavily in�uencedby smalltalk, CSP

and the functionalprogramming.

- Joe Armstrong, Erlang Creator

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 76/87

Difference #1:

PROCESSIDENTITY

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 77/87

Difference #2:

DIRECTCOMMUNICATION

VS CHANNELS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 78/87

Difference #3:

ASYNCHRONOUSVS

SYNCHRONOUSMESSAGEPASSING

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 79/87

NO SO DIFFERENT AFTER ALL?

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 80/87

HOW TO GIVE CSPPROCESS AN IDENTITY

1 CSP channel+ 1 CSP Process Reading From Channel= Channel Becomes De-facto Identity

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 81/87

HOW TO PASS CSPMESSAGES

ASYNCHRONOUSLYMessage Queue Process+ Channel For Sending+ Channel For Receiving= Asynchronous Message Passing

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 82/87

https://medium.com/capital-one-tech/building-an-unbounded-channel-in-

go-789e175cd2cd

BUILDING AN UNBOUNDED CHANNEL IN GO:

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 83/87

A GenServer in Go!

1. package counter 2. 3. type counterMessage interface { 4. handle(c * Counter) 5. } 6. 7. type Counter struct { 8. counterMessages chan counterMessage 9. 10. // do not touch outside run loop 11. value int

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 84/87

HOW DO WE IMPLEMENT CHANNELS IN ELIXIR?:

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 85/87

12. { queue, len, :unbuffered } ->

1. defmodule ElixirConurrency.Queue do 2. def new(max_size) do 3. { :queue.new, 0, max_size } 4. end 5. 6. def new(:unbuffered) do 7. { :queue.new, 0, :unbuffered } 8. end 9. 10. def put(bounded_queue, item ) do 11. case bounded_queue do

13. queue = :queue.in(item, queue) 14. {{:ok, queue }, len + 1} 15. { queue, len, max_size } -> 16. if len == max_size do 17. {:buffered, queue}

if queue unbuffered

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 86/87

CONCLUSIONS

3/18/2019 Spectacle Boilerplate

http://localhost:3000/#/?export 87/87

http://concurrency.techgirlwonder.com

THANK YOU!

Recommended