SlideShare a Scribd company logo
In the Loop
                                             W
                                                      5                                          R
                                                                                  10
                                           Lone Star
                                       6     Ruby
                               R
                                          Conference 9
                                                                                                          R
                                             2011
                                         7
                                       W          8
                                                                                         W

                                                      Lourens Naudé
            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Agenda
                  System calls
                  File descriptor semantics
                  Blocking, nonblocking and async I/O
                  The Reactor Pattern
                  Patterns and gotchas




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Caveats
                  Can’t run before you walk
                  Patterns are framework agnostic
                  KISS
                  read, write and connect only




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Head count ?
                  Eventmachine (or any client libs)
                  Redis
                  Nginx
                  node.js
                  ZeroMQ




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Reactor Pattern sweet spot ?




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Soft realtime systems where
                     throughput is more
                 important than processing



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
What does that mean ?
                  Lots of in flight I/O
                  Little CPU per request - proxies
                  Improve ON:OFF cpu time
                  NEVER about speed for a single client
                  or request




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
"Upgraded to Thin, my
                                   app's flying!"




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
EDOINGITWRONG




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
To maintain acceptable
       response times for more clients,
            with the same or less
                infrastructure


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Example app

                       Client 1

                         Client 2
                                                                                                            Broker
                         Client 3

                         Client 4

            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
FIX
                  FIX client
                  Financial Information eXchange
                  Compact protocol - encodes a lot of
                  domain info




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
FIX message

 8=FIX.4.49=4535=049=AB56=CD34=352=20000426-12:05:0610=22

       8=FIX.4.4            # FIX version
       9=45                # body length
       35=0                # Heartbeat msg
       49=AB                # sender
       56=CD                # receiver
       34=3                 # sequence number
       52=20000426-12:05:06 # sent at
       10=220               # checksum



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
The kernel


                     Mediates access to system
                            resources :
                      I/O, memory and CPU



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
User mode


                        Sandboxed execution for
                          security and stability
                              requirements



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
How do we access resources ?


                           System calls ( syscalls )

                                 read(5, &buf, 4000)



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Syscalls

       Ruby
      process

                                                                  Kernel                                              disk

       Ruby
      process
                                               read(5, &buf, 4000)
            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Definition


                       A protected function call
                       from user to kernel space
                       with the intent to interact
                        with a system resource


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Characteristics
                  Uniform API on POSIX systems
                  Ditto for behavior
                  LIES !
                  Slow emulations




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Syscall performance
                  MUCH slower than function calls
                  20x and more
                  Definite context switch
                  Calls can block - slow upstream peers




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Who doesn't know what file
                descriptors or file handles
                           are ?



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
open syscall




              fd = open(“/path/file”, O_READ)
              read(fd, &buf, 4000)




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Examples
                  local file
                  socket
                  directory
                  pipe
                  Consistent API, semantics differ




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Definition
                  Numeric handle
                  References a kernel allocated resource
                  Kernel buffer
                  User space buffer




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Blocking I/O


                A request to read a chunk of
                data from a descriptor may
                    block depending on
                        buffer state


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Blocked buffer state



                                                     read(5, &b, 4000)


                                                                                                                      Kernel
         User buffer
                                                                                                                      buffer
                    2000
                                                                                                                      1000



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Nonblocking I/O



fd = socket (PF_INET, SOCK_STREAM, 0);
fcntl (fd, F_SETFL, O_NONBLOCK)




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Nonblocking I/O


                     A read request would only
                      be initiated if the system
                          call won’t block



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
EAGAIN or
                                  EWOULDBLOCK




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Not a silver bullet
                  Guideline only
                  Not free - invokes a syscall
                  Not supported by all devices




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Async I/O myth


                       Nonblocking I/O IS NOT
                          asynchronous I/O




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Async I/O
                  Windows I/O Completion Ports
                  AIO: POSIX Realtime Extension
                  Async I/O often skip double buffering
                  Supports file I/O only




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Recap
                  O_NONBLOCK is a guideline only
                  Invoked in user space
                  Data transfer in user space
                  Blocking and nonblocking I/O terms




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Reactor Pattern


                Remember, the primary use
                case is increased throughput




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Challenges
                  Large number of file descriptors
                  Respond to descriptor state changes
                  Execute domain logic




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
WHILE maintaining acceptable
             response times




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
The Reactor Loop

              while true do
               # shortened for brevity
               add_new_descriptors     # attach new client descriptors
               modify_descriptors      # update descriptor state
               break if terminate?     # conditional loop break
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Registered descriptors
                                          W
                                                      5                                                   R
                                                                                        10


                        R
                                    6                    Reactor                                          9
                                                                                                                      R
                                             7
                                     W                                                8
                                                                                                 W
            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                    Who's familiar with select,
                     poll, epoll or kqueue ?




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O
                  Nonblocking I/O is inefficient
                  Retry on EAGAIN is polling
                  Multiplexed I/O: concurrent blocking
                  Notified of state changes on any
                  registered fd




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O
                                                                                                    fd 5
                                                                                                    fd 6
                                                                                                    fd 7
                                                                                                     fd 8
                               I/O bound
                                  App                                                        Multiplexer

                                                                                                    fd 9
                                                                                                   fd 10
                                                                                                   fd 11
                                                                                                   fd 12


                                 User space                                              Kernel space


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                    1. Tell me when fds 1..200
                     are ready for a read or a
                               write



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                    2. Do other work until one
                         of them's ready




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                               3. Get woken up by the
                                 multiplexer on state
                                       changes



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                         4. Which of fd set 1..200
                            are in a ready state ?




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Multiplexed I/O


                     5. Handle all fds ready for
                                I/O




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Event driven I/O


                    These notifications, or
                    rather state changes in
                   readiness for reading or
                  writing, that's the Events in
                       Event Driven I/O

            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Event handlers / callbacks

              module MyFixApp
               def connection_completed
               end

               def receive_data(buffer)
               end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Callbacks
                  Reactor provides I/O concurrency
                  NO concurrent handling of user
                  code
                  Fire on the reactor thread
                  Runs within the reactor loop




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Ticks
                  A turn through the event loop
                  The reactor can do several thousand
                  ticks per second
                  Time slice for doing work
                  Aim to use minimal CPU per tick




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Best practices, patterns and
                            gotchas




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#1: Coupling Layers

                                                     App handler


                                             Dispatch handler


                                                 I/O Multiplexer


                                                              Kernel

            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Dispatch Handler
                  Handles low level events from the
                  multiplexed I/O framework
                  eg. readiness for read or write
                  Hide complexities from apps
                  Buffering
                  Transfer
                  Conversion to and from Ruby strings



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Dispatch Handler

              class EventableDescriptor
                def read; end             # Tell don't ask interface
                def write; end
                def heartbeat; end
                def select_for_read; end # I/O multiplexer
                def select_for_write; end # agnostic
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Application Handlers
                  Domain layer
                  Higher level events
                  Connection accepted, read,
                  disconnected
                  Encode and decode protocols
                  Perform business logic




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Application Handlers

              class FixConnection
                def receive_data(data); end                                                   # callbacks
                def connection_completed; end
                def unbind; end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Glue

              class FixConnection
                def receive_data(buffer)
                  # Encapsulate excess business logic in callbacks
                  @queue.push *FIX::Parser.parse(buffer)
                end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#2: Testing


                    You don’t need a transport
                           for testing




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Testing

              class FixConnection
                def receive_data(data)
                 @queue << data
                end
              end

              def test_enqueue_on_receive
                conn.receive_data "stub data"
                assert_equal 1, conn.queue.size
              end



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#3: Confusing sync and async


                         Events occur
                   asynchronously, but they're
                     handled synchronously



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
App handler


                                             Dispatch handler


                                                  I/O Multiplexer


                                                              Kernel

            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Defer work to a thread pool

              class FixConnection
                def receive_data(buffer)
                 # enqueue to a thread pool
                 EM.defer{ FIX::Parser.parse(buffer) }
                end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Schedule on the reactor thread

              class FixConnection
                def receive_data(buffer)
                 # allow the reactor to service other clients also
                 EM.schedule{ FIX::Parser.parse(buffer) }
                end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#4: Too much ON cpu time
                  Reactor is single threaded
                  Single core on SMP systems
                  Pegging the core blocks the event loop
                  Time sharing




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Tight loops

              [1..n].each do |i|
               process(i) # expensive work
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Prefer a tick loop

              work = [1..n]
              EM.tick_loop do
                if work.empty?
                  :stop
                else
                  process(work.shift) # expensive work
               end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Slow Protocol parsers


                Protocol parsers incur a cost
                   for both encoding and
                   decoding from buffers



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Negate encoding / decoding costs


                    Prefer a fast C extension /
                    parser to native Ruby code




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#5: Name resolution and connects

              EM.connect("slow-broker.net", 2000, FixConnection)




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Async resolver

              d = EM::DNS::Resolver.resolve "slow-broker.net"
              d.callback do |addrs|
               # connect to broker API when resolved
               EM.connect(addrs.first, 2000, FixConnection)
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Async HTTP APIs

              conn = EM::HttpRequest.new('http://api.a-broker.net')

              req1 = conn.get :path => “x”, :keepalive => true
              req1.callback {
                req2 = conn.get :path => “y”
                req2.callback {
                  # same connection as req1
                }
              }




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#6: Blocking I/O


                         Not all file descriptors
                        support O_NONBLOCK




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Ruby wrapper for libeio

              $ gem install eio

              EIO.open(__FILE__) do |fd|
               EIO.read(fd, 1000) do |buf|
                p buf
                EIO.close(fd)
               end
              end

              http://github.com/methodmissing/eio



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#7: All libs have to be evented

              redis = EM::Protocols::Redis.connect
              redis.errback do |code|
               puts "Error code: #{code}"
              end
              redis.set "a", "foo" do |response|
               redis.get "a" do |response|
                puts response
               end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Evented MySQL

              client = Mysql2::EM::Client.new
              defer = client.query "SELECT sleep(3) as query"
              defer.callback do |result|
                puts "Result: #{result.to_a.inspect}"
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#8: Memory


                           The Garbage Collector
                           could be invoked at any
                             time during request
                                 processing


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Minimize GC pressure


                               Be careful with sloppy
                                allocation patterns




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Connection level proxy
                 module ProxyConnection
                  def initialize(client, request)
                   @client, @request = client, request
                  end

                       def post_init
                        EM::enable_proxy(self, @client)
                       end

                       def connection_completed
                        send_data @request
                       end

                   def proxy_target_unbound
                    close_connection
                 # end
            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
