googleads
Message Queues in Golang Via RabbitMQ | Hire Golang Developers India
Golang Standard Technical

Message Queues in Golang Via RabbitMQ

Introduction:

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 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

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

Requirement

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 https://github.com/rabbitmq/rabbitmq-tutorials/tree/master/go

To learn more, see RabbitMQ.

The following two tabs change content below.

Bharanidharan Arumugam

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".

Leave a Reply

Your email address will not be published. Required fields are marked *