Slide 1

Slide 1 text

@josevalim / Plataformatec Concurrency Ruby in

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

•Started with Ruby in 2006 About me

Slide 4

Slide 4 text

•Started with Ruby in 2006 •Open Source Contributor About me

Slide 5

Slide 5 text

•Started with Ruby in 2006 •Open Source Contributor •Joined Rails Core in 2010 About me

Slide 6

Slide 6 text

•Started with Ruby in 2006 •Open Source Contributor •Joined Rails Core in 2010 •Author of elixir-lang.org About me

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Topics

Slide 9

Slide 9 text

•Why concurrency? Topics

Slide 10

Slide 10 text

•Why concurrency? •The problem today Topics

Slide 11

Slide 11 text

•Why concurrency? •The problem today •Tools of the trade Topics

Slide 12

Slide 12 text

Why concurrency?

Slide 13

Slide 13 text

Server Server Server Server

Slide 14

Slide 14 text

50 cores $2600

Slide 15

Slide 15 text

It is no longer about the future, it is about now

Slide 16

Slide 16 text

Server

Slide 17

Slide 17 text

In Ruby, we talk about threads

Slide 18

Slide 18 text

We need our applications to be thread-safe

Slide 19

Slide 19 text

The problem

Slide 20

Slide 20 text

class User def self.current_user=(user) @@current_user = user end def self.current_user @@current_user end end

Slide 21

Slide 21 text

post "/do_something" do User.current_user = user_from_request # some work Mailer.deliver_to( User.current_user) end

Slide 22

Slide 22 text

1. Assign current_user 2. Do some work 3. Get current_user to send an e-mail

Slide 23

Slide 23 text

thread 1 current_ user thread 2

Slide 24

Slide 24 text

thread 1 current_ user thread 2 jose

Slide 25

Slide 25 text

thread 1 current_ user thread 2 jose jose

Slide 26

Slide 26 text

thread 1 current_ user thread 2 jose jose jose

Slide 27

Slide 27 text

thread 1 current_ user thread 2 jose jose jose jose

Slide 28

Slide 28 text

thread 1 current_ user thread 2 jose jose matz jose jose

Slide 29

Slide 29 text

thread 1 current_ user thread 2 jose jose matz matz jose jose

Slide 30

Slide 30 text

thread 1 current_ user thread 2 jose jose matz matz jose jose jose

Slide 31

Slide 31 text

thread 1 current_ user thread 2 jose jose matz matz jose jose jose jose

Slide 32

Slide 32 text

thread 1 current_ user thread 2 jose jose matz matz jose jose jose jose jose

Slide 33

Slide 33 text

thread 1 current_ user thread 2 jose jose matz matz jose jose jose jose jose jose

Slide 34

Slide 34 text

thread 1 current_ user thread 2 jose jose matz matz jose jose jose jose jose jose

Slide 35

Slide 35 text

User.current_user is shared mutable state (global)

Slide 36

Slide 36 text

ActionMailer::Base.from ActionController::Base.logger Devise.password_length SimpleForm.form_options

Slide 37

Slide 37 text

What is Ruby?

Slide 38

Slide 38 text

Ruby is a programming language with different implementations

Slide 39

Slide 39 text

Rails 2.2 threadsafe

Slide 40

Slide 40 text

Thread-safety means different things for different implementations

Slide 41

Slide 41 text

hash = {} 10.times do |i| Thread.new { hash[i] = "Hello #{rand}" } end Threadsafe?

Slide 42

Slide 42 text

Thread-safe YARV Yes JRUBY No RBX 2.0 No Hashes

Slide 43

Slide 43 text