#9: Buffer sizes

              class FixConnection
                def receive_data(inbound)
                 # inbound.size can be a single protocol
                 # message or a batch - stream of data
                end

                def send_data(outbound)
                 # enqueued on a write queue
                end
              end



            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Scheduled parsing

              class FixConnection
                def receive_data(buffer)
                  # buffer packs multiple FIX messages
                  Parser.parse(buffer).each do |msg|
                    EM.next_tick { process(msg) }
                  end
                end
              end




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Takeaways
                  Time sharing for I/O bound services
                  System call overheads and behavior
                  Blocking, nonblocking and async I/O
                  Schedule through ticks
                  OS, dispatch and app layers
                  Transport last, interfaces first




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Questions ?




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Wildfire Interactive, Inc. is hiring




            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011
Thanks !



     http://wildfireapp.com/buzz/jobs
         follow @methodmissing
     fork github.com/methodmissing


            Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929

sábado, 13 de Agosto de 2011

More Related Content

PDF
TX/RX 101: Transfer data efficiently
PPTX
Applied VoIP Security
PDF
Gigamon U - Its Not The Network
PDF
Gigamon U - Missing Link TAP Technology
PDF
Gigamon U - You Will See, Content Monitoring, Alerting, and Forensic Analysis
PDF
OMG DDS Interoperability Demo 2009
PPTX
Career portfolio rough draft
PDF
IPv6 and How It Impacts Communication Applications
TX/RX 101: Transfer data efficiently
Applied VoIP Security
Gigamon U - Its Not The Network
Gigamon U - Missing Link TAP Technology
Gigamon U - You Will See, Content Monitoring, Alerting, and Forensic Analysis
OMG DDS Interoperability Demo 2009
Career portfolio rough draft
IPv6 and How It Impacts Communication Applications

Viewers also liked (8)

