Rhebok, High Performance Rack Handler / Rubykaigi 2015

Rhebok,
High performance Rack Handler
Masahiro Nagano @kazeburo
RubyKaigi 2015
Me
•Masahiro Nagano
•@kazeburo
•Principal Site Reliability Engineer
at Mercari, Inc.
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Mercari
•Download: 27M (JP+US)
•GMV: Several Billion per a Month
•Items: Several hundreds of thousand
or more new items in a Day
•Backend language: PHP, Go, lua, etc
Agenda
•Rhebok Overview and Benchmark
•How to create a High Performance
Rack Handler & Rhebok internals
Rhebok
Overview and Benchmark
Rhebok
• Rack Handler/Web Server
• 1.5x-2x performance when compared to
Unicorn
• Prefork Architecture same as Unicorn
• Rhebok is suitable for running HTTP application
servers behind a reverse proxy like nginx
• Ruby port of Perl’s Gazelle
What’s Gazelle?
• High Performance Plack Handler
• Plack is Perl’s Rack
• 2x~3x times faster than servers
commonly used like Starman, Starlet
• Production Ready
• Installed to dozen servers and has shown to
reduce their CPU usage by 1-3%
https://www.flickr.com/photos/rohit_saxena/9819136626/
https://www.flickr.com/photos/wildlifewanderer/8176461065/
Who should use Rhebok?
•A Highly optimized high traffic
websites
• Gaming, Ad-tec, Recipe Site, Media or
massive scale SNS
• By using Rhebok, it is possible to improve
the response speed to higher level
•Can be applied to any website
general website
optimized website
SQLCacheWAFRack
Handler Ruby
SQLCacheRubyWAFRack
Handler
% in response time
Who should not use
Rhebok?
•Who want to use WebSocket or
Streaming
•Who can not setup the reverse proxy
in front of Rhebok
Rhebok Spec
•HTTP/1.1 Web Server
•Support full HTTP/1.1 features except
for KeepAlive
•Support TCP and Unix Domain Socket
•Hot Deployment using start_server
•OobGC
Usage
$ rackup -s Rhebok 
--port 8080 
-E production
-O MaxWorkers=20 
-O MaxRequestPerChild=1000 
-O OobGC=yes 
config.ru
Recommended
configuration
RhebokAmazon Web Services LLC or its affiliates. All rights reserved.
Client Multimedia Corporate
data center
Traditional
server
Mobile Client
IAM Add-on Example:
IAM Add-on
Assignment/
Task
RequesterWorkers
Reverse Proxy
(Nginx,h2o)
HTTP/2
HTTP/1.1
TCP
Unix Domain Socket
http {
listen 443 ssl http2;
upstream app {
server unix:/path/to/app.sock;
}
server {
location / {
proxy_pass http://app;
}
location ~ ^/assets/ {
root /path/to/webapp/assets;
}
}
}
Hot Deploy
$ start_server --port 8080 
-- rackup -s Rhebok 
-E production
-O MaxWorkers=20 
-O MaxRequestPerChild=1000 
-O OobGC=yes 
config.ru
perl: https://metacpan.org/release/Server-Starter
golang: https://github.com/lestrrat/go-server-starter
start_server
How works start_server
start_server --port 8080 -- rackup
Rhebok
worker worker worker
socket
fork
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
RequesterWorkersAmazon
Mechanical Turk
Non-Service Specific
Socket.for_fd(
ENV["SERVER_STARTER_PORT"]
)
How works start_server
start_server --port 8080 -- rackup
Rhebok
worker worker worker
socket
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
RequesterWorkersAmazon
Mechanical Turk
Non-Service Specific
SIGHUP
Rhebok
worker worker worker
fork
Socket.for_fd(
ENV["SERVER_STARTER_PORT"]
)
How works start_server
start_server --port 8080 -- rackup
Rhebok
worker worker worker
socket
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
RequesterWorkersAmazon
Mechanical Turk
Non-Service Specific
SIGHUP
Rhebok
worker worker worker
SIGTERM
Benchmark
Benchmark environment
• Amazon EC2 c3.8xlarge
• 32 vcpu
• Amazon Linux
• Ruby 2.2.3
• Unicorn 5.0.0 / rhebok 0.9.0
• patched wrk that supports unix domain socket
• https://github.com/kazeburo/wrk/tree/unixdomain2
Benchmark
HelloWorld sinatra rails
5577
30788
248094
6151
34557
398898
req/sec
Rhebok
unicorn
ISUCON benchmark
• ISUCON
• web application tuning contest
• Contestants compete with the scores of
benchmark created by organizers
• Web application that becomes the theme
of ISUCON is close to the service it is in
reality
ISUCON 4 Qualifier
43560
41175
SCORE
unicorn Rhebok
How to create
a high performance Rack Handler
and Rhebok internals
Basics of
Rack and Rack Handler
Rack
•Rack is specification
• interface between webservers that support
ruby and ruby web frameworks
•Rack also is implementation
• eg. Rack::Request, Response and
Middlewares
web server interface
unicorn
thin
puma
Rack
Web
interface
Rails
sinatra
Padrino
Web Server Framework
Rack Application
app = Proc.new do |env|
[
'200',
{'Content-Type' => 'text/html'},
['Hello']
]
end
Rack env hash
•Hash object contains Request Data
•CGI keys
• REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
QUERY_STRING, HTTP_Variables
•Rack specific keys
• rack.version, rack.url_scheme, rack.input, rack.errors,
rack.multithread, rack.multiprocess, rack.run_once,rack.hijack?
Response Array
[
'200',
{
'Content-Type' => 'text/html',
‘X-Content-Type-Options’ => ‘nosniff’,
‘X-Frame-Options’ => ‘SAMEORIGIN’,
‘X-XSS-Protection’ => ‘1; mode=block’
},
['Hello',‘world’]
]
Response body
•Response body must respond to each
• Array of strings
• Application instance
• File like object
Role of Rack Handler
•Create env from an HTTP request sent
from a client
•Call an application
•Create an HTTP response from array
and send back to the client
env app array
HTTP req HTTP res
Create a Rack
Handler
module Rack
module Handler
class Shika
def self.run(app, options)
slf = new()
slf.run_server(app)
end
def run_server(app)
server = TCPServer.new('0.0.0.0', 8080)
while true
conn = server.accept
buf = ""
while true
buf << conn.sysread(4096)
break if buf[-4,4] == "rnrn"
end
reqs = buf.split("rn")
req = reqs.shift.split
env = {
'REQUEST_METHOD' => req[0],
'SCRIPT_NAME' => '',
'PATH_INFO' => req[1],
'QUERY_STRING' => req[1].split('?').last,
'SERVER_NAME' => '0.0.0.0',
'SERVER_PORT' => '5000',
'rack.version' => [0,1],
'rack.input' => StringIO.new('').set_encoding('BINARY'),
'rack.errors' => STDERR,
'rack.multithread' => false,
'rack.multiprocess' => false,
'rack.run_once' => false,
'rack.url_scheme' => 'http'
}
reqs.each do |header|
header = header.split(": ")
env["HTTP_"+header[0].upcase.gsub('-','_')] = header[1];
end
status, headers, body = app.call(env)
res_header = "HTTP/1.0 "+status.to_s+"
res_header << "+Rack::Utils::HTTP_STATUS_CODES[status]+"rn"
headers.each do |k, v|
res_header << "#{k}: #{v}rn"
end
res_header << "Connection: closernrn"
conn.write(res_header)
body.each do |chunk|
conn.write(chunk)
end
conn.close
end
end
end
end
create socket
accept
read request &
create env
run app
create
response
Run server
$ rackup -r ./shika.rb -s Shika -E production config.ru
This rack handler has
some problems
• Performance problem
• Handle only one request at once
• Stop the whole world when one request
lagged
• No TIMEOUT
• No HTTP request parser support HTTP/
1.1 spec
Increase concurrency
• Multi process
• simple and easy to scale
• Multi thread
• lightweight context switch compared to the
process
• IO Multiplexing
• Event driven, can handle many connections
Concurrency strategy
• Unicorn
• -> multi process
• PUMA
• -> multi thread + limited event model
(+ multi process)
• Thin
• event model (+ multi process)
Manager
Prefork Architecture
Manager
bind
listen
Prefork Architecture
Worker
accept
Worker
accept
Worker
accept
Worker
accept
Manager
bind
listen
fork fork fork fork
Prefork Architecture
Worker
accept
Worker
accept
Worker
accept
Worker
accept
Manager
bind
listen
fork fork fork fork
Client Client ClientClient
Prefork Architecture
prefork_engine
•https://github.com/kazeburo/
prefork_engine
•Ruby port of Perl’s Parallel::Prefork
•a simple prefork server framework
prefork_engine
server = TCPServer.new('0.0.0.0', 8080)
pe = PreforkEngine.new({
"max_workers" => 5,
"trap_signals" => {
"TERM" => 'TERM',
"HUP" => 'TERM',
},
})
while !pe.signal_received.match(/^TERM$/)
pe.start { # child
while true
conn = server.accept
....
end
}
end
pe.wait_all_children
IO timeout
IO timeout
•Unicorn does not have io timeout
• send SIGKILL to a long running process
• default timeout 30 sec
E, [2015-12-08T03:13:24.863287 #90217] ERROR -- : worker=0 PID:
90243 timeout (61s > 60s), killing
E, [2015-12-08T03:13:24.865764 #90217] ERROR -- : reaped
#<Process::Status: pid 90243 SIGKILL (signal 9)> worker=0
I, [2015-12-08T03:13:24.866176 #90217] INFO -- : worker=0
spawning...
Using select(2)
while true
connection = @server.accept
buf = self.read_timeout(connection)
if buf == nil
connection.close
next
end
parse_http_header(…)
--
def read_timeout(conn)
if !IO.select([conn],nil,nil,READ_TIMEOUT)
return nil
end
return connection.sysread(4096)
end
Rhebok supports IO timeout
•Implement read_timeout in C
• avoid strange behavior of nonblock +
sysread
• use poll(2) instead of select(2)
$ rackup -s Rhebok -O Timeout=60 config.ru
Parse HTTP request
HTTP parser
• HTTP Parser is easy to cause security issue. It's
safer to choose an existing one that is widely used
• There are several fast implementation
• Mongrel based - Unicorn, PUMA
• Node.js based - Passenger 5
• PicoHTTPParser - Rhebok, h2o
• pico_http_parser in rubygems
• Ruby binding of PicoHTTPParser
pico_http_parser benchmark
0 1 2 4 10
80814
118499
140395153002
167823
109602
166615
203201
231919
455188
# of headers
picohttpparser unicorn
PicoHTTPParser in Rhebok
•uses PicoHTTPParser directly
• does not use pico_http_parser.gem
•performs both of reading and parsing
the HTTP header in a C function
• reduce overhead of create Ruby’s string
contain HTTP header
TCP optimization
TCP_NODELAY
•When data is written, TCP does not
send packets immediately. There are
some delays.
•TCP uses Nagle’s algorithm to collect
small packets in order to send them all
at once by default
•TCP_NODELAY disable it
write(“foo”)
write(“bar”)
os/kernel clientApplication
buffering
“foobar”
Nagle’s algorithm
delay
write(“foo”)
write(“bar”)
os/kernel clientApplication
“foo”
“bar”
TCP_NODELAY
Problem of TCP_NODELAY
• When TCP_NODEALY is enable, take
care of excessive fragmentation of tcp
packet
• causes increase network latency
• To prevent fragmentation
• concat data in application
• use writev(2)
writev(2)
w/o writev(2)
char *buf1 = “Hello ”;
char *buf2 = “RubyKaigi”;
char *buf3 = “rn”;
write(fd, buf1, strlen(buf1));
write(fd, buf2, strlen(buf2));
write(fd, buf3, strlen(buf3));
kernel
User Users Client MultimMobile Client
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
WorkersAmazon
Mechanical Turk
Non-Service Specific
“Hello“ “RubyKaigi” “rn”
many syscalls
w/o writev(2)
char *buf1 = “Hello ”;
char *buf2 = “RubyKaigi”;
char *buf3 = “rn”;
char *buf;
str = (char *)malloc(100);
strcat(buf, buf1);
strcat(buf, buf2);
strcat(buf, buf2);
write(fd, buf, strlen(buf));
free(buf); kernel
“Hello RubyKaigirn”
one syscall
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
WorkersAmazon
Mechanical Turk
Non-Service Specific
allocate memory
writev(2)
ssize_t rv;
char *buf1 = “Hello ”;
char *buf2 = “RubyKaigi”;
char *buf3 = “rn”;
struct iovec v[3];
v[0].io_base = buf1;
v[0].io_len = strlen(buf1);
...
v[2].io_base = buf3;
v[2].io_len = strlen(buf3);
rv = writev(fd, v, 3); kernel
Gathering
buffers
Amazon Mechanical Turk
On-Demand Workforce
Human Intelligence
Tasks (HIT)
Assignment/
Task
WorkersAmazon
Mechanical Turk
Non-Service Specific
“Hello RubyKaigirn”
one syscall
Rhebok internals
•Prefork Architecture
•Effecient network IO
•Ultra Fast HTTP parser
•TCP Optimization
•Implemented C
conclusion
conclusion
•Rhebok is a High Performance Rack
Handler
•Rhebok is built on many modern
technologies
•Please use Rhebok and feedback to
me
end
1 of 67

Recommended

Practical Testing of Ruby Core by
Practical Testing of Ruby CorePractical Testing of Ruby Core
Practical Testing of Ruby CoreHiroshi SHIBATA
16.6K views51 slides
Integrating icinga2 and the HashiCorp suite by
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteBram Vogelaar
86 views39 slides
Big Master Data PHP BLT #1 by
Big Master Data PHP BLT #1Big Master Data PHP BLT #1
Big Master Data PHP BLT #1Masahiro Nagano
39.3K views19 slides
Integrating icinga2 and the HashiCorp suite by
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteBram Vogelaar
534 views46 slides
Creating Reusable Puppet Profiles by
Creating Reusable Puppet ProfilesCreating Reusable Puppet Profiles
Creating Reusable Puppet ProfilesBram Vogelaar
58 views23 slides
Bootstrapping multidc observability stack by
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stackBram Vogelaar
190 views35 slides

More Related Content

What's hot

PSGI and Plack from first principles by
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principlesPerl Careers
2K views42 slides
Ansible fest Presentation slides by
Ansible fest Presentation slidesAnsible fest Presentation slides
Ansible fest Presentation slidesAaron Carey
463 views23 slides
How to build a High Performance PSGI/Plack Server by
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server Masahiro Nagano
18.6K views134 slides
Autoscaling with hashi_corp_nomad by
Autoscaling with hashi_corp_nomadAutoscaling with hashi_corp_nomad
Autoscaling with hashi_corp_nomadBram Vogelaar
219 views33 slides
Nodejs Explained with Examples by
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
112.3K views78 slides

What's hot(20)

PSGI and Plack from first principles by Perl Careers
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
Perl Careers2K views
Ansible fest Presentation slides by Aaron Carey
Ansible fest Presentation slidesAnsible fest Presentation slides
Ansible fest Presentation slides
Aaron Carey463 views
How to build a High Performance PSGI/Plack Server by Masahiro Nagano
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
Masahiro Nagano18.6K views
Autoscaling with hashi_corp_nomad by Bram Vogelaar
Autoscaling with hashi_corp_nomadAutoscaling with hashi_corp_nomad
Autoscaling with hashi_corp_nomad
Bram Vogelaar219 views
Nodejs Explained with Examples by Gabriele Lana
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
Gabriele Lana112.3K views
Observability with Consul Connect by Bram Vogelaar
Observability with Consul ConnectObservability with Consul Connect
Observability with Consul Connect
Bram Vogelaar377 views
Puppet and the HashiCorp Suite by Bram Vogelaar
Puppet and the HashiCorp SuitePuppet and the HashiCorp Suite
Puppet and the HashiCorp Suite
Bram Vogelaar285 views
Securing Prometheus exporters using HashiCorp Vault by Bram Vogelaar
Securing Prometheus exporters using HashiCorp VaultSecuring Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp Vault
Bram Vogelaar397 views
Static Typing in Vault by GlynnForrest
Static Typing in VaultStatic Typing in Vault
Static Typing in Vault
GlynnForrest255 views
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf by Tom Croucher
Using Node.js to  Build Great  Streaming Services - HTML5 Dev ConfUsing Node.js to  Build Great  Streaming Services - HTML5 Dev Conf
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Tom Croucher22.7K views
Matthew Eernisse, NodeJs, .toster {webdev} by .toster
Matthew Eernisse, NodeJs, .toster {webdev}Matthew Eernisse, NodeJs, .toster {webdev}
Matthew Eernisse, NodeJs, .toster {webdev}
.toster1.4K views
How to Begin Developing Ruby Core by Hiroshi SHIBATA
How to Begin Developing Ruby CoreHow to Begin Developing Ruby Core
How to Begin Developing Ruby Core
Hiroshi SHIBATA1.9K views
Hacking ansible by bcoca
Hacking ansibleHacking ansible
Hacking ansible
bcoca12.5K views
Bootstrapping multidc observability stack by Bram Vogelaar
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stack
Bram Vogelaar125 views
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon by Masahiro Nagano
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Masahiro Nagano8.1K views

Viewers also liked

Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT by
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LTGazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LTMasahiro Nagano
39.2K views19 slides
Mackerel & Norikra mackerel meetup #4 LT by
Mackerel & Norikra mackerel meetup #4 LTMackerel & Norikra mackerel meetup #4 LT
Mackerel & Norikra mackerel meetup #4 LTMasahiro Nagano
36.6K views22 slides
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT by
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LTNorikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LTMasahiro Nagano
38.8K views21 slides
ISUCONの勝ち方 YAPC::Asia Tokyo 2015 by
ISUCONの勝ち方 YAPC::Asia Tokyo 2015ISUCONの勝ち方 YAPC::Asia Tokyo 2015
ISUCONの勝ち方 YAPC::Asia Tokyo 2015Masahiro Nagano
54.6K views89 slides
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月 by
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月Masahiro Nagano
42.6K views51 slides
メルカリでのNorikraの活用、 Mackerelを添えて by
メルカリでのNorikraの活用、 Mackerelを添えてメルカリでのNorikraの活用、 Mackerelを添えて
メルカリでのNorikraの活用、 Mackerelを添えてMasahiro Nagano
71K views39 slides

Viewers also liked(19)

Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT by Masahiro Nagano
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LTGazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
Gazelle & CPAN modules for performance. Shibuya.pm Tech Talk #17 LT
Masahiro Nagano39.2K views
Mackerel & Norikra mackerel meetup #4 LT by Masahiro Nagano
Mackerel & Norikra mackerel meetup #4 LTMackerel & Norikra mackerel meetup #4 LT
Mackerel & Norikra mackerel meetup #4 LT
Masahiro Nagano36.6K views
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT by Masahiro Nagano
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LTNorikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
Norikraで作るPHPの例外検知システム YAPC::Asia Tokyo 2015 LT
Masahiro Nagano38.8K views
ISUCONの勝ち方 YAPC::Asia Tokyo 2015 by Masahiro Nagano
ISUCONの勝ち方 YAPC::Asia Tokyo 2015ISUCONの勝ち方 YAPC::Asia Tokyo 2015
ISUCONの勝ち方 YAPC::Asia Tokyo 2015
Masahiro Nagano54.6K views
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月 by Masahiro Nagano
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
メルカリのデータベース戦略 / PHPとMySQLの怖い話 MyNA会2015年8月
Masahiro Nagano42.6K views
メルカリでのNorikraの活用、 Mackerelを添えて by Masahiro Nagano
メルカリでのNorikraの活用、 Mackerelを添えてメルカリでのNorikraの活用、 Mackerelを添えて
メルカリでのNorikraの活用、 Mackerelを添えて
Masahiro Nagano71K views
仮想化専門コンサルタントが教える「成功するエンタープライズクラウド環境構のポイント」 by VirtualTech Japan Inc.
仮想化専門コンサルタントが教える「成功するエンタープライズクラウド環境構のポイント」仮想化専門コンサルタントが教える「成功するエンタープライズクラウド環境構のポイント」
仮想化専門コンサルタントが教える「成功するエンタープライズクラウド環境構のポイント」
飛び道具ではないMetal #iOSDC by Shuichi Tsutsumi
飛び道具ではないMetal #iOSDC飛び道具ではないMetal #iOSDC
飛び道具ではないMetal #iOSDC
Shuichi Tsutsumi8.5K views
[ICLR2017読み会 @ DeNA] ICLR2017紹介 by Takeru Miyato
[ICLR2017読み会 @ DeNA] ICLR2017紹介[ICLR2017読み会 @ DeNA] ICLR2017紹介
[ICLR2017読み会 @ DeNA] ICLR2017紹介
Takeru Miyato26.1K views
言葉のもつ広がりを、モデルの学習に活かそう -one-hot to distribution in language modeling- by Takahiro Kubo
言葉のもつ広がりを、モデルの学習に活かそう -one-hot to distribution in language modeling-言葉のもつ広がりを、モデルの学習に活かそう -one-hot to distribution in language modeling-
言葉のもつ広がりを、モデルの学習に活かそう -one-hot to distribution in language modeling-
Takahiro Kubo37.9K views
ICLR読み会 奥村純 20170617 by Jun Okumura
ICLR読み会 奥村純 20170617ICLR読み会 奥村純 20170617
ICLR読み会 奥村純 20170617
Jun Okumura33.9K views
Semi-Supervised Classification with Graph Convolutional Networks @ICLR2017読み会 by Eiji Sekiya
Semi-Supervised Classification with Graph Convolutional Networks @ICLR2017読み会Semi-Supervised Classification with Graph Convolutional Networks @ICLR2017読み会
Semi-Supervised Classification with Graph Convolutional Networks @ICLR2017読み会
Eiji Sekiya34.1K views
ICLR2017読み会 Data Noising as Smoothing in Neural Network Language Models @Dena by Takanori Nakai
ICLR2017読み会 Data Noising as Smoothing in Neural Network Language Models @DenaICLR2017読み会 Data Noising as Smoothing in Neural Network Language Models @Dena
ICLR2017読み会 Data Noising as Smoothing in Neural Network Language Models @Dena
Takanori Nakai33.5K views

Similar to Rhebok, High Performance Rack Handler / Rubykaigi 2015

Sharding and Load Balancing in Scala - Twitter's Finagle by
Sharding and Load Balancing in Scala - Twitter's FinagleSharding and Load Balancing in Scala - Twitter's Finagle
Sharding and Load Balancing in Scala - Twitter's FinagleGeoff Ballinger
3.8K views11 slides
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition) by
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)Wesley Beary
2K views151 slides
Rack by
RackRack
RackSarah Allen
675 views36 slides
fog or: How I Learned to Stop Worrying and Love the Cloud by
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the CloudWesley Beary
3.9K views165 slides
Writing robust Node.js applications by
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
15.6K views82 slides
Complex Made Simple: Sleep Better with TorqueBox by
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxbobmcwhirter
1.7K views81 slides

Similar to Rhebok, High Performance Rack Handler / Rubykaigi 2015(20)

Sharding and Load Balancing in Scala - Twitter's Finagle by Geoff Ballinger
Sharding and Load Balancing in Scala - Twitter's FinagleSharding and Load Balancing in Scala - Twitter's Finagle
Sharding and Load Balancing in Scala - Twitter's Finagle
Geoff Ballinger3.8K views
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition) by Wesley Beary
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
Wesley Beary2K views
fog or: How I Learned to Stop Worrying and Love the Cloud by Wesley Beary
fog or: How I Learned to Stop Worrying and Love the Cloudfog or: How I Learned to Stop Worrying and Love the Cloud
fog or: How I Learned to Stop Worrying and Love the Cloud
Wesley Beary3.9K views
Writing robust Node.js applications by Tom Croucher
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
Tom Croucher15.6K views
Complex Made Simple: Sleep Better with TorqueBox by bobmcwhirter
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
bobmcwhirter1.7K views
Using Sinatra to Build REST APIs in Ruby by LaunchAny
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
LaunchAny9.5K views
8 Minutes On Rack by danwrong
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
danwrong16.7K views
ELK stack at weibo.com by 琛琳 饶
ELK stack at weibo.comELK stack at weibo.com
ELK stack at weibo.com
琛琳 饶2.9K views
Cutting through the fog of cloud by Kyle Rames
Cutting through the fog of cloudCutting through the fog of cloud
Cutting through the fog of cloud
Kyle Rames1.5K views
A language for the Internet: Why JavaScript and Node.js is right for Internet... by Tom Croucher
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
Tom Croucher9.4K views
Future Decoded - Node.js per sviluppatori .NET by Gianluca Carucci
Future Decoded - Node.js per sviluppatori .NETFuture Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NET
Gianluca Carucci235 views
A language for the Internet: Why JavaScript and Node.js is right for Internet... by Tom Croucher
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
Tom Croucher6.2K views
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and... by NGINX, Inc.
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
NGINX, Inc.336 views
Introduction to Marionette Collective by Puppet
Introduction to Marionette CollectiveIntroduction to Marionette Collective
Introduction to Marionette Collective
Puppet5.5K views
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard by SV Ruby on Rails Meetup
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine YardHow I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
Puppet Performance Profiling by ripienaar
Puppet Performance ProfilingPuppet Performance Profiling
Puppet Performance Profiling
ripienaar2.1K views

