1

I am confused with node.js' advantages over other tech. I've been reading this article : http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js and this How to decide when to use Node.js? to familiarize myself with it and have left me confused.

I am familiar with cpu intensive task like the computation of the Fibonacci series but that's where my understanding ends.

For example, I have a Rest API that does all the computation or recommendation and is housed on a different server from the machine running node, then node.js won't have any trouble with having to deal with cpu intensive task. Just call the api then tell the client that your request is acknowledged.

I can't shake this thinking about comparing node.js with a simple ajax call to send the request from a form to the server, display a ticker then show the result. I am guessing that node.js is a web server, doing lot's of "ajax" type calls and handling concurrent connections.

Are my assumptions correct?

Is it also correct to assume that retrieving data from a database is an io operation but creating a complex report from that data a cpu intensive one?

Community
  • 1
  • 1
Francis Zabala
  • 1,037
  • 2
  • 11
  • 30

3 Answers3

2

You are right about handling many ajax requests, however thats true in worker based model also (php/python workers threads)

Main difference for event based system there will be only one worker doing all sorts of computation part of code (such as filtering data, adding processing etc). When it calls io ops like read from file, or db etc. node doesn't have control over that, instead of waiting on that to finish it puts a call back in the queue and moves on with next processing in queue (if any).

For analogy think of pizza outlet, if only one person is taking order and handing over the order to kitchen, once its ready cutting it, packing and giving it to customer. Where ever there is wait, he just moves on to next task. This is what node does, that person wont hang-on next to kitchen until pizza gets cooked.

In case of worker based approach think of a bank teller and you see couple of them (may be 5 or so) they take every kind of request but they dont switch between customer / request.

Dileep
  • 775
  • 5
  • 20
  • Ah, so scale the worker based model, you have to add lots of office space or in servers, RAM. In the pizza outlet model, what happens if the pizza gets burnt? What would node do? – Francis Zabala Jun 26 '15 at 06:57
  • Idea here is kitchen is slower than other processes, so you dont need person per request. If kitchen can produce what one person can handle, one can add another staff here also. Its perfectly fine to have multiple nodejs processes when you have multicore machines. – Dileep Jun 26 '15 at 09:37
1

Refer to these resources for a deeper understanding of how JavaScript event loop works.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

http://latentflip.com/loupe/

Raghavendra
  • 5,281
  • 4
  • 36
  • 51
0

I can't answer all your doubts, but would like you to have some clarity over AJAX. AJAX - Asynchronous JavaScript + XML is a technique to make requests to a server. Nodejs server knows how to handle such requests, but saying that is the only thing it can do is absolutely wrong. Nodejs is single threaded, hence async. Whether it is good for CPU intensive tasks, I would say why not, unless you want to solve issues in a multithreaded fashion.

Pbd
  • 1,219
  • 1
  • 15
  • 32