Search code examples
javascriptnode.jsasynchronousreal-time

Do Asynchronous Queries In Node.js Help Overcome Latency In Real-Time Data Stream?


New to Node.js and am researching real-time data stream using node.js - I am aware that real-time data stream is a major reason why node.js was developed. May someone either answer or provide an article to if Asynchronous Queries help overcome latency in real-time data streaming?


Solution

  • One of the ideas behind node.js is that the server should never block waiting for I/O (networking, disk reads/writes, hardware interfacing, etc...). To make this a reality in node.js programming, one must always use asynchronous interfaces for doing any of these potentially time consuming operations.

    There are a number of advantages to having a server written to work this way and then writing code that uses it that way in that the core server process itself is relatively highly available (because it never calls functions that could potentially block for awhile). So, a single server process can have a lot of things going on at once.

    Other servers that allow blocking I/O find that they would be horribly unresponsive unless they implement multiple threads (so they too can have multiple requests being worked on at the same time). Even though a given CPU is only working on one thing at a time, when you have multiple threads, you could have 9 threads all blocked on some sort of I/O and the 10th thread is actually returning data to a request.

    What folks have found is that the node.js way has some scale advantages for certain types of operations because async programming is ultimately a lower resource method of serving lots of requests all doing I/O, then a method that requires a bunch of threads. There are other advantages too (such as much simpler synchronization between non-threaded requests in node.js vs. threaded requests in other servers).

    There are plenty of disadvantages to the node.js model too (programming burden of writing async code, unfair scheduling model, etc...).


    OK, all of that is background to your original question:

    May someone either answer or provide an article to if Asynchronous Queries help overcome latency in real-time data streaming?

    The answer is sort of yes. If you have a server that does a lot of I/O (and I'm assuming that "real-time data streaming" probably qualifies as heavy I/O) then the asynchronous I/O model can be more scalable than the threaded synchronous model. It shifts some of the programming burden onto the developer, but reaps some benefits from that. I said "sort of" because there are no absolutes here. Everything depends upon the particulars of the situation and exactly what is being done and how its being done.

    But, if I needed to do near real-time data streaming to a large number of clients, I'd probably use a cluster of servers that use an async I/O model like node.js does.

    Note also that you don't have to use node.js to do async I/O programming. There is the ability to do that type of development in some other environments too. But, it is a core characteristic of the node.js design.