Skip to main content

There are multiple good reasons to build Node.js microservices, this post discusses 5 of the potent ones. Node.js and microservices are a match made in modern software engineering heaven. Let’s analyze further why Node.js microservices makes more sense.

Create noejs microservices

Table of contents #

  1. What is microservices architecture
  2. What is Node.js
  3. Node.js pros and cons
  4. Why Node.js microservices?
    1. Enables full-stack web development
    2. Built-in web server for Node.js Microservices
    3. Node.js Microservices elevate developer happiness
    4. Node Js is fast and scalable
    5. Battle-tested and used at the enterprise scale
  5. Conclusion

What is microservices architecture #

Martin fowler a visionary in this domain describes microservices as:

The microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.

He further adds these services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

From his definition, it is crystal clear that microservices need to be small and cater to only one slice of the business.

Technically, it should be independent to deploy in an automated way. Node.js fits very well with the requirements for the application to be small and performant. Time to examine what node is and what it can do for creating better microservices.

What is Node.js #

Mozilla developer portal describes Node.js as:

Node (or more formally Node.js) is an open-source, cross-platform runtime environment that allows developers to create all kinds of server-side tools and applications in JavaScript. The runtime is intended for use outside of a browser context (i.e. running directly on a computer or server OS). As such, the environment omits browser-specific JavaScript APIs and adds support for more traditional OS APIs including HTTP and file system libraries.

From the above description, it is evident that Node.js is javascript but running in a different runtime than the browser. This runtime is the V8 engine. It is written in C++ and is used in Chrome too.
This is a solid sign of Atwood’s law that states:

Any application that can be written in JavaScript, will eventually be written in JavaScript.

So Node.js enables us to write server-side applications in a non-blocking way and in a single thread. You can read more about Node.js being single-threaded or not in this piece. Next, examine the pros and cons of Node.js and how it helps for Node.js microservices.

Node.js pros and cons #

As Bjarne Stroustrup (known for inventing C++) very well said:

There are only two kinds of languages: the ones people complain about and the ones nobody uses.

Without doubts, Node.js is also a language runtime that a lot of people use and complain about. Funny enough you can use C++ addons in Node.js. The video below summarizes the history as well as pros and cons of Node.js in an unambiguous way:

It boils down to Node.js is fast, lightweight, performant, and scaleable. It also has lots of packages on npm. On the flip side, it is not great for CPU intensive programs due to blocking the event loop. Well, the video suggests “immature tools” but that can be said for most languages.

Why Node.js microservices? #

Node.js is very well suited for microservices.

From Node.js about page:

HTTP is a first-class citizen in Node.js, designed with streaming and low latency in mind. This makes Node.js well suited for the foundation of a web library or framework.
It is great for decoupled applications as you can use lots of npm modules to sew up a great microservice. Node.js is fast and its event-based nature makes it a great choice even for real-time applications.

From a practical experience, we once re-wrote a service from PHP to Node.js and it was 70% faster. It also used up fewer resources. Another example is GoDaddy moved from .net to Node.js and they loved it. Netflix improved its application load time by 70%. Let’s analyze more reasons to opt for Node.js for your next microservice:

Enables full-stack web development #

Node.js enables actual full-stack web development. The frontend without doubts needs to be in javascript. Of course, the choice of framework or library for the frontend can be a good discussion. With Node.js, the same javascript runs on the server too. If there are 5 developers in a team and all of them code javascript. It helps a lot for them to be full-stack.

Yes, the developers should learn the backend vs frontend concepts but they don’t need to learn a completely new programming language.

Node.js has many other features and benefits. Unlike the frontend, the software engineer doesn't need to worry about cross-browser compatibility and browser versions while writing Node.js code for a REST API. Node.js also doesn’t need the weight of transpilers like Babel.

As reported at Paypal - “Using JavaScript on both the front-end and the back-end removed an artificial boundary between the browser and server, allowing engineers to code both.” So Node.js adoption does open new doors of full-stack web development.

Built-in web server for Node.js Microservices #

Node.js has a built-in web server. You don't have to wrangle with another Nginx or Apache. You can also happily say goodbye to things like FPM as Node.js is essentially single-threaded. You might want to use PM2 to manage the process.

