Servers with Event Machine - David Troy - RailsConf 2011
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Servers with Event Machine - David Troy - RailsConf 2011

  • 3,093 views
Uploaded on

Building Scalable, Stateful Servers with Rails

Building Scalable, Stateful Servers with Rails

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,093
On Slideshare
3,082
From Embeds
11
Number of Embeds
3

Actions

Shares
Downloads
31
Comments
0
Likes
6

Embeds 11

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

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

  • 1. Building Scalable ServersWith EventMachine and Rails Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy
  • 2. EventMachine
  • 3. EventMachine DRY
  • 4. EventMachine DRY DRAG
  • 5. EventMachine DRY DRAG(Don’t Repeat Aman Gupta)
  • 6. require ‘aman_gupta’
  • 7. EventMachinescalable non-blocking i/o in ruby
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. Receiving Email Using ActionMailer port 25 postfix (smtp) (spool) fork process Action pgsql Mailer ActiveRecord (db)
  • 18. Receiving Email Using LMTP port 25 postfix (smtp) (spool) port 8081 (lmtp) EM pgsql (w/Rails) ActiveRecord (db)
  • 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. LMTP Daemonhttp://tools.ietf.org/html/rfc2033
  • 21. POP3 Daemonhttp://tools.ietf.org/html/rfc1939
  • 22. IMAP Daemonhttp://tools.ietf.org/html/rfc3501
  • 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. replyz.comDoes Anybody Know...?mailstrom.comailstrom.410labs.comPower Tools for Your Inboxshortmail.comEmail, simplified.
  • 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. 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. Dave Troy Chief Executive Officer davetroy@shortmail.com @davetroy facebook.com/davetroy Tomorrow @ BohConf!http://emrubyconf.com/