NodeJS vs Java! An epic battle, which is going to rule in 2020! Part 2

Shashank Sharma
5 min readOct 8, 2019

In the previous article, we discussed the performance factors around the two programming languages, now lets dive into some of the linguistic paradigms of Java and JS.

The earlier versions of Java have mainly focused on OOP and hence has gained the title of OO Language, whereas JS has been very dynamic and functional in nature. However, this perception of both languages is changing rapidly today. Let us go through some of these features.

Anonymous functions

Functions are first-class citizens in Javascript. Meaning, they are treated like any other object. They can be used as a function’s parameters, stored in a variable, can hold there own properties, and even be returned from a function.

This makes Javascript use powerful design patterns such as higher-order functions, Functional programming, etc.

This power was missing in Java until the introduction of Lambda expressions.

A Lambda expression is one of the biggest paradigms shift in Java specifications. This was Java’s first step to Functional Programming.

Some of its features are

  • It can be passed to a method as regular argument anonymously
  • It can be invoked without any Class or object reference
  • It can be returned from a method

Asynchronous operations

In javascript, a Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

In Java there is a similar construct, CompletableFuture is used for asynchronous programming in Java. It is thereby enabling a non-blocking code by executing a task on a separate thread rather than the main application thread and notifying the main thread about its progress, completion or failure.

Non-Blocking IO

NodeJS is free from deadlocks in the process, as it does not perform any I/O directly. This makes NodeJS a good candidate for scalable network applications.

Java has provided a second I/O system called NIO (New I/O). Java NIO provides a different way of working with I/O than the standard I/O API. It is an alternate I/O API for Java (from Java 1.4). Example nio.file provides a non-blocking way to access the file system.

Similarly, there are server framework implementations which leverage NIO, e.g. Netty

Netty is an NIO client-server framework. It eases the development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

Multi-threading

multithreading in Java and Javascript

Java applications utilise the OS threads to achieve high computability and concurrency. Even while using blocking IO APIs, the application remains responsive.

On the other hand, with NodeJS, being a Single-threaded system has always been it’s biggest criticism. That has challenged most of the developers to treat JS as fully-fledged backend/server technology. Well, that has changed now with the introduction of WorkerThreads and Thread pools.

What to use?

The good part about today’s Cloud system is that you no need to rely on one specific technology for everything. Use one solution for one problem. For example, a heavy computation work one can rely on Java and for IO heavy system NodeJS can be a better choice. How to use both simultaneously? Microservices comes in rescue! Let us take a real-world example to apply this logic.

E-commerce platform — A real-world example

Let us take an example of an E-commerce platform a typical use case could be a shopping experience

A user selects a product, places an order and gets an invoice pdf. To implement such web experience, one needs a couple of systems

  • A database of product catalogue
  • A worker engine to generate pdf
  • An HTTP server to serve web pages/data

Now let us categorise these systems into BIO and NIO.

Most of the stable enterprise DB drivers are blocking in nature, mostly to achieve accuracy, eg JDBC. It is simple to use existing Java frameworks to build a service to interface with the database. So Java is the choice for this.

Generation of PDF files usually requires more computation. BOMs, Tax, Discounts, followed by rendering pdfs, this sounds like heavy computation, now we know Java is safe to use.

Finally, we need to deliver these features as a service to web clients. Now it is all about IO as most of the time consuming and heavy computation task is taken care of by other services. NodeJS is a good candidate for this.

A sample system design is shown below

System Design for Enterprise web app

Wrapping it up

Both Java and Javascript have come a long way and have always been evolving. There are absolutely no limitations of one against others when it comes to achieving functionalities. Still comparatively one exceeds the other in different scenarios.

Some thumb rules

  • Before jumping into a design decision understand your requirement in details
  • Break it down into computation and IO
  • Easy way to start is to consider NodeJS for front end servers or IOs and other microservices in java

--

--

Shashank Sharma

💻 Web Architect | Developer | Working with PayPal | Learning & Delivering can go together 😎