SlideShare ist ein Scribd-Unternehmen logo
1 von 84
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

Ai Tornado Grenade Manual
Ai Tornado Grenade ManualAi Tornado Grenade Manual
Ai Tornado Grenade Manualgutman
 
Klima Og Sundhed Jordens Dag 17. Marts
Klima Og Sundhed Jordens Dag 17. MartsKlima Og Sundhed Jordens Dag 17. Marts
Klima Og Sundhed Jordens Dag 17. MartsAnne Grete Rasmussen
 
ZeroMQ as scriptable sockets
ZeroMQ as scriptable socketsZeroMQ as scriptable sockets
ZeroMQ as scriptable socketsLourens Naudé
 
Advanced Fieldcraft
Advanced FieldcraftAdvanced Fieldcraft
Advanced Fieldcraftgutman
 

Andere mochten auch (8)

Ai Tornado Grenade Manual
Ai Tornado Grenade ManualAi Tornado Grenade Manual
Ai Tornado Grenade Manual
 
ddddkkk
ddddkkkddddkkk
ddddkkk
 
Klima Og Sundhed Jordens Dag 17. Marts
Klima Og Sundhed Jordens Dag 17. MartsKlima Og Sundhed Jordens Dag 17. Marts
Klima Og Sundhed Jordens Dag 17. Marts
 
Payments On Rails
Payments On RailsPayments On Rails
Payments On Rails
 
ZeroMQ as scriptable sockets
ZeroMQ as scriptable socketsZeroMQ as scriptable sockets
ZeroMQ as scriptable sockets
 
Barcamp PT
Barcamp PTBarcamp PT
Barcamp PT
 
Advanced Fieldcraft
Advanced FieldcraftAdvanced Fieldcraft
Advanced Fieldcraft
 
Qatar Project
Qatar ProjectQatar Project
Qatar Project
 

Ähnlich wie Reactor Pattern for Soft Realtime Systems

Timothy j cash_mini_career_portfolio
Timothy j cash_mini_career_portfolioTimothy j cash_mini_career_portfolio
Timothy j cash_mini_career_portfolioTimothy Cash
 
Building a platform with Django, Docker and Salt | Djangocon lightning talk
Building a platform with Django, Docker and Salt | Djangocon lightning talkBuilding a platform with Django, Docker and Salt | Djangocon lightning talk
Building a platform with Django, Docker and Salt | Djangocon lightning talkdotCloud
 
Building a Platform with Django, Docker and Salt
Building a Platform with Django, Docker and SaltBuilding a Platform with Django, Docker and Salt
Building a Platform with Django, Docker and SaltDocker, Inc.
 
Building a platform with Django, Docker, and Salt
Building a platform with Django, Docker, and SaltBuilding a platform with Django, Docker, and Salt
Building a platform with Django, Docker, and Saltbaremetal
 
Wac web access_client_spec_sheet
Wac web access_client_spec_sheetWac web access_client_spec_sheet
Wac web access_client_spec_sheetQBsoft Solutions
 
Cloud Foundry, the Open Platform As A Service
Cloud Foundry, the Open Platform As A ServiceCloud Foundry, the Open Platform As A Service
Cloud Foundry, the Open Platform As A ServicePatrick Chanezon
 
Turbocharging Rackspace Cloud with the RightScale Cloud Management Platform
Turbocharging Rackspace Cloud with the RightScale Cloud Management PlatformTurbocharging Rackspace Cloud with the RightScale Cloud Management Platform
Turbocharging Rackspace Cloud with the RightScale Cloud Management PlatformRightScale
 
Cloud Computing Disaster Recovery Framework
Cloud Computing Disaster Recovery FrameworkCloud Computing Disaster Recovery Framework
Cloud Computing Disaster Recovery FrameworkOnline Tech
 
Unlocked London - General Session
Unlocked London - General SessionUnlocked London - General Session
Unlocked London - General SessionWayne Walls
 
Plone Deployment Secrets & Tricks
Plone Deployment Secrets & TricksPlone Deployment Secrets & Tricks
Plone Deployment Secrets & TricksSteve McMahon
 
Driving Down Costs of z Systems™ Storage
Driving Down Costs of z Systems™ StorageDriving Down Costs of z Systems™ Storage
Driving Down Costs of z Systems™ StorageCA Technologies
 
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamuraIndonesia Network Operators Group
 
Moulding and shaping Asterisk for any size business
Moulding and shaping Asterisk for any size businessMoulding and shaping Asterisk for any size business
Moulding and shaping Asterisk for any size businessClarotech_Events
 
Plone Deployment (PloneConf Edition)
Plone Deployment (PloneConf Edition)Plone Deployment (PloneConf Edition)
Plone Deployment (PloneConf Edition)Steve McMahon
 
Silver peak acceleration, agility and velocity
Silver peak   acceleration, agility and velocitySilver peak   acceleration, agility and velocity
Silver peak acceleration, agility and velocityresponsedatacomms
 

Ähnlich wie Reactor Pattern for Soft Realtime Systems (19)

