Monday, March 13, 2023
HomeGolangMessage Queues, or how one can make processes discuss to one another...

Message Queues, or how one can make processes discuss to one another · Utilized Go

When Processes want to speak

Functions and providers typically should be scalable. The consumer base may develop from 10 to 10,000, or the incoming variety of requests may improve by some orders of magnitude. One strategy to scaling is to make use of quicker computer systems. The opposite one is to make use of extra computer systems and distribute workload amongst them.

One other state of affairs: Generally you must preserve separate issues utterly separated. In different phrases, each distinct performance shall run as a separate OS course of: A database, a Net server, a course of that implements your online business logic, and so forth.

Message Queues assist connecting these processes.

Message Queues in a nutshell

Message Queue techniques (or brief, MQ techniques) present a method to move arbitrary messages between processes. Often they supply some kinds of topologies, or communication patterns, every of which inserts a selected communication goal.

Some MQ techniques additionally present a brokerage service, which acts as a widely known contact level for locating a selected service, in addition to for routing messages from sender to receiver. This may be handy particularly for giant distributed techniques. Small techniques could do properly with out a dealer and thus profit from increased throughput, as brokerless MQ techniques might be a lot

nanomsg – the minimalist MQ framework

To be able to look into some examples of inter-process communications, we’ll use [nanomsg][NMG], a light-weight messaging system that has a few advantages:

  • nanomsg doesn’t want a dealer, nor a server infrastructure. Light-weight as might be.
  • nanomsg is dead-easy to grasp and to make use of. No bloated ideas and over-complicated implementations.
  • nanomsg gives an affordable set of frequent communication topologies (additionally referred to as “scalability protocols” in nanomsg terminology) out of the field. No must reinvent the wheel over and over.
  • A pure Go consumer is accessible (see under).
  • There’s a ton of nanomsg implementations for different languages obtainable, too. Wish to join your Go course of with another course of written in C++, Java, Python, Rust, Ocaml, Erlang,…?
    Right here you go!

Mangos: nanomsg in pure Go

Mangos is a Go implementation of nanomsg. It options a simple and intuitive API, as we’ll uncover later when going via a primary instance.

How does nanomsg outline communication patterns?

It does so by offering so-called “Scalability Protocols”. Every Scalability Protocol defines a selected communication sample.

These protocols are at present outlined:

  • Pair


    Motto: Scale your software by breaking it in two items.

  • Request-Reply


    Motto: Distribute workload amongst a number of stateless staff.

  • Writer-Subscriber


    Motto: Broadcast messages to a number of locations. Receivers can subscribe to particular subjects.

  • Pipeline


    Motto: Accumulate output from a number of nodes of 1 processing step and distribute it among the many nodes of the subsequent processing step.

  • Survey


    Motto: Broadcast a survey and collect the responses. Watch for the replies for a sure time solely.

  • Bus


    Motto: Broadcast messages from any node to all different nodes.

The essential constructing block of a Scalability Protocol is a nanomsg Socket.

What’s a socket?

Sockets typically are messaging endpoints, often outlined by

  • a transport mechanism,
  • an IP handle, and
  • a port quantity.




nanomsg gives a few transport mechanisms:

  • In-process
  • Inter-process (however nonetheless on the identical machine)
  • TCP
  • WebSockets

A course of can present a socket to different processes, in addition to connect with a distant socket of one other course of.

When a course of gives a socket to others, it “listens” on the socket (in Mangos lingo). When it connects to a distant socket, it “dials” this socket. (You’ll be able to see this distinction later in our expample.)

Sockets in nanomsg have one other fascinating characteristic: They impelement a selected Scalability Protocol. Which means, a nanomsg socket takes care of managing all internals of the protocol in order that your code can roughly concentrate on sending and receiving messages.

A primary instance: PAIR

Let’s dive straight into our first instance: A easy PAIR communication.

The PAIR protocol lets two processes ship messages to one another. Not one of the two nodes has a selected position. Every one can ship and obtain messages to and from the opposite one.

Typical use case: To separate up a big software into two smaller elements.


So what are we going to implement? In brief, we need to have two processes operating. One in all them listens on a socket, the opposite one dials that socket. As soon as they’re linked, they change a few messages.

You may get the complete supply code at

Use go get -d to make sure that the binary does not get put in into your $GOPATH/bin listing. Moderately, use go construct to generate an area binary that you just then can run as ./messaging.

Putting in Mangos

(Notice: In the event you go get the messaging code, Mangos is already included through the vendor listing. On this case you don’t want to put in Mangos individually.)

Putting in Mangos is as straightforward as getting into

go get -u

on the command line.
To make sure every little thing has been put in accurately, you may need to run the exams. For this, enter:

go take a look at $GOPATH/src/ a look at

If every little thing is okay, we will transfer ahead to making a pattern PAIR implementation.

Implementing a PAIR instance

To run this instance, get the code from github:

go get -d

(The -d flag prevents Go from putting in the binary in your $GOPATH/bin listing.)
Then cd to the pair listing and run:

go construct

Then open a second terminal and cd to the identical listing. Within the first terminal, enter

$ ./messaging 0 "tcp://localhost:54545"

and within the different one, sort

$ ./messaging 1 "tcp://localhost:54545"

(Notice that you may decide an arbitrary port quantity from the “Dynamic” vary between 49,151 and 65,535 – they solely should be the identical for each processes.)

In the event you began node 0 first, your output ought to appear to be this:

$ ./messaging 0 "tcp://localhost:45454"
2016/02/04 11:44:55 Node 0 sends message 0 from node 0.
2016/02/04 11:44:58 Node 0 obtained message 0 from node 1.
2016/02/04 11:44:58 Node 0 sends message 1 from node 0.
2016/02/04 11:44:58 Node 0 obtained message 1 from node 1.
2016/02/04 11:44:58 Node 0 sends message 2 from node 0.
2016/02/04 11:44:58 Node 0 obtained message 2 from node 1.
2016/02/04 11:44:58 Node 0: Completed.

And node 1 ought to have procuded one thing like this:

$ ./messaging 1 "tcp://localhost:45454"
2016/02/04 11:44:58 Node 1 can not pay attention on socket 'tcp://localhost:45454': pay attention tcp bind: handle already in use
Attempting to dial as a substitute
2016/02/04 11:44:58 Node 1 sends message 0 from node 1.
2016/02/04 11:44:58 Node 1 obtained message 0 from node 0.
2016/02/04 11:44:58 Node 1 sends message 1 from node 1.
2016/02/04 11:44:58 Node 1 obtained message 1 from node 0.
2016/02/04 11:44:58 Node 1 sends message 2 from node 1.
2016/02/04 11:44:58 Node 1 obtained message 2 from node 0.
2016/02/04 11:44:58 Node 1: Completed.

Train 1

Attempt ipc: as a substitute of tcp:

Train 2

The loop in runNode could appear foolish because it serializes sending and receiving for no good purpose (aside from attempting to stay easy).
Flip the loop into two goroutines that ship and obtain independently.

What’s subsequent?

The PAIR protocol is the best one of many Scalability Protocols. The extra advanced ones are, not surprisingly, additionally the extra fascinating ones.
Within the subsequent article we’ll discover the PubSub protocol, a standard sample for distributing data from one sender to a number of receivers.

Updates and errata

  • 2016-05-29 Fastened: Small glitch within the path of go take a look at.
  • 2016-05-29 Up to date: Mangos is now within the vendor dir. You should use go get with out go-getting Mangos first.
  • 2016-05-30 Fastened: Damaged hyperlinks and a small typo.
  • 2016-06-13 Fastened: go get was lacking the -d flag.


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments