Slide 1

Slide 1 text

Rails Four & The Future!

Slide 2

Slide 2 text

Rails Four For you and for me!

Slide 3

Slide 3 text

Aaron Patterson @tenderlove

Slide 4

Slide 4 text

HI!! :-)

Slide 5

Slide 5 text

AT&T, AT&T logo and all AT&T related marks are trademarks of AT&T Intellectual Property and/or AT&T affiliated companies.

Slide 6

Slide 6 text

Aloha Ruby Conference

Slide 7

Slide 7 text

SPAM!!!

Slide 8

Slide 8 text

Dog the Bounty Hunter

Slide 9

Slide 9 text

Magnum P.I.

Slide 10

Slide 10 text

Proof!

Slide 11

Slide 11 text

WWFMD

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Gorbachev Puff-Puff Thunderhorse

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

@gorbypuff

Slide 21

Slide 21 text

Rails Four For you and me!!!

Slide 22

Slide 22 text

The Web!

Slide 23

Slide 23 text

Not a TV Psychic

Slide 24

Slide 24 text

Ruby Rails Web

Slide 25

Slide 25 text

Concurrency In Ruby

Slide 26

Slide 26 text

Parallelization

Slide 27

Slide 27 text

P56N (possibly misspelled)

Slide 28

Slide 28 text

GIL Global Interpreter Lock

Slide 29

Slide 29 text

Alternatives: JRuby / Rubinius

Slide 30

Slide 30 text

Good News!

Slide 31

Slide 31 text

GIL was removed! In Ruby 1.9

Slide 32

Slide 32 text

Bad News.

Slide 33

Slide 33 text

GIL was replaced with GVL

Slide 34

Slide 34 text

Is MRI useless? For P32N

Slide 35

Slide 35 text

def fib(n) if n < 3 1 else fib(n-1) + fib(n-2) end end 4.times { fib(34) } Recursive Fib()

Slide 36

Slide 36 text

[aaron@higgins git]$ time ruby test.rb real 0m5.727s user 0m5.706s sys 0m0.011s [aaron@higgins git]$

Slide 37

Slide 37 text

def fib(n) if n < 3 1 else fib(n-1) + fib(n-2) end end 4.times.map { Thread.new { fib(34) } }.each(&:join) Threaded Fib()

Slide 38

Slide 38 text

[aaron@higgins git]$ time ruby test.rb real 0m5.753s user 0m5.732s sys 0m0.014s [aaron@higgins git]$

Slide 39

Slide 39 text

❤ Use JRuby or Rubinius ❤ Use multiple processes What do we do?

Slide 40

Slide 40 text

Slow Server server = WEBrick::GenericServer.new(:Port => 28561, :MaxClients => 4) server.start do |socket| { } until socket.gets == "\r\n" socket.print "HTTP/1.0 200 OK\r\n" socket.print "\r\n" sleep 0.5 socket.print "Hello World" socket.close end

Slide 41

Slide 41 text

Slow Server server = WEBrick::GenericServer.new(:Port => 28561, :MaxClients => 4) server.start do |socket| { } until socket.gets == "\r\n" socket.print "HTTP/1.0 200 OK\r\n" socket.print "\r\n" sleep 0.5 socket.print "Hello World" socket.close end

Slide 42

Slide 42 text

Client require 'net/http' uri = URI('http://localhost:28561') 4.times { puts Net::HTTP.get_response uri }

Slide 43

Slide 43 text

$ time ruby client.rb Hello World Hello World Hello World Hello World real 0m2.029s user 0m0.017s sys 0m0.012s $

Slide 44

Slide 44 text

$ time ruby client.rb Hello World Hello World Hello World Hello World real 0m2.029s user 0m0.017s sys 0m0.012s $ 4 * 0.5

Slide 45

Slide 45 text

require 'net/http' uri = URI('http://localhost:28561') 4.times.map { Thread.new { puts Net::HTTP.get_response(uri).body } }.each &:join Threaded Client

Slide 46

Slide 46 text

$ time ruby client.rb Hello World Hello World Hello World Hello World real 0m0.525s user 0m0.016s sys 0m0.009s $

Slide 47

Slide 47 text

$ time ruby client.rb Hello World Hello World Hello World Hello World real 0m0.525s user 0m0.016s sys 0m0.009s $ 0.525!

Slide 48

Slide 48 text

❤ Release GVL ❤ Wait until data is ready (select()) ❤ Acquire GVL IO#read

Slide 49

Slide 49 text

rb_thread_blocking_region() GVL Magic

Slide 50

Slide 50 text

What does it mean?

Slide 51

Slide 51 text

Fibonacci As A Service

Slide 52

Slide 52 text

FAAS (the next big thing)

Slide 53

Slide 53 text

A Blocked VM is a Blocked VM

Slide 54

Slide 54 text

Threads Matter Even with a GVL

Slide 55

Slide 55 text

Thread Safety In Rails

Slide 56

Slide 56 text

Delete config.threadsafe!

Slide 57

Slide 57 text

Why delete?

Slide 58

Slide 58 text

Always be thread safe!

Slide 59

Slide 59 text

Always be thread safe! My Opinion!

Slide 60

Slide 60 text

Simplify

Slide 61

Slide 61 text

Is it safe to remove?

Slide 62

Slide 62 text

What did it do?

Slide 63

Slide 63 text

threadsafe! ❤ Preload frameworks (enable) ❤ Cache classes (enable) ❤ Dependency loading (disable) ❤ Allow concurrency (enable)

Slide 64

Slide 64 text

Loading Code isn’t Thread-safe*

Slide 65

Slide 65 text

Preload Frameworks EN A B LED

Slide 66

Slide 66 text

Cache classes EN A B LED

Slide 67

Slide 67 text

Dependency Loading D ISA B LED

Slide 68

Slide 68 text

Allow Concurrency EN A B LED

Slide 69

Slide 69 text

Get Lock Read from Socket Process Stuff Write to Socket Release Lock Rack::Lock

Slide 70

Slide 70 text

Thread 2 Get Lock Read from Socket Process Stuff Write to Socket Release Lock Thread 1 Rack::Lock

Slide 71

Slide 71 text

Thread 2 Get Lock Read from Socket Process Stuff Write to Socket Release Lock Rack::Lock

Slide 72

Slide 72 text

Thread 2 Get Lock Read from Socket Process Stuff Write to Socket Release Lock Rack::Lock

Slide 73

Slide 73 text

Rack::Lock with Processes

Slide 74

Slide 74 text

Rack::Lock with Threads

Slide 75

Slide 75 text

Best Case: Extra Overhead

Slide 76

Slide 76 text

Worst Case: 1 req at a time

Slide 77

Slide 77 text

IMPACT

Slide 78

Slide 78 text

Boot time increases (in prod)

Slide 79

Slide 79 text

Fewer middleware

Slide 80

Slide 80 text

Multi-Proc servers stay the same

Slide 81

Slide 81 text

Threaded servers Just Work™

Slide 82

Slide 82 text

Bug Fixes

Slide 83

Slide 83 text

100% Caching

Slide 84

Slide 84 text

Locking ||= def some_method @value ||= some_calculation end

Slide 85

Slide 85 text

check then act Is nil? Calculate Return Set

Slide 86

Slide 86 text

check then act Is nil? Calculate Return Thread 1 Set

Slide 87

Slide 87 text

check then act Is nil? Calculate Return Thread 1 Set Thread 2

Slide 88

Slide 88 text

check then act Is nil? Calculate Return Thread 1 Set Thread 2

Slide 89

Slide 89 text

check then act Is nil? Calculate Return Set

Slide 90

Slide 90 text

Fix #1 (eager init) class Foo class << self def hard_calculation @calc end end @calc = fib(34) end p Foo.hard_calculation

Slide 91

Slide 91 text

Fix #2 (locking) class Foo @lock = Mutex.new class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end end end p Foo.hard_calculation

Slide 92

Slide 92 text

Fix #2 (locking) class Foo @lock = Mutex.new class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end end end p Foo.hard_calculation

Slide 93

Slide 93 text

Fix #2 (locking) class Foo @lock = Mutex.new class << self def hard_calculation @lock.synchronize do @calc ||= fib(34) end end end end p Foo.hard_calculation

Slide 94

Slide 94 text

Move methods to instances

Slide 95

Slide 95 text

Move to object class Foo def initialize @calc = fib(34) end def hard_calculation @calc end end Foo.new.hard_calculation

Slide 96

Slide 96 text

Lazy Object class Foo include Mutex_m def hard_calculation synchronize do @calc ||= fib(34) end end end Foo.new.hard_calculation

Slide 97

Slide 97 text

Maintain API class Foo include Mutex_m def hard_calculation synchronize do @calc ||= fib(34) end end Instance = new def self.hard_calculation Instance.hard_calculation end end Foo.hard_calculation

Slide 98

Slide 98 text

Hash.new { } class Foo def initialize @cache = Hash.new { |h,k| h[k] = [] } end def some_value(key) @cache[key] end end

Slide 99

Slide 99 text

Fix #1 (lock) class Foo include Mutex_m def initialize super @cache = Hash.new { |h,k| h[k] = [] } end def some_value(key) synchronize { @cache[key] } end end

Slide 100

Slide 100 text

Fix #2 thread_safe https://github.com/headius/thread_safe

Slide 101

Slide 101 text

What about the App Level?

Slide 102

Slide 102 text

Thread Safety In Web Apps

Slide 103

Slide 103 text

Avoid shared data

Slide 104

Slide 104 text

Most people don’t type “Thread.new”

Slide 105

Slide 105 text

Look for things that are global.

Slide 106

Slide 106 text

Global Variables $so_global = {} $so_global[:foo] ||= “bar”

Slide 107

Slide 107 text

Constants SET_TWICE = 10 SET_TWICE = 10 ALSO_GLOBAL = {} ALSO_GLOBAL[:foo] ||= “bar” Warning No Warning

Slide 108

Slide 108 text

Class methods class MyModel def self.some_cache @foo ||= fib(34) end end

Slide 109

Slide 109 text

Avoid global data

Slide 110

Slide 110 text

Add locks

Slide 111

Slide 111 text

Streaming

Slide 112

Slide 112 text

Template Rendering Today

Slide 113

Slide 113 text

Templates Results are Buffered

Slide 114

Slide 114 text

Clients are blocked while Rails works

Slide 115

Slide 115 text

The entire page must fit in memory

Slide 116

Slide 116 text

Rack Encourages Buffering class MyApplication def call(env) [200, {}, [‘my page’]] end end

Slide 117

Slide 117 text

We can do I/O and CPU in parallel.

Slide 118

Slide 118 text

So why buffer?

Slide 119

Slide 119 text

ActionController::Live

Slide 120

Slide 120 text

Example class BrowserController < ApplicationController include ActionController::Live def index 100.times do response.stream.write "hello!\n" end response.stream.close end end

Slide 121

Slide 121 text

Example class BrowserController < ApplicationController include ActionController::Live def index 100.times do response.stream.write "hello!\n" end response.stream.close end end Mix in Stream

Slide 122

Slide 122 text

response.stream acts like an I/O object

Slide 123

Slide 123 text

Everything Is a File

Slide 124

Slide 124 text

How does it work?

Slide 125

Slide 125 text

Our API def index response.status = 200 response.headers[‘X-Whatever’] = ‘<3’ response.stream.write ‘hello’ response.stream.write ‘ world’ response.stream.close end

Slide 126

Slide 126 text

Rack API def call(env) return [200, {‘X-Whatever’ => ‘<3’}, [‘hello world’]] end

Slide 127

Slide 127 text

Wrapped Request class Response attr_accessor :status attr_reader :headers, :stream def initialize @status = 200 @headers = {} @stream = StringIO.new end end def call(env) res = Response.new controller.response = res controller.index [res.status, res.headers, res.stream] end

Slide 128

Slide 128 text

Threaded action def call(env) res = Response.new controller.response = res Thread.new { controller.index } [res.status, res.headers, res.stream] end

Slide 129

Slide 129 text

Block until write def call(env) res = Response.new controller.response = res Thread.new { controller.index } res.stream.await [res.status, res.headers, res.stream] end

Slide 130

Slide 130 text

Block until write def call(env) res = Response.new controller.response = res Thread.new { controller.index } res.stream.await [res.status, res.headers, res.stream] end Block

Slide 131

Slide 131 text

Blocking Buffer class Buffer def initialize @latch = Latch.new @buffer = Queue.new end def await # wait for write @latch.await end def write(str) @latch.release @buffer << str end end

Slide 132

Slide 132 text

Blocking Buffer class Buffer def initialize @latch = Latch.new @buffer = Queue.new end def await # wait for write @latch.await end def write(str) @latch.release @buffer << str end end `call` blocks here

Slide 133

Slide 133 text

