Blog

Hydra — A Node.js Microservice Library

Offering a leveraging aftereffect for the developers.

ByPayal Mittal
June 8th . 5 min read
Hydra — A Node.js Microservice Library

Hydra is a module which wraps Hydra and Express.JS to provide an out of the box microservice which can support API routes and handlers.

The Hydra project was open-sourced in New York City at the Empire Node 2016 conference. The key function of Hydra is to build scalable Node microservices.

There are certain arguments about Hydra that mostly what it offers can easily be achieved with the proper use of a combination of libraries, tools, and frameworks. Then, why do we need Hydra??

Quite strong reasoning, don’t you think?? Well, the argument can be discarded by having a better understanding of Hydra-

What is Hydra?

“Hydra is a NodeJS package which facilitates building distributed applications such as Microservices.”

It offers some amazing features such as distributed messaging, service discovery, message load balancing, health monitoring, logging, and presence. Additionally, it accomplishes all these functions by leveraging the Redis’ awesome power.

Moreover, one very interesting fact about the project is- it was named after a mythical beast called Hydra having multiple heads from Greek and Roman mythology. It was assumed that each one of the heads had the capability to act independently, doing multi-tasks, to achieve an objective.

The motive behind creating hydra, to a great extent, was to create a light-weighted npm package that will provide appropriate solutions regarding microservice concerns, that too without having to force developers to change their conception about building microservices.

Not to mention that Hydra is the master key that dampens the barriers and opens up a flat entryway to build and deploy robust and scalable Node microservices.

Features of Hydra -

Although Hydra is implemented with Node.js, its functionalities can be utilized on other platforms as well. The drop-in functionality of Hydra enables it to deal with the following affairs-

  • Service Registration: Registration of services when they come online and publishing their HTTP API routes.
  • API Routability: Enables routing of API calls to a microservice.
  • Messaging: Supports Inter-service communication using server’s networking info, service message queues, makeAPIRequest method, and built-in channels.
  • Networking: Support for various networking options including IP address and Port exploring.
  • Service Discovery: Locate services directly by using the findService() method. Though, only registered services are discoverable.
  • Presence Reporting: Just because a service is located doesn’t explain its availability in the current scenario. Hydra determines whether the service is currently online and active.
  • Health Reporting: Automatic health check reporting and sends its presence information, such as whether the application is healthy and fully functional.

Hydra Powered by Redis

Redis is quite popular among the community of developers and considered an ideal choice, for good reasons indeed. A fair reason behind using Redis for building Hydra is due to its availability on laptops, IoT devices, cloud, and having a supportive host environment like RedisLabs. Redis is highly ranked in online polls conducted during surveys.

“Redis is also likely to already be in your environment as it’s often a go-to solution for intelligent caching.”

Redis is the only dependency Hydra has. Hydra addresses several Node microservices concerns by utilizing the power of Redis and offers a leveraging aftereffect for the developers.

In order to build microservices, Hydra requires to use a Redis server. There are several ways through which you can get access to a Redis server.

  • One of the easiest methods, however, is to sign up with a free-tier cloud provider such as RedisLabs, without having the need to install.
  • For a Docker user, installing an official Redis image will be an astounding option. It will also work for PC users.
  • On Mac, using an easy command brew install redis via Homebrew will suffice to install Redis. However, if you don’t want to install Homebrew, then feel free to install Redis here.

Hydra Modes -

Hydra can be configured in the following modes to use-

1. Service Mode

Under this mode, Hydra acts as a service and utilizes other services as well. When configured in this mode, it records application information and machine data in the Redis server.

In order to use Hydra in service mode, you need to register it first as-

hydra.registerService();

However, if the application doesn’t to be a service, there is no need for this registration.

2. Consumer Mode

In this mode, Hydra works as a consumer of services only and not being a service itself. When a consumer-mode application calls for a service-mode method, it results in an exception or error.

Hydra Methods

There are certain methods that Hydra export publicly. Though, being a module, Hydra discourages using its internal methods to ensure that it works fine as proposed with the forever-extending list of services.

Here’s the list of publicly-exported methods of Hydra that are required by the applications and services, though, not all applications use all of these methods-

  • Module setup and service registration
  • Presence inspection
  • Health check and logging
  • Service discovery
  • Message sending and routing

Hydra Core -

Hydra (core) is a light-weight npm package that empowers the Hydra library. It finds applications in a vast range of conditions from small IoT devices to a dockerized cloud structure.

It is one of the sections under the Hydra project and was designed to enable the building of microservices and making the non-service applications use the microservices. It is generally used to build non-express.js applications.

Hydra Express -

Hydra-Express is another npm package for developers having familiarity with Express.js. With Hydra-Core, it is used to build express-js based distributed applications in the easiest and fastest way.

It can be installed and use in other projects under the following command-

$ npm install hydra-express

Hydra Tools

The hydra project uses a handful of tools to build and test hydra applications and microservices, described as under -

Hydra-CLI:

A command-line client that simplifies working with hydra-enabled apps. You need to install it first to get started, with the given command -

$ sudo npm install -g hydra-cli

Hydra-Router:

A Service-Aware router is a gateway for directing WebSocket messages and HTTP requests to the registered devices. HTTP and WebSocket clients can communicate to hydra microservices and vice-versa, by making use of Hydra Router.

You can use get the Hydra-Router directly from the Docker container -

$ docker pull flywheelsports/hydra-router

Hydra Generator:

It is a tool that augments the generation of Hydra/Hydra-Express starter applications. An interesting fact about this Yeoman generator is that you can build a microservice with it in merely 15 seconds.

The command used to install the Yeoman and generator is-

$ sudo npm install -g yo generator-fwsp-hydra

Using Hydra Router external clients can connect to services without knowing their IP or port information. Hydra Router takes care of service discovery and routing.

Getting Started with Hydra

Before getting started with Hydra, you will need-

  • Node version 6.2.1 or higher
  • Access to a Redis server

Let’s see how to install and initialize Hydra -

#Install Hydra
$ npm install hydra
#Importing Hydra
const hydra = require('hydra');
#Initializing Hydra
hydra.init(initObject);
#The initObject consists of-
{
serviceName: 'hydramcp',
serviceDescription: 'Hydra Master Control Program',
serviceIP: '',
servicePort: 0,
serviceType: 'mcp',
redis: {
host: '127.0.0.1',
port: 6379,
db: 0
}
}

There is a whole lot of other important things that must be understood about the Hydra module. Using Hydra comprises several steps including getting Redis, setting up hydra, grabbing Hydra tools, building, and testing microservices. This blog is an effort to give a mere idea about the Hydra module for the beginners in the briefest way, without fiddling with the details.

Now that you’ve got familiar with it, you might want to dive into the depth of its features and benefits, don’t you? Well, you can learn more about Hydra here.

If you found this blog helpful, you might let us know via your claps. Furthermore, feel free to reach out to our team in case you are looking for an IT company to get your project done.

Thanks for reading!!

Share:
0
+0