Timothy j cash_mini_career_portfolio
Timothy j cash_mini_career_portfolioTimothy j cash_mini_career_portfolio
Timothy j cash_mini_career_portfolio
 
Building a platform with Django, Docker and Salt | Djangocon lightning talk
Building a platform with Django, Docker and Salt | Djangocon lightning talkBuilding a platform with Django, Docker and Salt | Djangocon lightning talk
Building a platform with Django, Docker and Salt | Djangocon lightning talk
 
Building a Platform with Django, Docker and Salt
Building a Platform with Django, Docker and SaltBuilding a Platform with Django, Docker and Salt
Building a Platform with Django, Docker and Salt
 
Building a platform with Django, Docker, and Salt
Building a platform with Django, Docker, and SaltBuilding a platform with Django, Docker, and Salt
Building a platform with Django, Docker, and Salt
 
Wac web access_client_spec_sheet
Wac web access_client_spec_sheetWac web access_client_spec_sheet
Wac web access_client_spec_sheet
 
Cloud Foundry, the Open Platform As A Service
Cloud Foundry, the Open Platform As A ServiceCloud Foundry, the Open Platform As A Service
Cloud Foundry, the Open Platform As A Service
 
Turbocharging Rackspace Cloud with the RightScale Cloud Management Platform
Turbocharging Rackspace Cloud with the RightScale Cloud Management PlatformTurbocharging Rackspace Cloud with the RightScale Cloud Management Platform
Turbocharging Rackspace Cloud with the RightScale Cloud Management Platform
 
Going Google? Is Your School Infrastructure Ready?
Going Google? Is Your School Infrastructure Ready?Going Google? Is Your School Infrastructure Ready?
Going Google? Is Your School Infrastructure Ready?
 
Cloud Computing Disaster Recovery Framework
Cloud Computing Disaster Recovery FrameworkCloud Computing Disaster Recovery Framework
Cloud Computing Disaster Recovery Framework
 
Synthetic and Real World Web Performance Monitoring
Synthetic and Real World Web Performance Monitoring Synthetic and Real World Web Performance Monitoring
Synthetic and Real World Web Performance Monitoring
 
Unlocked London - General Session
Unlocked London - General SessionUnlocked London - General Session
Unlocked London - General Session
 
Plone Deployment Secrets & Tricks
Plone Deployment Secrets & TricksPlone Deployment Secrets & Tricks
Plone Deployment Secrets & Tricks
 
Driving Down Costs of z Systems™ Storage
Driving Down Costs of z Systems™ StorageDriving Down Costs of z Systems™ Storage
Driving Down Costs of z Systems™ Storage
 
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura
04 (IDNOG01) Handling massive numbers subscribers and attacks by Takeki kumamura
 
Moulding and shaping Asterisk for any size business
Moulding and shaping Asterisk for any size businessMoulding and shaping Asterisk for any size business
Moulding and shaping Asterisk for any size business
 
Breaking the Status Quo
Breaking the Status QuoBreaking the Status Quo
Breaking the Status Quo
 
Plone Deployment (PloneConf Edition)
Plone Deployment (PloneConf Edition)Plone Deployment (PloneConf Edition)
Plone Deployment (PloneConf Edition)
 
Silver peak acceleration, agility and velocity
Silver peak   acceleration, agility and velocitySilver peak   acceleration, agility and velocity
Silver peak acceleration, agility and velocity
 
Aruba Webinar - 1-29-15
Aruba Webinar - 1-29-15Aruba Webinar - 1-29-15
Aruba Webinar - 1-29-15
 

Mehr von Lourens Naudé

EuRuKo 2011 - In the Loop
EuRuKo 2011 - In the LoopEuRuKo 2011 - In the Loop
EuRuKo 2011 - In the LoopLourens Naudé
 
Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven ArchitectureLourens Naudé
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsLourens Naudé
 
RailswayCon 2010 - Command Your Domain
RailswayCon 2010 - Command Your DomainRailswayCon 2010 - Command Your Domain
RailswayCon 2010 - Command Your DomainLourens Naudé
 
Railswaycon Inside Matz Ruby
Railswaycon Inside Matz RubyRailswaycon Inside Matz Ruby
Railswaycon Inside Matz RubyLourens Naudé
 

Mehr von Lourens Naudé (6)

EuRuKo 2011 - In the Loop
EuRuKo 2011 - In the LoopEuRuKo 2011 - In the Loop
EuRuKo 2011 - In the Loop
 
Event Driven Architecture
Event Driven ArchitectureEvent Driven Architecture
Event Driven Architecture
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
RailswayCon 2010 - Command Your Domain
RailswayCon 2010 - Command Your DomainRailswayCon 2010 - Command Your Domain
RailswayCon 2010 - Command Your Domain
 
Railswaycon Inside Matz Ruby
Railswaycon Inside Matz RubyRailswaycon Inside Matz Ruby
Railswaycon Inside Matz Ruby
 
Embracing Events
Embracing EventsEmbracing Events
Embracing Events
 

Kürzlich hochgeladen

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Kürzlich hochgeladen (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Reactor Pattern for Soft Realtime Systems

  • 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