An image

Corporate Blogs

Message Queues in Golang Via RabbitMQ

E-Commerce Businesses Need a Mobile App

Golang Developers, In this article, we shall see how to implement message queues in Golang using RabbitMQ.

We believe message queues are a vital component to any architecture or application. For example, some process takes more time to process or occasionally, one needs to delay a time-consuming job for a while. In such cases, the process needs to be queued for future execution.

For this to happen, you need a broker: someone who will accept messages (e.g. jobs, tasks) from various senders (i.e. a web application), queue them up, and distribute them to the relevant parties (i.e. workers) to make use of them – all asynchronously and on-demand.

We can use NSQ for the purpose, which is purely written in Golang or alternatively we can use RabbitMQ which is written in Erlang but wholly supports Golang.

As mentioned above, in this article, we will walk you through RabbitMQ queue implementation in Golang. It is written based on Ubuntu, but it works in other Linux libraries too.

Why use message queues?

Here are the main reasons:

  • Decouplingallows you to extend and modify these processes independently
  • Elasticity & SpikabilityReduce loads and delivery times
  • PersistencePersistence is needed to pass the information along from one place to another. This eliminates data loss since it is readily available in the queue until the process in the queue is completed
  • ScalabilityEasy to scale up the rate with which messages are added to the queue or processed
  • Asynchronous CommunicationAllows you to put a message on the queue without processing it immediately. Queue up as many messages as you like, then process them at your leisure
  • Delivery GuaranteeProvides guaranteed delivery

What is RabbitMQ?

It is an open-source message-broker application stack which implements the Advanced Message Queuing Protocol (AMQP) to facilitate the passing of messages between or within systems.

How does it work?

RabbitMQ connects message senders (Publishers) with receivers (Consumers) through an exchange (Broker) which distributes the data to relevant lists (Message Queues).

Installing RabbitMQ

Install RabbitMQ with following commands




Using RabbitMQ:

The following steps explains how your message gets published and how your message gets handled correctly:

  1. Requirement
  2. Making a connection
  3. Making a channel
  4. Declaring a queue
  5. Publishing a message
  6. Consuming a message


To implement message queue, we need Go RabbitMQ client


Making a connection

AMQP connections are typically long-lived. AMQP is an application level protocol that uses TCP for reliable delivery. AMQP connections use authentication and can be protected using TLS (SSL). When an application no longer needs to be connected to an AMQP broker, it should gracefully close the AMQP connection instead of abruptly closing the underlying TCP connection.

This creates long-lived TCP connection between an AMQP client and a queue broker.


Making a channel

This creates a short-lived sub-connection between a client and a broker.


Declaring a queue

A queue is a first-in, first out (FIFO) holder of messages.
We can handle several queues at the same time. This facilitates categorisation.


Publishing a message

Publish() is the method that puts (publishes) the data into the queue.


Consuming a message

Now that the data is ‘published’ into the queue, it has to be consumed so as to handle the data for the processing.


This is a sample usage of the RabbitMQ to handle message queueing. As mentioned above, the advantages of queueing messages are numerous, and RabbitMQ in Golang enables easy usage to implement it.

For more examples, Check this

To learn more, see RabbitMQ.


We offer Golang development services for building world-class enterprise apps. We have expertise in building the most complex software solutions using Google’s Go language. Chat with us now and hire golang developer within 72 hours.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
Technical Architect | Tech Lead | Mentor | - An enthusiastic & vibrant Full stack developer has 7 plus years of experience in Web development arena. Owns legitimate knowledge in Ruby, Ruby On Rails, AngularJs, NodeJs, MEAN Stack, CMS Services. Apart all, A Modest human who strictly says "No To Harming Humans".

Thoughts on Message Queues in Golang Via RabbitMQ

Hi Renato,

Thank you.

1) Please make sure consumer is listening to your queue.
2) Publishing and consuming message should be using same queue.

Nice article.

I can’t consume pre-existing messages on my queue, just messages inserted when the consumer is running. Any idea on this? I bit frustrated 🙁

Leave a Comment

You have an Idea. We have the Solution.

We help business evolve with lates technologies and infrastructures tailored to their needs and market trends.