Categories
- BLOG -

What NodeJS did right

NodeJS, the MEAN stack and Javascript in general are the new hot kids on the block. Everyone’s raving and companies are developing strategies.

What NodeJS did right is use Non-Blocking IO.

What is Non-Blocking IO?

When I go to www.wikipedia.org the following happens:

  1. My computer connects with the kind servers at WikiPedia’s impressive offices and says “Hey Mustafa wants to access your site”
  2. WikiPedia being a non-biased organization says “Yea sure. Come on in.” This response and the decision making for this response is done by some of their CDN/LoadBalancer/Web Server programs.And thus a connection is established between us.
  3. What happens next is totally dependant on what I asked for!Let’s say I requested a *.gif image found on their site … the response will just be a rendering image in my browser. Fairly simple.However, if I had requested one of their usual wiki articles. The following would have to happen:
  4. The web server would pass along the request by me to their PHP based wiki application with  the URL, and other server collected details about me.
  5. The PHP application would now be in control. It can either also continue to give me a green light and render the content I requested; or decline my request and thus end our connection.Let’s assume the PHP app doesn’t decline and decides to process my request.
  6. The PHP app will execute several queries to the database server(s) to retrieve the article data I requested. Assuming the database server has the requested data, it will send back the data to the PHP app.
  7. The PHP application would compose the final HTML page with the all the retrieved data and send it back to the webserver with an “All clear”.
  8. The web server would then send me back the rendered HTML page in my browser where I would see it.

Each of the above steps take some time. Let’s say the whole process from steps 1 to 8 takes 1.5 seconds; step 6 can take 0.45 seconds of that time.

That’s a large portion of the total process!

In PHP (and most other server side technologies) this entire chain of events described above is blocking and synchronous.

Only when step 1 is complete, step 2 can commence and the sequence goes on to the end.

Web apps usually have excessive communication with their  database servers. These back-and-forth communications between the server-side app and database servers has latency costs and account for a noticeable chunk  of the entire response time of any web app.

So someone in the NodeJS founding team probably said to themselves “Hey why don’t we come up with a way to make the input/output step non-blocking to the rest of the app’s processes?”

So step 6 in the above workflow; if we make it non-blocking and take it out of the sequence of events necessary to complete the HTTP request;  would cut down the entire app’s response time by 30% for the above mentioned request.

That’s what Non-Blocking I/O (input/output) is! Step 6 just occurs asynchronously. Once the app completes step 6 separately, it sends the response back via a callback to the frontend Javascript app (using AJAX); allowing the app to run more smoothly.

Is NodeJS perfect? I don’t think so.

However they analyzed the workflow of web apps carefully and came up with an innovative solution to I/O latency. So bravo to them!

The PHP community (as well as all other tech stacks) should also try to come up with their own solutions to the I/O latency, as it is a real thing.

At Web Dotz, we are trying to come up with our own innovative solution to this problem: Intuitive I/O.

By Mustafa Ghayyur
July 5th, 2018