ZeroMQ

An introduction

- Pål Ruud / @ruudud

What is ZeroMQ?

  1. Just another MQ
  2. Aha, so it's just plain old sockets.
  3. But messaging, isn't that a solved problem? (Maybe with ZeroMQ)
  • An asynchronous messaging library
  • Acts like a concurrency framework
  • Fast
  • Used by banks for trading systems
  • 0MQ, ZMQ, ØMQ, ZeroMQ
  • Don't need no Message Broker
  • The API is similar to the socket API
  • The same people that drafted AMQP created ZeroMQ
  • Bindings to most/all languages

Transport layer

  • TCP
  • PGM (multicast)
  • IPC (inter-process)
  • ITC/INPROC (inter-thread)

Funfact: Can have higher throughput than TCP because of message batching.

The point of a messaging system

$$$

Because a client may easily send work to more servers, you have a way of getting more horsepower without writing a lot of code.

Even poorly implemented code can have a new life with scaling by the assistance of the messaging middleware.

Design criteria in a messaging system

  • Whether messages are transferred reliably
  • Whether messages are guaranteed to be delivered in order
  • Whether messages are passed
    • one-to-one (unicast)
    • one-to-many (multicast/broadcast)
    • many-to-one (client-server)
    • many-to-many
  • Whether communication is synchronous or asynchronous.

Source: Wikipedia

Message Patterns in ZeroMQ

Request-reply


RPC and classic client-server

Publish-subscribe


Data distribution

Push-pull (pipeline)


The Broker/Queue

Broker - Advantages

  • Apps don't need to care about the whereabouts of other apps
  • Sender and receiver lifetimes don't have to overlap
  • The app can fail if the broker has received the message

Broker - Drawbacks

  • Excessive amount of network communication
  • Can turn in to a bottleneck, or SPOF

Brokers in ZeroMQ

Zero M Q

But that doesn't mean we can't create one.

Example 1 - request and reply


ctx = zmq.Context()
socket = ctx.socket(zmq.REQ)
socket.connect("tcp://0.0.0.0:12345")

while True:
    socket.send("Hello from %s" % sys.argv[1])
    print socket.recv() # Blocking
                        

client.py

Example 1 - request and reply


ctx = zmq.Context()
socket = ctx.socket(zmq.REP)
socket.bind("tcp://*:12345")

while True:
    request = socket.recv() # Blocking
    print 'Got message: %s' % request
    time.sleep(.5)
    socket.send('Hello world back at ya from the server')
                        

server.py

Example 2 - publish and subscribe


context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

while True:
    socket.send("Arsenal:red card")

    time.sleep(1)
                        

publisher.py

Example 2 - publish and subscribe


context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.connect ("tcp://localhost:5556")

socket.setsockopt(zmq.SUBSCRIBE, 'Arsenal')

while True:
    string = socket.recv()
    print string
                        

subscriber.py

Links

High-level overview of ZeroMQ socket

Thanks!

@ruudud