blog.pleb.in

Life of Apps

Showing posts with label Restify. Show all posts
Showing posts with label Restify. Show all posts

Understanding MicroServices Choreography using RabbitMQ & Node.js

A colleague and I had discussed on choreography of microservices a while ago and the topic came up for discussion again recently. In order to ensure that I understood the nuances correctly, I decided to write some code to implement the approach. Here are the tools/technologies that I chose:

  • Node.js for building the microservices 
  • Restify for exposing Restful APIs
  • CloudAMQP - a RabbitMQ as a service offering as the integration bus
  • amqp.node - a Node.js client library for RabbitMQ.  

CloudAMQP offers a free tier good enough for learning and trying things out. It also comes with an easy to use management console that gives good statistics. The tutorials and articles are useful too.

There is a host of options for the Node.js RabbitMQ client libraries. I tried a few and then settled with amqp.node as the RabbitMQ site had their tutorials on it. The library comes in two flavours, one with callbacks and the other with promises. Being comfortable with callbacks, I opted for it.

The approach that I followed was to mimic a part of the order management process where a POST method triggers the order creation service. The service processes the order, creates an entry in the order management database and sends a message to a RabbitMQ topic. Following is the code snippet.



Another service listens to the topic for a specific key that denotes that the order creation is complete and gets triggered when a message with that key is published. This service checks for inventory and either calls a service to replenish stock if it is low or calls an order shipment service if the stock is available. It too publishes a message on the topic with the respective keys to trigger the following services.  Following is the code snippet.

The code below publishes and consumes from a RabbitMQ topic. Apache Kafka can be used as an alternative to RabbitMQ as an event bus.

This approach will work while creating new microservices as they can be made to start on the basis of an event.

As can be seen from the code, it is heavily work in progress. I am hoping to do the following tasks:

  • Figuring out a good way of closing RabbitMQ connections Done:Updated the code to use npm module death to run a function on process kill to close RabbitMQ connections
  • Making modules out of some common code functions Done: moved the helper functions for HTTP methods and RabbitMQ to a module. 
  • Add compensation logic Done: this post explains the compensation logic 
  • Add an OpenAPI (Swagger) spec Done: added a spec for Order Creation here
  • Create Docker containers 
Full code is available here.


Node.js Code Organization

As mentioned in my previous post, I learnt Node.js and Restify through the Udemy course titled "A Simple Node.js/Mongo/Restify API in Less Than 3 Hours" by Jim Hlad. I made a copy of the Node.js API sample app that was taught in the course and called it Cwitiq. This app would manage reviews instead of users. 

A rough diagram of the code is depicted below. On the right, the directories for the various layers are shown. 


  • include (require) the restify, restify plugins, validator and mongoose modules
  • create a restify server using the createServer function
  • the config directory has the db connection and helper functions to provide responses (output, error etc.)
  • the models directory has the schema and model for storing the reviews in MongoDB using Mongoose
  • the business logic is in the reviewController that is present in the controllers directory along with setupController that utilizes Restify features to control APIs - authorization, whitelisting, throttling
I have been using this layout and code organization while creating other apps in Node.js.


Learning Node.js on Udemy


After a bit of a struggle, I finally would like to believe that I have become comfortable with Node.js. I had initially tried learning Node.js through the course titled "Learn and Understand NodeJS" on Udemy by Anthony Alicea. The course is a bit unconventional as it goes deep into the guts of Node.js right at the start instead of providing an overview. I lost interest in it after a few lectures and almost gave up learning Node.js.

After a while, I decided to look for courses of shorter duration and found a course titled "A Simple Node.js/Mongo/Restify API in Less Than 3 Hours" by Jim Hlad. The USP of this course is that the lectures are of a short duration ~5-7 min. This helps in assimilating the content learned easily. The instructor explains the concepts while coding, instead of talking theory first, making the lectures short and crisp.

This course helped me grasp the basics of Node.js and try out things such as Web Sockets using Socket.io. It would be nicer if the instructor offers this course with Express or other more popular libraries instead of Restify. Also, it is worth noting that this course is aimed at building APIs using Node.js rather than a complete application thereby skipping the UI layer.