Blocking Buffer class Buffer def initialize @latch = Latch.new @buffer = Queue.new end def await # wait for write @latch.await end def write(str) @latch.release @buffer << str end end `write` unblocks

Slide 134

Slide 134 text

What can we do?

Slide 135

Slide 135 text

Rails Internals

Slide 136

Slide 136 text

Streaming ERB

Slide 137

Slide 137 text

View Source # encoding: utf-8 require 'erb' doc = ERB.new '<%= hello %> world' puts doc.src

Slide 138

Slide 138 text

Source #coding:UTF-8 _erbout = ''; _erbout.concat(( hello ).to_s); _erbout.concat " world"; _erbout.force_encoding(__ENCODING__)

Slide 139

Slide 139 text

Control Output class MyERB < ERB def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.write" compiler.insert_cmd = "#{eoutvar}.write" compiler.pre_cmd = [] compiler.post_cmd = [] end end doc = MyERB.new '<%= hello %> world', nil, nil, '$stdout' puts doc.src

Slide 140

Slide 140 text

Source #coding:UTF-8 $stdout.write(( hello ).to_s); $stdout.write " world"

Slide 141

Slide 141 text

$ ruby test.rb hello world $

Slide 142

Slide 142 text

Web Apps

Slide 143

Slide 143 text

Infinite Streams

Slide 144

Slide 144 text

Server Sent Events

Slide 145

Slide 145 text

SSE Response HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: text/event-stream Transfer-Encoding: chunked event: ping data: {"ping":"2012-10-06T21:44:41-07:00"} event: reload data: {"changed":["/Users/aaron/git/lolwut/app/views/ users/"]}

Slide 146

Slide 146 text

SSE Response HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: text/event-stream Transfer-Encoding: chunked event: ping data: {"ping":"2012-10-06T21:44:41-07:00"} event: reload data: {"changed":["/Users/aaron/git/lolwut/app/views/ users/"]}

Slide 147

Slide 147 text

SSE Response HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: text/event-stream Transfer-Encoding: chunked event: ping data: {"ping":"2012-10-06T21:44:41-07:00"} event: reload data: {"changed":["/Users/aaron/git/lolwut/app/views/ users/"]}

Slide 148

Slide 148 text

SSE Response HTTP/1.1 200 OK X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff Content-Type: text/event-stream Transfer-Encoding: chunked event: ping data: {"ping":"2012-10-06T21:44:41-07:00"} event: reload data: {"changed":["/Users/aaron/git/lolwut/app/views/ users/"]}

Slide 149

Slide 149 text

Client Side jQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control'); // if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1); });

Slide 150

Slide 150 text

Client Side jQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control'); // if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1); });

Slide 151

Slide 151 text

Client Side jQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control'); // if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1); });

Slide 152

Slide 152 text

Client Side jQuery(document).ready(function() { setTimeout(function() { var source = new EventSource('/control'); // if we get a reload command, reload the page source.addEventListener('reload', function(e) { window.location.reload(); }); }, 1); });

Slide 153

Slide 153 text

Real-Time Browser Communication

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

Puma Browser FS-Events FS Events

Slide 156

Slide 156 text

Puma Browser FS-Events FS Events

Slide 157

Slide 157 text

Puma Browser FS-Events FS Events

Slide 158

Slide 158 text

Puma Browser FS-Events FS Events

Slide 159

Slide 159 text

Puma Browser Console DRB DB Events

Slide 160

Slide 160 text

Puma Browser Console DRB DB Events Socket

Slide 161

Slide 161 text

Other Input Sources ❤ Embedded systems (sausage box) ❤ Telephony (twilio) ❤ Other users (chat systems)

Slide 162

Slide 162 text

Thread Safety P72N Streaming

Slide 163

Slide 163 text

Cores are increasing.

Slide 164

Slide 164 text

We need to utilize the entire machine

Slide 165

Slide 165 text

High latency clients are increasing.

Slide 166

Slide 166 text

Patience is decreasing.

Slide 167

Slide 167 text

Lie. Using cached data

Slide 168

Slide 168 text

Cheat. Updating partial parts of the page

Slide 169

Slide 169 text

Steal. Move computations to client side via JS

Slide 170

Slide 170 text

Lie. Cheat. Steal.

Slide 171

Slide 171 text

Be Good Engineers

Slide 172

Slide 172 text

THANK YOU

Slide 173

Slide 173 text

Questions?