• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Servers with Event Machine - David Troy - RailsConf 2011
 

Servers with Event Machine - David Troy - RailsConf 2011

on

  • 2,842 views

Building Scalable, Stateful Servers with Rails

Building Scalable, Stateful Servers with Rails

Statistics

Views

Total Views
2,842
Views on SlideShare
2,831
Embed Views
11

Actions

Likes
6
Downloads
30
Comments
0

3 Embeds 11

http://mezura.com.br 5
http://www.linkedin.com 5
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Servers with Event Machine - David Troy - RailsConf 2011 Servers with Event Machine - David Troy - RailsConf 2011 Presentation Transcript

  • Building Scalable ServersWith EventMachine and Rails Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy
  • EventMachine
  • EventMachine DRY
  • EventMachine DRY DRAG
  • EventMachine DRY DRAG(Don’t Repeat Aman Gupta)
  • require ‘aman_gupta’
  • EventMachinescalable non-blocking i/o in ruby
  • About MeAman GuptaSan Francisco, CABeen maintaining EventMachine for 18months (last 4 releases)Ruby Hero 2009amqp, REE patches, perftools.rb, gdb.rb,memprofgithub.com/tmm1@tmm1
  • What is EventMachine?Implementation of the Reactor pattern similar to Python’s Twisted also node.js, libevRuby VM Support Ruby 1.8 (MRI) Ruby 1.9 (YARV) c++ reactor Rubinius JRuby java reactor + simple Pure Ruby version
  • What is I/O? Generally Network I/O mysql query responses http responses memcache results Most web applications are I/O bound, not CPU bound Basic idea behind EM: Instead of waiting on a response from the network, use that time to process other requests
  • What can I use EM for?Scaling I/O heavy applications handle 5-10k concurrent connections with a single ruby processAny type of network I/O http requests sending emails custom tcp proxies data access redis couchdb mysql postgres casandra memcached
  • Simple TCP ServerTCPServer#accept require socket server = TCPServer.new(2202)to accept while client = server.acceptconnections from msg = client.readlinenew clients client.write "You said: #{msg}" client.closeTCPSocket#read* endblocks, so you can require socketonly handle one server = TCPServer.new(2202)client at a time while true Thread.new(server.accept){ |client|Common Solution: msg = client.readline client.write "You said: #{msg}"use a thread per client.closeclient } end
  • require socket inbound server = TCPServer.new(2202) list of clients clients = []buffer per buffers = {} client IO.select returns while true sockets = [server] + clients readable/writable readable, writable = IO.select(sockets) sockets readable.each do |sock| begin if sock == server clients << server.accept_nonblock else client, buf = sock, buffers[sock] ||= read in available data, buf << client.read_nonblock(1024)process if full line received if buf =~ /^.+?r?n/ client.write "You said: #{buf}" Non-Blocking I/O client.close buffers.delete(client) clients.delete(client) end Alternative to end rescue Errno::EAGAIN, Errno::EWOULDBLOCK threads: simply don’t # socket would block, try again later end block end end
  • EM does Non-Blocking I/Ohandles low module EchoServerlevel sockets def post_initfor you @buf = endinbound/ def receive_data(data) @buf << dataoutbound if @buf =~ /^.+?r?n/buffers for send_data "You said: #{@buf}" close_connection_after_writingmaximal endthroughput end endefficient i/o withwritev/readv require eventmachine EM.run do EM.start_server 0.0.0.0, 2202, EchoServerepoll & kqueue endsupport
  • So, what’s a Reactor? while reactor_running? expired_timers.each{ |timer| timer.process } new_network_io.each{ |io| io.process } endreactor is simply a single threaded while loop, calledthe “reactor loop”your code “reacts” to incoming eventsif your event handler takes too long, other eventscannot firelesson: never block the reactor while reactor_running? while true no sleep(1) end no long loops (100_000.times) # reactor is blocked! no blocking I/O (mysql queries) end no polling (while !condition)
  • Writing Asynchronous Codesynchronous ruby code uses return values ret = operation() do_something_with(ret)evented async code uses blocks insteadoperation{ |ret| do_something_with(ret) }different from how you usually use ruby blocks. the blockis stored and invoked later (it’s asynchronous)puts(1) puts(1)1.times{ puts(2) } operation{ puts(3) }puts(3) puts(2)
  • Receiving Email Using ActionMailer port 25 postfix (smtp) (spool) fork process Action pgsql Mailer ActiveRecord (db)
  • Receiving Email Using LMTP port 25 postfix (smtp) (spool) port 8081 (lmtp) EM pgsql (w/Rails) ActiveRecord (db)
  • Receiving Email — Scalably via LMTP postfix postfix message (spool) (spool) systems EM EM EM EM (w/Rails) (w/Rails) (w/Rails) (w/Rails)
  • LMTP Daemonhttp://tools.ietf.org/html/rfc2033
  • POP3 Daemonhttp://tools.ietf.org/html/rfc1939
  • IMAP Daemonhttp://tools.ietf.org/html/rfc3501
  • EventMachineGeo Firehose LISTEN 8192(one connection - Port 80) 250, 500, 1000 Connections twittervision.com/ Apache streamRewriteRule ^/stream(.*)$ http://127.0.0.1:8192%{REQUEST_URI} [P,QSA,L]
  • replyz.comDoes Anybody Know...?mailstrom.comailstrom.410labs.comPower Tools for Your Inboxshortmail.comEmail, simplified.
  • Some Other Handy Stuff... • start_tls: secure any TCP connection • Timers: Periodic, Once, Scheduled • epoll, kqueue, threadpool_size • set_effective_user (listen on protected ports) • teh docs: http://eventmachine.rubyforge.org/EventMachine.html
  • So, Some Stuff To Remember... • EventMachine: awesome for I/O, events • Code is Data, Data is Code • Often Working with State Machines • Don’t peg the CPU. No really, don’t. • TEBWTDI • Testing can be interesting
  • Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy Tomorrow @ BohConf!http://emrubyconf.com/