Search code examples
rubyrabbitmqamqp

How does a Ruby AMQP client efficiently block waiting for messages?


Let's say you're a Ruby client of a queueing service, e.g. RabbitMQ. How is a wait for messages implemented so you don't do active polling, consuming resources? This follows directly from question Ruby blocking on a value -- if an AMQP client can do it efficiently, one can do something similar with a Memcached read... And in general, for RabbitMQ and such usage, we have to satisfy ourselves that a wait is smart!


Solution

  • In the Ruby world this is mostly done by using asynchronous event driven I/O. If you look at the AMPQ-client in GitHub, it has support for 2 of these libraries, EventMachine and Cool.IO.

    It is polling in effect, but the waiting loop is done with the help of your operating system's kernel. For example, EventMachine would use something like epoll if running on Linux.

    When the desired event is triggered, your callback function is called. If you want a very deep explanation of how EventMachine works, you can look here.

    The AMPQ client is still communicating over a socket to the Queue server, but the APIs defer the wait loop and notification to the OS kernel.