Question

Please correct me if i'm wrong but, I am developing in Ruby for a while and one of it's most obvious disadvantages is the lack of multithreading. Lately I have been exploring node js especially for it's non blocking IO "feature". My questions are:

  1. Why use A single threaded language and use rabbitmq to achieve asynchronously when you can just use a a multi threading language like node js that you can fire fire a 1000 requests in parallel and they all get done simultaneously?
  2. Is there any innovation in node js compare to other multi threaded languages? I guess what i'm really asking is : Is there a difference between node js non blocking IO and multi threading?
Was it helpful?

Solution

nodeJS is not multi-threaded. It is single-threaded, but IO happens outside of that single thread. One way to catchphrase it: "Everything is parallel but your code".

So node is simple - there is only one thread. Only one line of code is executed at a time. However, IO operations (reading from disk, etc) are indeed 'nonblocking', so your code continues running while the IO operation is happening. When the IO is over, the callback you supplied the IO operation is executed.

To answer your questions explicitly:

  1. Using Node, your 1000 requests will not exactly be handled simulatenously -- but the IO part might (the OS will do that, outside your code). The part running in your actual application will still be very much sequential, but the return order will depend on the order of finishing the IO, which is not in your hands.

    More importantly, if you use a messaging scheme to perform asynch operations (after finishing the request) you perform background tasks in a separate process than your main, 'listening' process. Thus if the 'background' process crashes, your listening process is still alive.

  2. As above - Node has a single thread which is nonblocking. In a multithreaded language, your code will be in parallel (thus you might have to deal with exciting stuff like thread safety), but each request will block until any IO it performs is done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top