Building Scalable ServersWith EventMachine and Rails                               Dave Troy                              ...
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 pat...
What is EventMachine?Implementation of the Reactor pattern  similar to Python’s Twisted   also node.js, libevRuby VM Suppo...
What is I/O? Generally Network I/O   mysql query responses   http responses   memcache results Most web applications are I...
What can I use EM for?Scaling I/O heavy applications  handle 5-10k concurrent connections with a single  ruby processAny t...
Simple TCP ServerTCPServer#accept     require socket                     server = TCPServer.new(2202)to accept            ...
require socket inbound                         server = TCPServer.new(2202)               list of clients   clients = []bu...
EM does Non-Blocking I/Ohandles low         module EchoServerlevel sockets         def post_initfor you                 @b...
So, what’s a Reactor?       while reactor_running?         expired_timers.each{ |timer| timer.process }         new_networ...
Writing Asynchronous Codesynchronous ruby code uses return values            ret = operation()            do_something_wit...
Receiving Email Using ActionMailer  port 25        postfix  (smtp)         (spool)                        fork            ...
Receiving Email Using LMTP  port 25       postfix  (smtp)        (spool)                     port 8081                    ...
Receiving Email — Scalably via LMTP    postfix       postfix       message    (spool)       (spool)       systems     EM  ...
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)   ...
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, t...
So, Some Stuff To Remember...  • EventMachine: awesome for I/O, events  • Code is Data, Data is Code  • Often Working with...
Dave Troy                             Chief Executive Officer    davetroy@shortmail.com    @davetroy    facebook.com/davet...
Servers with Event Machine - David Troy - RailsConf 2011
Upcoming SlideShare
Loading in...5
×

Servers with Event Machine - David Troy - RailsConf 2011

2,649

Published on

Building Scalable, Stateful Servers with Rails

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,649
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. Building Scalable ServersWith EventMachine and Rails Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy
    2. 2. EventMachine
    3. 3. EventMachine DRY
    4. 4. EventMachine DRY DRAG
    5. 5. EventMachine DRY DRAG(Don’t Repeat Aman Gupta)
    6. 6. require ‘aman_gupta’
    7. 7. EventMachinescalable non-blocking i/o in ruby
    8. 8. 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
    9. 9. 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
    10. 10. 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
    11. 11. 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
    12. 12. 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
    13. 13. 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
    14. 14. 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
    15. 15. 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)
    16. 16. 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)
    17. 17. Receiving Email Using ActionMailer port 25 postfix (smtp) (spool) fork process Action pgsql Mailer ActiveRecord (db)
    18. 18. Receiving Email Using LMTP port 25 postfix (smtp) (spool) port 8081 (lmtp) EM pgsql (w/Rails) ActiveRecord (db)
    19. 19. Receiving Email — Scalably via LMTP postfix postfix message (spool) (spool) systems EM EM EM EM (w/Rails) (w/Rails) (w/Rails) (w/Rails)
    20. 20. LMTP Daemonhttp://tools.ietf.org/html/rfc2033
    21. 21. POP3 Daemonhttp://tools.ietf.org/html/rfc1939
    22. 22. IMAP Daemonhttp://tools.ietf.org/html/rfc3501
    23. 23. 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]
    24. 24. replyz.comDoes Anybody Know...?mailstrom.comailstrom.410labs.comPower Tools for Your Inboxshortmail.comEmail, simplified.
    25. 25. 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
    26. 26. 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
    27. 27. Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy Tomorrow @ BohConf!http://emrubyconf.com/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×