More from Masahiro Nagano

Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min by
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/MinAdvanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/MinMasahiro Nagano
48.1K views33 slides
Stream processing in Mercari - Devsumi 2015 autumn LT by
Stream processing in Mercari - Devsumi 2015 autumn LTStream processing in Mercari - Devsumi 2015 autumn LT
Stream processing in Mercari - Devsumi 2015 autumn LTMasahiro Nagano
3K views20 slides
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術 by
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術Masahiro Nagano
18.4K views37 slides
Isucon makers casual talks by
Isucon makers casual talksIsucon makers casual talks
Isucon makers casual talksMasahiro Nagano
3K views25 slides
blogサービスの全文検索の話 - #groonga を囲む夕べ by
blogサービスの全文検索の話 - #groonga を囲む夕べblogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べMasahiro Nagano
10.9K views48 slides
Gazelle - Plack Handler for performance freaks #yokohamapm by
Gazelle - Plack Handler for performance freaks #yokohamapmGazelle - Plack Handler for performance freaks #yokohamapm
Gazelle - Plack Handler for performance freaks #yokohamapmMasahiro Nagano
13.1K views26 slides

More from Masahiro Nagano(20)

Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min by Masahiro Nagano
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/MinAdvanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Advanced nginx in mercari - How to handle over 1,200,000 HTTPS Reqs/Min
Masahiro Nagano48.1K views
Stream processing in Mercari - Devsumi 2015 autumn LT by Masahiro Nagano
Stream processing in Mercari - Devsumi 2015 autumn LTStream processing in Mercari - Devsumi 2015 autumn LT
Stream processing in Mercari - Devsumi 2015 autumn LT
Masahiro Nagano3K views
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術 by Masahiro Nagano
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
ISUCON4 予選問題で(中略)、”my.cnf”に1行だけ足して予選通過ラインを突破するの術
Masahiro Nagano18.4K views
blogサービスの全文検索の話 - #groonga を囲む夕べ by Masahiro Nagano
blogサービスの全文検索の話 - #groonga を囲む夕べblogサービスの全文検索の話 - #groonga を囲む夕べ
blogサービスの全文検索の話 - #groonga を囲む夕べ
Masahiro Nagano10.9K views
Gazelle - Plack Handler for performance freaks #yokohamapm by Masahiro Nagano
Gazelle - Plack Handler for performance freaks #yokohamapmGazelle - Plack Handler for performance freaks #yokohamapm
Gazelle - Plack Handler for performance freaks #yokohamapm
Masahiro Nagano13.1K views
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014 by Masahiro Nagano
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Dockerで遊んでみよっかー YAPC::Asia Tokyo 2014
Masahiro Nagano32.9K views
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT by Masahiro Nagano
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LTWeb Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Web Framework Benchmarksと Perl の現状報告会 YAPC::Asia Tokyo 2014 LT
Masahiro Nagano11.8K views
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版 by Masahiro Nagano
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
ISUCONで学ぶ Webアプリケーションのパフォーマンス向上のコツ 実践編 完全版
Masahiro Nagano55.5K views
Webアプリケーションの パフォーマンス向上のコツ 実践編 by Masahiro Nagano
 Webアプリケーションの パフォーマンス向上のコツ 実践編 Webアプリケーションの パフォーマンス向上のコツ 実践編