global virtual machine lock (YARV) @cached[template] = File.read(template) static VALUE env_aset(VALUE obj, VALUE nm, VALUE val) { ... hash.c

Slide 44

Slide 44 text

global virtual machine lock (YARV) @cached[template] = File.read(template) static VALUE env_aset(VALUE obj, VALUE nm, VALUE val) { ... hash.c

Slide 45

Slide 45 text

C Code C2 C3 C4 C1

Slide 46

Slide 46 text

C Code C2 C3 C4 C1

Slide 47

Slide 47 text

C Code C2 C3 C4 C1

Slide 48

Slide 48 text

C Code C2 C3 C4 C1

Slide 49

Slide 49 text

C Code C2 C3 C4 C1

Slide 50

Slide 50 text

Without the GVL, other Ruby implementations may try to change the same Hash at the same time, corrupting it

Slide 51

Slide 51 text

• Class definitions • Instance variables • Class variables • Data structures ...

Slide 52

Slide 52 text

Tools of the trade

Slide 53

Slide 53 text

Thread locals

Slide 54

Slide 54 text

class User def self.current_user=(user) @@current_user = user end def self.current_user @@current_user end end

Slide 55

Slide 55 text

class User def self.current_user=(user) Thread.current[:cu] = user end def self.current_user Thread.current[:cu] end end

Slide 56

Slide 56 text

Time.zone I18n.locale ActiveSupport::Notifications. instrumenter

Slide 57

Slide 57 text

class Auth def initialize(callback) @callback = callback end def call(info) @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end

Slide 58

Slide 58 text

auth = Auth.new -> { puts "Hello" } auth.call(id: 13)

Slide 59

Slide 59 text

class Auth def initialize(callback) @callback = callback end def call(info) @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end

Slide 60

Slide 60 text

class AuthMiddleware def initialize(callback) @callback = callback end def call(info) @user = user_from_info(info) res = @callback.call(info) update_user(res) # access @user res end end

Slide 61

Slide 61 text

config.middleware.use( AuthMiddleware )

Slide 62

Slide 62 text

Eager loading

Slide 63

Slide 63 text

class MyApp < Sinatra::Base autoload :Broker, "my_app/broker" post "/content" do # ... MyApp::Broker.push(info) end end Threadsafe?

Slide 64

Slide 64 text

module MyApp::Broker # ... def push do # ... end # ... end

Slide 65

Slide 65 text

module MyApp::Broker # ... def push do # ... end # ... end

Slide 66

Slide 66 text

module MyApp::Broker # ... def push do # ... end # ... end

Slide 67

Slide 67 text

Let’s replace all autoloads by requires!

Slide 68

Slide 68 text

module MyApp extend ActiveSupport::Autoload eager_autoload do autoload :Broker end end MyApp.eager_load!

Slide 69

Slide 69 text

config.eager_load_namespaces << MyApp

Slide 70

Slide 70 text

Mutex

Slide 71

Slide 71 text

hash = {} 10.times do |i| Thread.new { hash[i] = "Hello #{rand}" } end

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

“There are heaps of non thread-safe usage of Hashes in Rails and the surrounding gems”

Slide 74

Slide 74 text

Using hashes as a cache is an extremely common pattern in Ruby

Slide 75

Slide 75 text

@cached[template] = File.read(template) Template lookup

Slide 76

Slide 76 text

@controller_names[“posts”] = PostsController Router

Slide 77

Slide 77 text

Helper def presenter_for(model) "#{model.class.name}Presenter". constantize.new(model) end presenter_for(Post.new) #=> PostPresenter<@post=...>

Slide 78

Slide 78 text

@@p = Hash.new do |h, k| h[k] = "#{k}Presenter".constantize end def presenter_for(model) @@p[model.class.name].new(model) end

Slide 79

Slide 79 text

hash = {} 10.times do |i| Thread.new { hash[i] = "Hello #{rand}" } end

Slide 80

Slide 80 text

hash = {} mutex = Mutex.new 10.times do |i| Thread.new { mutex.synchronize { hash[i] = "Hello #{rand}" } } end

Slide 81

Slide 81 text

Hash C2 C3 C4 C1

Slide 82

Slide 82 text

Hash C2 C3 C4 C1

Slide 83

Slide 83 text

Hash C2 C3 C4 C1

Slide 84

Slide 84 text

Hash C2 C3 C4 C1

Slide 85

Slide 85 text

Hash C2 C3 C4 C1

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Thread safe gem https://github.com/headius/thread_safe

Slide 91

Slide 91 text

ThreadSafe::Hash.new ThreadSafe::Array.new

Slide 92

Slide 92 text

Better tools of the trade

Slide 93

Slide 93 text

Queues

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

live_assets#sse Manager SSE Subscriber live_assets#sse SSE live_assets#sse SSE Subscriber app/assets vendor/assets lib/assets Operating System Notifications

Slide 96

Slide 96 text

class Manager def initialize @subscribers = [] end def subscribe(s) @subscribers << s end

Slide 97

Slide 97 text

def unsubscribe(s) @subscribers.delete(s) end def notify(event) @subscribers.each do |s| s << event end end end

Slide 98

Slide 98 text

live_assets#sse Manager SSE Subscriber live_assets#sse SSE live_assets#sse SSE Subscriber app/assets vendor/assets lib/assets Operating System Notifications

Slide 99

Slide 99 text

class Manager attr_reader :queue def initialize @subscribers = [] @queue = Queue.new end def loop while message = @queue.pop event, *args = message send(event, *args) end end

Slide 100

Slide 100 text

manager = Manager.new Thread.new { manager.loop } manager.queue << [:notify, :reloadCSS]

Slide 101

Slide 101 text

C2 C3 C4 C1

Slide 102

Slide 102 text

C2 C3 C4 C1

Slide 103

Slide 103 text

C2 C3 C4 C1

Slide 104

Slide 104 text

We need abstractions that can move us away from shared mutable state

Slide 105

Slide 105 text

github.com/celluloid

Slide 106

Slide 106 text

Conclusion

Slide 107

Slide 107 text

We have learned about different tools to tackle thread safety issues

Slide 108

Slide 108 text

thread.rb • Thread • Mutex • ConditionVariable • Queue • SizedQueue

Slide 109

Slide 109 text

Gems • atomic • thread_safe

Slide 110

Slide 110 text

The different semantics existing in different implementations makes concurrency in Ruby harder

Slide 111

Slide 111 text

We need to push for better semantics at the same time we change how we think about concurrency

Slide 112

Slide 112 text

Thank you! @josevalim / Plataformatec