Novandi Banitama
3 min readMay 15, 2021

--

NSQ Consumer

Nowadays in microservice era, we would find many complicated services interacting to each other. And as it evolves, the communication between services / inter-service communication has changed as well. Saying on https://microservices.io/patterns/communication-style/messaging.html , it’s common to use “Messaging Pattern” to decouple their service with their dependencies. As we notice, there are several technologies implementing this pattern like rabbitMQ, apache kafka and NSQ.

Well well, if you ask me which one we should use among those tools, honestly, I still don’t have the precise answer for that. So sorry to say that but let’s try to focus on one specific tools like I mentioned on the title “NSQ Consumer”.

NSQ from its github

NSQ is a realtime distributed messaging platform.”

If you hope to find the benefit and features of NSQ, please refer to its official documentation https://nsq.io or maybe I could give you a gist of it on the next article, hopefully yaa. And also watch this video to help your understanding on scaling NSQ. Please bear with me :)

https://www.youtube.com/watch?v=OwD-W7uU2zU

From here on, you need to be familiar with several terms like nsqd, consumer, producer, topic etc so you’re not getting lost along the way.

nsq.io

As we started on our topic, lemme to underline that this implementation is only for Golang. I use the package from “github.com/nsqio/go-nsq” to implement the consumer.

Before we can add our consumer, we need several steps to do beforehand.

At the beginning, setup the configuration for the nsq. Many things you could setup for this like:

  • Maximum number of times consumer will attempt to process a message before giving up
  • Maximum number of messages to allow in flight
  • Maximum duration when Re-Queueing
setup the configuration

Secondly, create a message handler if the consumer receives message from nsqd. Here, you will also decide if the message is really accepted by the consumer or not. If not, you could requeue the message.

implementation message handler for consumer

Then finally, you could create the consumer for getting the message from nsqd.

Since we don’t know how many consumer we will need, I also add “the hub” to handle and wrap registering many incoming consumer or possibly to unregister as well.

handle registering / unregistering the consumer

And, it’s finished!!! You have done creating a consumer to nsqd. You could publish message to nsqd via producer(next article probably) or via curl to test your consumer.

curl to publish message to nsqd

That’s all the consumer I can share. You could also refer from my github https://github.com/nbanitama-tech/go-consumer for this code. If you have any queries, please feel free to reach me. Thanks!!!

--

--