PDF
Ai Tornado Grenade Manual
PPT
ddddkkk
PPT
Klima Og Sundhed Jordens Dag 17. Marts
PDF
Payments On Rails
PDF
ZeroMQ as scriptable sockets
PDF
Barcamp PT
PDF
Advanced Fieldcraft
PPT
Qatar Project
Ai Tornado Grenade Manual
ddddkkk
Klima Og Sundhed Jordens Dag 17. Marts
Payments On Rails
ZeroMQ as scriptable sockets
Barcamp PT
Advanced Fieldcraft
Qatar Project
Ad

Similar to In the Loop - Lone Star Ruby Conference (20)

PDF
E2 Labs: ADVANCED PROGRAM ON: THE SECURITY OF A WEBSITE
PDF
Agile Seaside
PDF
Seaside — Agile Software Development
PDF
DevOps Introduction @Cegeka
PDF
State of the Framework Address: Recent Developments in the Metasploit Framework
PDF
2011 June - Singapore GTUG presentation. App Engine program update + intro to Go
PDF
Don Schwarz App Engine Talk
PDF
OpenStack meetup, March2013 keynote
PDF
CandyCaneのご紹介 (2011/10/1版)
PDF
What's this NetKernel Thing Anyway?
PDF
Introduction to the Linux ecosystem
PDF
Seaside Status Message
PDF
Seaside News
PPTX
Ogce Workflow Suite Tg09
PDF
One Man Lightning Talks
PDF
RESTful Services and Distributed OSGi - 04/2009
PDF
Railsconf 2010
PDF
Practical Search in the Cloud - By Marc Krellenstein
PDF
Errors and handling them. YOW nights Sydney 2011
PDF
Error Handling Done Differently
E2 Labs: ADVANCED PROGRAM ON: THE SECURITY OF A WEBSITE
Agile Seaside
Seaside — Agile Software Development
DevOps Introduction @Cegeka
State of the Framework Address: Recent Developments in the Metasploit Framework
2011 June - Singapore GTUG presentation. App Engine program update + intro to Go
Don Schwarz App Engine Talk
OpenStack meetup, March2013 keynote
CandyCaneのご紹介 (2011/10/1版)
What's this NetKernel Thing Anyway?
Introduction to the Linux ecosystem
Seaside Status Message
Seaside News
Ogce Workflow Suite Tg09
One Man Lightning Talks
RESTful Services and Distributed OSGi - 04/2009
Railsconf 2010
Practical Search in the Cloud - By Marc Krellenstein
Errors and handling them. YOW nights Sydney 2011
Error Handling Done Differently
Ad

More from Lourens Naudé (6)

PDF
EuRuKo 2011 - In the Loop
PDF
Event Driven Architecture
ODP
RailswayCon 2010 - Dynamic Language VMs
ODP
RailswayCon 2010 - Command Your Domain
PDF
Railswaycon Inside Matz Ruby
PDF
Embracing Events
EuRuKo 2011 - In the Loop
Event Driven Architecture
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Command Your Domain
Railswaycon Inside Matz Ruby
Embracing Events

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
Teaching material agriculture food technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Programs and apps: productivity, graphics, security and other tools
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...

