This post aims to give you a better understanding of microservices, what they are, what are the benefits and challenges of using this pattern and how you can start building them using Node.js.

Before diving into the world of microservices let us take a look at monoliths to better understand the motivation behind microservices.

The Monolithic Way

Monoliths are built as a single unit, so they are responsible for every possible functionality: handling HTTP requests, executing domain logic, database operations, communication with the browser/client, handling authentication and so on.

Because of this, even the smallest changes in the system involves building and deploying the whole application.

Building and deploying is not the only problem - just think about scaling. You have to run multiple instances of the monolith, even if you know that the bottleneck lays in one component only.

Take the following simplified example:

monolithic application

What happens when suddenly your users starts uploading lots of images? Your whole application will suffer performance issues. You have two options here - either scale the application by running multiple instances of the monolith or move the logic into a microservice.

The Microservices Way

An approach to developing a single application as a suite of small services. - Martin Fowler

The microservice pattern is not new. The term microservice was discussed at a workshop of software architects near Venice in May of 2011 to describe what the participants saw as a common architectural style that many of them had been recently exploring.

The previous monolith could be transformed using the microservies pattern into the following:

microservices

Advantages of Microservices

Evolutionary Design

One of the biggest advantages of the microservices pattern is that it does not require you to rewrite your whole application from the ground up. Instead what you can do is to add new features as microservices, and plug them into your existing application.

Small Codebase

Each microservice deals with one concern only - this result in a small codebase, which means easier maintainability.

Easy to Scale

Back to the previous example - What happens when suddenly your users starts uploading lots of images?.

In this case you have the freedom to only scale the Image API, as that service will handle the bigger load. Easy, right?

Easy to Deploy

Most microservices have only a couple of dependencies so they should be easy to deploy.

System Resilience

As your application is composed from multiple microservices, if some of them goes down only some features from your application will go down, not the entire application.

New Challenges

The microservice pattern is not the silver bullet for designing systems - it helps a lot, but also comes with new challenges.

Communication Between Microservices

One of the challenges is communication - our microservices will rely on each other and they have to communicate. Let's take a look at the most common options!

Using HTTP APIs

Microservices can expose HTTP endpoints, so other services can use their services.

But why HTTP? HTTP is the de facto, standard way of information exchange - every language has some kind of HTTP client (yes, you can write your microservices using different languages). We also have the toolset to scale it, no need to reinvent the wheel. Have I mentioned, that it is stateless as well?

Using Messaging Queues

Another way for microservices to comminucate with each other is to use messaging queues like RabbitMQ or ZeroMQ. This way of communication is extremely useful when talking about long-running worker tasks or mass processing. A good example of this is the Email API - when an email has to be sent out it will be put into a queue, and the Email API will process it and send it out.

Service Discovery

Speaking of communication: our microservices need to know how they can find each other so they can talk. For this we need a system that is consistent, highly available and distributed. Take the Image API as an example: the main application has to know where it can find the required service, so it has to acquire its address.

Useful libraries/tools/frameworks

Here you can find a list of projects that we frequently use at RisingStack to build microservice-based infrastructures. In the upcoming blogposts you will get a better picture on how you can fit them into your stack.

For HTTP APIs:

For messaging:

For service discovery:

Next up

This was the first post in a series dealing with microservices. In the next one we will discover how you can implement service discovery for Node applications.

Are you planning to introduce microservices into your organization? Look no further, we are happy to help! Check out the RisingStack webpage to get a better picture of our services.