Most likely you will not even need to deal with the HTTP library of Node.js. As it will be abstracted by the framework you use.

If you use express js, it is super easy to have the webserver up and running.

Node.js also handles multiple requests and responses very easily. This also makes dockerizing a Node.js application a breeze. You just have one container that has the code and the webserver. You can have a look at this Dockerfile as an example, it also utilized Docker multi-stage builds. Still, depending on the case you might want to put Nginx in front of Node.js.

Node.js Microservices elevate developer happiness #

Developer happiness is a subjective matter affected by multiple factors. The Node.js user survey report of 2018 states:

Node.js is continuing to have a positive impact on users particularly around developer productivity and satisfaction; when asked to describe Node.js, respondents use mostly positive terms like – “fast”, “easy”, “awesome”, “powerful”, “flexible” and even “fun”.

In addition, a report from 2019 stated that there are 11.4 million javascript developers globally. It made javascript the language with the most developers in the world.

The Stackoverflow developer survey points out:

Unsurprisingly, for the eighth year in a row, JavaScript has maintained it's stronghold as the most commonly used programming language.

In the same survey, it puts Typescript (a superset of Javascript) as the second most loved language. Javascript is at no. 10 of the most loved language list. Javascript is good to work with. Like any other language, it has its own quirks, and getting used to it. Once you are comfortable with javascript and Node.js it is a pretty good language to work with. Which is a good thing for developer happiness.

With the massive list of libraries at your disposal at NPM (the package manager for javascript), you can find many things you need already built and readily available.

On top of this developer’s happiness aspect, it will not be difficult to find a javascript or Node.js software engineer.

This speaks volumes about developer happiness in the Node.js ecosystem. It is also a key aspect for you to try Node.js for microservices.

One personal experience I want to share is, how easy it is to upgrade even major version of Node.js.

Recently I upgraded a microservice from Node.js 8 to Node.js version 14. The upgrade was as smooth as butter with a reduction in response time bonus. I wished I could say that for PHP. Deploying Node.js is also easy, if your application is dockerized it becomes even easier. You can learn about dockerizing your Node.js application in this step-by-step tutorial. This surely elevates developer happiness while working in Node.js to new levels.

Node Js is fast and scalable #

Node.js is fast because of the non-blocking IO and event loop. With Node.js you can easily put things in the background which doesn’t come out of the box in languages like PHP. PHP for instance executes code sequentially.

Where as in Noddejs you could get a request, respond that the task is scheduled and put the task in the background to be executed.

Node.js being event-based is a very good fit for microservices. With things like streams and real-time capabilities, choosing Node.js for your next microservice should be a no brainer.

Fast is another subjective term. Node.js is fast but compared against what. On the other hand, scalability is a bit more objective. It can be measured in terms of response times and requests per second. Wallmart the world’s 125th website in terms of traffic surely knows about high volumes and scalability. After a move to Node.js they reportedly survived 6 million page views per minute on a black Friday without downtime. That brings us to the next point, enterprise-ready.

Battle-tested and used at the enterprise scale #

There are many examples where Node.js has shined. Possibly many of them included Node.js microservices too. Having a quick look at companies using Express you find big names like IBM and Accenture. Myntra is also on that list, which is currently the world’s top 659th website taking the traffic into account.

LinkedIn tried Node.js in 2012, reporting 27 servers cut and responses 20x faster in some scenarios.

Similarly in Paypal, there was a 35% decrease in the average response time for the same page with the use of Node.js. Netflix measured a 70% reduction in startup time with the use of Node.js. Likewise, Ebay converted a Node.js Hackathon project into a production application due to scalability and resource usage.

The list goes on and on, Spotify using Node.js microservices definitely proves the scale with 75 million active users.

In addition, it also proves it can work in a large scale organization with 90 teams, 600 developers, and 5 development offices on 2 continents building the same product.

You can read more success stories in this post. Node.js microservices are here to stay and they have been proven both battle-tested and working amazingly at enterprise scale.

Conclusion #

Javascript might be the most misunderstood language in the world but with best practices in place Node.js is here for the long-term. It is ruling the enterprises as well as making the startup developers happy.

Don’t hesitate to pick up Node.js for your next microservice, you might be in for a sweet surprise.

Comments