In the Loop - Lone Star Ruby Conference

  • 1. In the Loop W 5 R 10 Lone Star 6 Ruby R Conference 9 R 2011 7 W 8 W Lourens Naudé Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 2. Agenda System calls File descriptor semantics Blocking, nonblocking and async I/O The Reactor Pattern Patterns and gotchas Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 3. Caveats Can’t run before you walk Patterns are framework agnostic KISS read, write and connect only Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 4. Head count ? Eventmachine (or any client libs) Redis Nginx node.js ZeroMQ Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 5. Reactor Pattern sweet spot ? Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 6. Soft realtime systems where throughput is more important than processing Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 7. What does that mean ? Lots of in flight I/O Little CPU per request - proxies Improve ON:OFF cpu time NEVER about speed for a single client or request Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 8. "Upgraded to Thin, my app's flying!" Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 9. EDOINGITWRONG Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 10. To maintain acceptable response times for more clients, with the same or less infrastructure Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 11. Example app Client 1 Client 2 Broker Client 3 Client 4 Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 12. FIX FIX client Financial Information eXchange Compact protocol - encodes a lot of domain info Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 13. FIX message 8=FIX.4.49=4535=049=AB56=CD34=352=20000426-12:05:0610=22 8=FIX.4.4 # FIX version 9=45 # body length 35=0 # Heartbeat msg 49=AB # sender 56=CD # receiver 34=3 # sequence number 52=20000426-12:05:06 # sent at 10=220 # checksum Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 14. The kernel Mediates access to system resources : I/O, memory and CPU Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 15. User mode Sandboxed execution for security and stability requirements Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 16. How do we access resources ? System calls ( syscalls ) read(5, &buf, 4000) Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 17. Syscalls Ruby process Kernel disk Ruby process read(5, &buf, 4000) Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 18. Definition A protected function call from user to kernel space with the intent to interact with a system resource Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 19. Characteristics Uniform API on POSIX systems Ditto for behavior LIES ! Slow emulations Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 20. Syscall performance MUCH slower than function calls 20x and more Definite context switch Calls can block - slow upstream peers Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 21. Who doesn't know what file descriptors or file handles are ? Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 22. open syscall fd = open(“/path/file”, O_READ) read(fd, &buf, 4000) Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 23. Examples local file socket directory pipe Consistent API, semantics differ Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 24. Definition Numeric handle References a kernel allocated resource Kernel buffer User space buffer Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 25. Blocking I/O A request to read a chunk of data from a descriptor may block depending on buffer state Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 26. Blocked buffer state read(5, &b, 4000) Kernel User buffer buffer 2000 1000 Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 27. Nonblocking I/O fd = socket (PF_INET, SOCK_STREAM, 0); fcntl (fd, F_SETFL, O_NONBLOCK) Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 28. Nonblocking I/O A read request would only be initiated if the system call won’t block Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 29. EAGAIN or EWOULDBLOCK Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 30. Not a silver bullet Guideline only Not free - invokes a syscall Not supported by all devices Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 31. Async I/O myth Nonblocking I/O IS NOT asynchronous I/O Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 32. Async I/O Windows I/O Completion Ports AIO: POSIX Realtime Extension Async I/O often skip double buffering Supports file I/O only Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 33. Recap O_NONBLOCK is a guideline only Invoked in user space Data transfer in user space Blocking and nonblocking I/O terms Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 34. Reactor Pattern Remember, the primary use case is increased throughput Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 35. Challenges Large number of file descriptors Respond to descriptor state changes Execute domain logic Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 36. WHILE maintaining acceptable response times Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 37. The Reactor Loop while true do # shortened for brevity add_new_descriptors # attach new client descriptors modify_descriptors # update descriptor state break if terminate? # conditional loop break end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 38. Registered descriptors W 5 R 10 R 6 Reactor 9 R 7 W 8 W Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 39. Multiplexed I/O Who's familiar with select, poll, epoll or kqueue ? Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 40. Multiplexed I/O Nonblocking I/O is inefficient Retry on EAGAIN is polling Multiplexed I/O: concurrent blocking Notified of state changes on any registered fd Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 41. Multiplexed I/O fd 5 fd 6 fd 7 fd 8 I/O bound App Multiplexer fd 9 fd 10 fd 11 fd 12 User space Kernel space Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 42. Multiplexed I/O 1. Tell me when fds 1..200 are ready for a read or a write Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 43. Multiplexed I/O 2. Do other work until one of them's ready Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 44. Multiplexed I/O 3. Get woken up by the multiplexer on state changes Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 45. Multiplexed I/O 4. Which of fd set 1..200 are in a ready state ? Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 46. Multiplexed I/O 5. Handle all fds ready for I/O Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 47. Event driven I/O These notifications, or rather state changes in readiness for reading or writing, that's the Events in Event Driven I/O Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 48. Event handlers / callbacks module MyFixApp def connection_completed end def receive_data(buffer) end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 49. Callbacks Reactor provides I/O concurrency NO concurrent handling of user code Fire on the reactor thread Runs within the reactor loop Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 50. Ticks A turn through the event loop The reactor can do several thousand ticks per second Time slice for doing work Aim to use minimal CPU per tick Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 51. Best practices, patterns and gotchas Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 52. #1: Coupling Layers App handler Dispatch handler I/O Multiplexer Kernel Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 53. Dispatch Handler Handles low level events from the multiplexed I/O framework eg. readiness for read or write Hide complexities from apps Buffering Transfer Conversion to and from Ruby strings Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 54. Dispatch Handler class EventableDescriptor def read; end # Tell don't ask interface def write; end def heartbeat; end def select_for_read; end # I/O multiplexer def select_for_write; end # agnostic end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 55. Application Handlers Domain layer Higher level events Connection accepted, read, disconnected Encode and decode protocols Perform business logic Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 56. Application Handlers class FixConnection def receive_data(data); end # callbacks def connection_completed; end def unbind; end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 57. Glue class FixConnection def receive_data(buffer) # Encapsulate excess business logic in callbacks @queue.push *FIX::Parser.parse(buffer) end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 58. #2: Testing You don’t need a transport for testing Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 59. Testing class FixConnection def receive_data(data) @queue << data end end def test_enqueue_on_receive conn.receive_data "stub data" assert_equal 1, conn.queue.size end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 60. #3: Confusing sync and async Events occur asynchronously, but they're handled synchronously Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 61. App handler Dispatch handler I/O Multiplexer Kernel Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 62. Defer work to a thread pool class FixConnection def receive_data(buffer) # enqueue to a thread pool EM.defer{ FIX::Parser.parse(buffer) } end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 63. Schedule on the reactor thread class FixConnection def receive_data(buffer) # allow the reactor to service other clients also EM.schedule{ FIX::Parser.parse(buffer) } end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 64. #4: Too much ON cpu time Reactor is single threaded Single core on SMP systems Pegging the core blocks the event loop Time sharing Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 65. Tight loops [1..n].each do |i| process(i) # expensive work end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 66. Prefer a tick loop work = [1..n] EM.tick_loop do if work.empty? :stop else process(work.shift) # expensive work end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 67. Slow Protocol parsers Protocol parsers incur a cost for both encoding and decoding from buffers Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 68. Negate encoding / decoding costs Prefer a fast C extension / parser to native Ruby code Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 69. #5: Name resolution and connects EM.connect("slow-broker.net", 2000, FixConnection) Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 70. Async resolver d = EM::DNS::Resolver.resolve "slow-broker.net" d.callback do |addrs| # connect to broker API when resolved EM.connect(addrs.first, 2000, FixConnection) end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 71. Async HTTP APIs conn = EM::HttpRequest.new('http://api.a-broker.net') req1 = conn.get :path => “x”, :keepalive => true req1.callback { req2 = conn.get :path => “y” req2.callback { # same connection as req1 } } Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 72. #6: Blocking I/O Not all file descriptors support O_NONBLOCK Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 73. Ruby wrapper for libeio $ gem install eio EIO.open(__FILE__) do |fd| EIO.read(fd, 1000) do |buf| p buf EIO.close(fd) end end http://github.com/methodmissing/eio Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 74. #7: All libs have to be evented redis = EM::Protocols::Redis.connect redis.errback do |code| puts "Error code: #{code}" end redis.set "a", "foo" do |response| redis.get "a" do |response| puts response end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 75. Evented MySQL client = Mysql2::EM::Client.new defer = client.query "SELECT sleep(3) as query" defer.callback do |result| puts "Result: #{result.to_a.inspect}" end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 76. #8: Memory The Garbage Collector could be invoked at any time during request processing Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 77. Minimize GC pressure Be careful with sloppy allocation patterns Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 78. Connection level proxy module ProxyConnection def initialize(client, request) @client, @request = client, request end def post_init EM::enable_proxy(self, @client) end def connection_completed send_data @request end def proxy_target_unbound close_connection # end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 79. #9: Buffer sizes class FixConnection def receive_data(inbound) # inbound.size can be a single protocol # message or a batch - stream of data end def send_data(outbound) # enqueued on a write queue end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 80. Scheduled parsing class FixConnection def receive_data(buffer) # buffer packs multiple FIX messages Parser.parse(buffer).each do |msg| EM.next_tick { process(msg) } end end end Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 81. Takeaways Time sharing for I/O bound services System call overheads and behavior Blocking, nonblocking and async I/O Schedule through ticks OS, dispatch and app layers Transport last, interfaces first Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 82. Questions ? Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 83. Wildfire Interactive, Inc. is hiring Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011
  • 84. Thanks ! http://wildfireapp.com/buzz/jobs follow @methodmissing fork github.com/methodmissing Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood City, CA 94063 | (888) 274-0929 sábado, 13 de Agosto de 2011