Webアプリケーションの パフォーマンス向上のコツ 実践編
Masahiro Nagano40.4K views
Webアプリケーションの パフォーマンス向上のコツ 概要編 by Masahiro Nagano
 Webアプリケーションの パフォーマンス向上のコツ 概要編 Webアプリケーションの パフォーマンス向上のコツ 概要編
Webアプリケーションの パフォーマンス向上のコツ 概要編
Masahiro Nagano36.5K views
Webアプリケーションとメモリ by Masahiro Nagano
WebアプリケーションとメモリWebアプリケーションとメモリ
Webアプリケーションとメモリ
Masahiro Nagano13.9K views
最近作ったN個のCPANモジュール Yokohama.pm #10 by Masahiro Nagano
最近作ったN個のCPANモジュール Yokohama.pm #10最近作ったN個のCPANモジュール Yokohama.pm #10
最近作ったN個のCPANモジュール Yokohama.pm #10
Masahiro Nagano2.7K views
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題 by Masahiro Nagano
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
『How to build a High Performance PSGI/Plack Server』のその後と ISUCON3を受けての話題
Masahiro Nagano8.6K views
1台から500台までのMySQL運用(YAPC::Asia編) by Masahiro Nagano
1台から500台までのMySQL運用(YAPC::Asia編)1台から500台までのMySQL運用(YAPC::Asia編)
1台から500台までのMySQL運用(YAPC::Asia編)
Masahiro Nagano11.3K views
グラフで捗る話#2 kansai.pm#14 by Masahiro Nagano
グラフで捗る話#2 kansai.pm#14グラフで捗る話#2 kansai.pm#14
グラフで捗る話#2 kansai.pm#14
Masahiro Nagano1.6K views
Web Operations and Perl kansai.pm#14 by Masahiro Nagano
Web Operations and Perl kansai.pm#14Web Operations and Perl kansai.pm#14
Web Operations and Perl kansai.pm#14
Masahiro Nagano2.1K views

Recently uploaded

1st parposal presentation.pptx by
1st parposal presentation.pptx1st parposal presentation.pptx
1st parposal presentation.pptxi238212
9 views3 slides
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院IttrainingIttraining
34 views8 slides
6g - REPORT.pdf by
6g - REPORT.pdf6g - REPORT.pdf
6g - REPORT.pdfLiveplex
9 views23 slides
DALI Basics Course 2023 by
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023Ivory Egg
14 views12 slides
Java Platform Approach 1.0 - Picnic Meetup by
Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic MeetupRick Ossendrijver
25 views39 slides
Roadmap to Become Experts.pptx by
Roadmap to Become Experts.pptxRoadmap to Become Experts.pptx
Roadmap to Become Experts.pptxdscwidyatamanew
11 views45 slides

Recently uploaded(20)

1st parposal presentation.pptx by i238212
1st parposal presentation.pptx1st parposal presentation.pptx
1st parposal presentation.pptx
i2382129 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
6g - REPORT.pdf by Liveplex
6g - REPORT.pdf6g - REPORT.pdf
6g - REPORT.pdf
Liveplex9 views
DALI Basics Course 2023 by Ivory Egg
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023
Ivory Egg14 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab15 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 views
Unit 1_Lecture 2_Physical Design of IoT.pdf by StephenTec
Unit 1_Lecture 2_Physical Design of IoT.pdfUnit 1_Lecture 2_Physical Design of IoT.pdf
Unit 1_Lecture 2_Physical Design of IoT.pdf
StephenTec11 views
From chaos to control: Managing migrations and Microsoft 365 with ShareGate! by sammart93
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
sammart939 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
Data-centric AI and the convergence of data and model engineering: opportunit... by Paolo Missier
Data-centric AI and the convergence of data and model engineering:opportunit...Data-centric AI and the convergence of data and model engineering:opportunit...
Data-centric AI and the convergence of data and model engineering: opportunit...
Paolo Missier34 views
Lilypad @ Labweek, Istanbul, 2023.pdf by Ally339821
Lilypad @ Labweek, Istanbul, 2023.pdfLilypad @ Labweek, Istanbul, 2023.pdf
Lilypad @ Labweek, Istanbul, 2023.pdf
Ally3398219 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson33 views

Rhebok, High Performance Rack Handler / Rubykaigi 2015

  • 1. Rhebok, High performance Rack Handler Masahiro Nagano @kazeburo RubyKaigi 2015
  • 2. Me •Masahiro Nagano •@kazeburo •Principal Site Reliability Engineer at Mercari, Inc.
  • 4. Mercari •Download: 27M (JP+US) •GMV: Several Billion per a Month •Items: Several hundreds of thousand or more new items in a Day •Backend language: PHP, Go, lua, etc
  • 5. Agenda •Rhebok Overview and Benchmark •How to create a High Performance Rack Handler & Rhebok internals
  • 7. Rhebok • Rack Handler/Web Server • 1.5x-2x performance when compared to Unicorn • Prefork Architecture same as Unicorn • Rhebok is suitable for running HTTP application servers behind a reverse proxy like nginx • Ruby port of Perl’s Gazelle
  • 8. What’s Gazelle? • High Performance Plack Handler • Plack is Perl’s Rack • 2x~3x times faster than servers commonly used like Starman, Starlet • Production Ready • Installed to dozen servers and has shown to reduce their CPU usage by 1-3%
  • 10. Who should use Rhebok? •A Highly optimized high traffic websites • Gaming, Ad-tec, Recipe Site, Media or massive scale SNS • By using Rhebok, it is possible to improve the response speed to higher level •Can be applied to any website
  • 11. general website optimized website SQLCacheWAFRack Handler Ruby SQLCacheRubyWAFRack Handler % in response time
  • 12. Who should not use Rhebok? •Who want to use WebSocket or Streaming •Who can not setup the reverse proxy in front of Rhebok
  • 13. Rhebok Spec •HTTP/1.1 Web Server •Support full HTTP/1.1 features except for KeepAlive •Support TCP and Unix Domain Socket •Hot Deployment using start_server •OobGC
  • 14. Usage $ rackup -s Rhebok --port 8080 -E production -O MaxWorkers=20 -O MaxRequestPerChild=1000 -O OobGC=yes config.ru
  • 15. Recommended configuration RhebokAmazon Web Services LLC or its affiliates. All rights reserved. Client Multimedia Corporate data center Traditional server Mobile Client IAM Add-on Example: IAM Add-on Assignment/ Task RequesterWorkers Reverse Proxy (Nginx,h2o) HTTP/2 HTTP/1.1 TCP Unix Domain Socket http { listen 443 ssl http2; upstream app { server unix:/path/to/app.sock; } server { location / { proxy_pass http://app; } location ~ ^/assets/ { root /path/to/webapp/assets; } } }
  • 17. $ start_server --port 8080 -- rackup -s Rhebok -E production -O MaxWorkers=20 -O MaxRequestPerChild=1000 -O OobGC=yes config.ru perl: https://metacpan.org/release/Server-Starter golang: https://github.com/lestrrat/go-server-starter start_server
  • 18. How works start_server start_server --port 8080 -- rackup Rhebok worker worker worker socket fork Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task RequesterWorkersAmazon Mechanical Turk Non-Service Specific Socket.for_fd( ENV["SERVER_STARTER_PORT"] )
  • 19. How works start_server start_server --port 8080 -- rackup Rhebok worker worker worker socket Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task RequesterWorkersAmazon Mechanical Turk Non-Service Specific SIGHUP Rhebok worker worker worker fork Socket.for_fd( ENV["SERVER_STARTER_PORT"] )
  • 20. How works start_server start_server --port 8080 -- rackup Rhebok worker worker worker socket Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task RequesterWorkersAmazon Mechanical Turk Non-Service Specific SIGHUP Rhebok worker worker worker SIGTERM
  • 22. Benchmark environment • Amazon EC2 c3.8xlarge • 32 vcpu • Amazon Linux • Ruby 2.2.3 • Unicorn 5.0.0 / rhebok 0.9.0 • patched wrk that supports unix domain socket • https://github.com/kazeburo/wrk/tree/unixdomain2
  • 24. ISUCON benchmark • ISUCON • web application tuning contest • Contestants compete with the scores of benchmark created by organizers • Web application that becomes the theme of ISUCON is close to the service it is in reality
  • 26. How to create a high performance Rack Handler and Rhebok internals
  • 27. Basics of Rack and Rack Handler
  • 28. Rack •Rack is specification • interface between webservers that support ruby and ruby web frameworks •Rack also is implementation • eg. Rack::Request, Response and Middlewares
  • 30. Rack Application app = Proc.new do |env| [ '200', {'Content-Type' => 'text/html'}, ['Hello'] ] end
  • 31. Rack env hash •Hash object contains Request Data •CGI keys • REQUEST_METHOD, SCRIPT_NAME, PATH_INFO, QUERY_STRING, HTTP_Variables •Rack specific keys • rack.version, rack.url_scheme, rack.input, rack.errors, rack.multithread, rack.multiprocess, rack.run_once,rack.hijack?
  • 32. Response Array [ '200', { 'Content-Type' => 'text/html', ‘X-Content-Type-Options’ => ‘nosniff’, ‘X-Frame-Options’ => ‘SAMEORIGIN’, ‘X-XSS-Protection’ => ‘1; mode=block’ }, ['Hello',‘world’] ]
  • 33. Response body •Response body must respond to each • Array of strings • Application instance • File like object
  • 34. Role of Rack Handler •Create env from an HTTP request sent from a client •Call an application •Create an HTTP response from array and send back to the client env app array HTTP req HTTP res
  • 36. module Rack module Handler class Shika def self.run(app, options) slf = new() slf.run_server(app) end def run_server(app) server = TCPServer.new('0.0.0.0', 8080) while true conn = server.accept buf = "" while true buf << conn.sysread(4096) break if buf[-4,4] == "rnrn" end reqs = buf.split("rn") req = reqs.shift.split env = { 'REQUEST_METHOD' => req[0], 'SCRIPT_NAME' => '', 'PATH_INFO' => req[1], 'QUERY_STRING' => req[1].split('?').last, 'SERVER_NAME' => '0.0.0.0', 'SERVER_PORT' => '5000', 'rack.version' => [0,1], 'rack.input' => StringIO.new('').set_encoding('BINARY'), 'rack.errors' => STDERR, 'rack.multithread' => false, 'rack.multiprocess' => false, 'rack.run_once' => false, 'rack.url_scheme' => 'http' } reqs.each do |header| header = header.split(": ") env["HTTP_"+header[0].upcase.gsub('-','_')] = header[1]; end status, headers, body = app.call(env) res_header = "HTTP/1.0 "+status.to_s+" res_header << "+Rack::Utils::HTTP_STATUS_CODES[status]+"rn" headers.each do |k, v| res_header << "#{k}: #{v}rn" end res_header << "Connection: closernrn" conn.write(res_header) body.each do |chunk| conn.write(chunk) end conn.close end end end end create socket accept read request & create env run app create response
  • 37. Run server $ rackup -r ./shika.rb -s Shika -E production config.ru
  • 38. This rack handler has some problems • Performance problem • Handle only one request at once • Stop the whole world when one request lagged • No TIMEOUT • No HTTP request parser support HTTP/ 1.1 spec
  • 39. Increase concurrency • Multi process • simple and easy to scale • Multi thread • lightweight context switch compared to the process • IO Multiplexing • Event driven, can handle many connections
  • 40. Concurrency strategy • Unicorn • -> multi process • PUMA • -> multi thread + limited event model (+ multi process) • Thin • event model (+ multi process)
  • 45. prefork_engine •https://github.com/kazeburo/ prefork_engine •Ruby port of Perl’s Parallel::Prefork •a simple prefork server framework
  • 46. prefork_engine server = TCPServer.new('0.0.0.0', 8080) pe = PreforkEngine.new({ "max_workers" => 5, "trap_signals" => { "TERM" => 'TERM', "HUP" => 'TERM', }, }) while !pe.signal_received.match(/^TERM$/) pe.start { # child while true conn = server.accept .... end } end pe.wait_all_children
  • 48. IO timeout •Unicorn does not have io timeout • send SIGKILL to a long running process • default timeout 30 sec E, [2015-12-08T03:13:24.863287 #90217] ERROR -- : worker=0 PID: 90243 timeout (61s > 60s), killing E, [2015-12-08T03:13:24.865764 #90217] ERROR -- : reaped #<Process::Status: pid 90243 SIGKILL (signal 9)> worker=0 I, [2015-12-08T03:13:24.866176 #90217] INFO -- : worker=0 spawning...
  • 49. Using select(2) while true connection = @server.accept buf = self.read_timeout(connection) if buf == nil connection.close next end parse_http_header(…) -- def read_timeout(conn) if !IO.select([conn],nil,nil,READ_TIMEOUT) return nil end return connection.sysread(4096) end
  • 50. Rhebok supports IO timeout •Implement read_timeout in C • avoid strange behavior of nonblock + sysread • use poll(2) instead of select(2) $ rackup -s Rhebok -O Timeout=60 config.ru
  • 52. HTTP parser • HTTP Parser is easy to cause security issue. It's safer to choose an existing one that is widely used • There are several fast implementation • Mongrel based - Unicorn, PUMA • Node.js based - Passenger 5 • PicoHTTPParser - Rhebok, h2o • pico_http_parser in rubygems • Ruby binding of PicoHTTPParser
  • 53. pico_http_parser benchmark 0 1 2 4 10 80814 118499 140395153002 167823 109602 166615 203201 231919 455188 # of headers picohttpparser unicorn
  • 54. PicoHTTPParser in Rhebok •uses PicoHTTPParser directly • does not use pico_http_parser.gem •performs both of reading and parsing the HTTP header in a C function • reduce overhead of create Ruby’s string contain HTTP header
  • 56. TCP_NODELAY •When data is written, TCP does not send packets immediately. There are some delays. •TCP uses Nagle’s algorithm to collect small packets in order to send them all at once by default •TCP_NODELAY disable it
  • 59. Problem of TCP_NODELAY • When TCP_NODEALY is enable, take care of excessive fragmentation of tcp packet • causes increase network latency • To prevent fragmentation • concat data in application • use writev(2)
  • 61. w/o writev(2) char *buf1 = “Hello ”; char *buf2 = “RubyKaigi”; char *buf3 = “rn”; write(fd, buf1, strlen(buf1)); write(fd, buf2, strlen(buf2)); write(fd, buf3, strlen(buf3)); kernel User Users Client MultimMobile Client Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task WorkersAmazon Mechanical Turk Non-Service Specific “Hello“ “RubyKaigi” “rn” many syscalls
  • 62. w/o writev(2) char *buf1 = “Hello ”; char *buf2 = “RubyKaigi”; char *buf3 = “rn”; char *buf; str = (char *)malloc(100); strcat(buf, buf1); strcat(buf, buf2); strcat(buf, buf2); write(fd, buf, strlen(buf)); free(buf); kernel “Hello RubyKaigirn” one syscall Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task WorkersAmazon Mechanical Turk Non-Service Specific allocate memory
  • 63. writev(2) ssize_t rv; char *buf1 = “Hello ”; char *buf2 = “RubyKaigi”; char *buf3 = “rn”; struct iovec v[3]; v[0].io_base = buf1; v[0].io_len = strlen(buf1); ... v[2].io_base = buf3; v[2].io_len = strlen(buf3); rv = writev(fd, v, 3); kernel Gathering buffers Amazon Mechanical Turk On-Demand Workforce Human Intelligence Tasks (HIT) Assignment/ Task WorkersAmazon Mechanical Turk Non-Service Specific “Hello RubyKaigirn” one syscall
  • 64. Rhebok internals •Prefork Architecture •Effecient network IO •Ultra Fast HTTP parser •TCP Optimization •Implemented C
  • 66. conclusion •Rhebok is a High Performance Rack Handler •Rhebok is built on many modern technologies •Please use Rhebok and feedback to me
  • 67. end