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.
Table of contents #
- What is microservices architecture
- What is Node.js
- Node.js pros and cons
- Why Node.js microservices?
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:
This is a solid sign of Atwood’s law that states:
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 #
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 -
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.
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 #
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”.
The Stackoverflow developer survey points out:
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.
Don’t hesitate to pick up Node.js for your next microservice, you might be in for a sweet surprise.