Friday, July 9, 2010

Review of ZeroMQ

Introduction

0MQ, or ZeroMQ, is message-passing middleware designed to be scalable, simple to include in your app (many language bindings), and above all else FAST. I'd recommend using ZeroMQ and not the low level Berkeley socket interface or a high level messaging system because it gives users the flexibility and performance of the low level while still having the ease of implementation of the high level. Also, maintaining raw sockets is difficult and cumbersome when you want to implement a scalable system. A high level system often works perfect if you use it for the situation it was designed for, but it can be difficult to change core elements of the system and its ease of use often comes with a cost in performance.

FAST

See some of these benchmarks in this scipy talk.  Here is one example using the python language bindings, PyZMQ:


Easy to use

Using 0MQ amounts to three things:
  1. Choose a transport
  2. Set up the infrastructure
  3. Select a messaging pattern
This is demonstrated in the following simple example using the python bindings:

import zmq
c = zmq.Context()
s = c.socket(zmq.REP)
s.bind('tcp://127.0.0.1:10001')
s.bind(‘inproc://myendpoint’)
while True:
    msg = s.recv()
    s.send(msg)

Where I (1) create a context, (2) create a socket type, (3) pick one or more transports, and (4) call socket.send and socket.recv. In actuality 0MQ provides 4 different transports:
  • INPROC an In-Process communication model
  • IPC an Inter-Process communication model
  • MULTICAST multicast via PGM, possibly encapsulated in UDP
  • TCP a network based transport
The TCP transport is often the best choice.  To see a few more examples of 0MQ see the introduction by Nicholas.  I downloaded it on my linux box, built it, and got the python bindings from git working in no time.  Especially useful with large-scale consumer websites, high-frequency trading, and multiscale modeling of complex systems. 



No comments: