Simple ZMQ Message Passing

There are many ways to create RPCs and send messages between nodes in a distributed system. Typically when we think about messaging, we think about a transport layer (TCP, IP) and a protocol layer (HTTP) along with some message serialization. Perhaps best known are RESTful APIs which allow us to GET, POST, PUT, and DELETE JSON data to a server. Other methods include gRPC which uses HTTP and protocol buffers for interprocess communication.

ZMQ is a bit different. It provides an abstraction for sockets that look like embedded networking but can actually be used for in- and inter-process channels, multicast, TCP, and more. ZMQ has many patterns, starting on simple REQ/REP (request/reply) where a client connects to a socket that a server is bound on; the client sends a REQ and waits for a response, the REP from the server.

The interesting thing about this (pretty standard) network communication is that the server doesn’t have to be up for the client to connect, it will just wait until the server is available. Moreover, there is no need for multiplexing because ZMQ buffers messages under the hood. The pattern is incredibly failure resistant. ZMQ is not HTTP, ZMQ is something different with its own protocol, and even though its a lower level networking abstraction, it can be used for very powerful distributed systems design.

This is just a snippet with a bare bones REQ/REP message server and client that passes strings back and forth.

To use this code, download the gist and run the server and client in two different terminal windows with go run. To run the server:

$ go run zmqmsg.go serve

And to send messages:

$ go run zmqmsg.go send "first message" "second message" "third message"

You should see messages received at the server and replies sent back to the client. Of course this is pretty much the hello world of the ZMQ REQ/REP model and there are many other networking patterns and sockets provided by ZMQ to check out. In particular, there is a PUB/SUB pattern where clients can connect to a publisher to receive updates pushed to them. More to come!