Search code examples
node.jsmultithreadingreadfile

Doubts about event loop , multi-threading, order of executing readFile() in Node.js?


fs.readFile("./large.txt", "utf8", (err, data) => {
console.log('It is a large file') 

//this file has many words (11X MB). 
//It takes 1-2 seconds to finish reading (usually 1)

});

fs.readFile("./small.txt","utf8", (err, data) => {

for(let i=0; i<99999 ;i++)
console.log('It is a small file');

//This file has just one word. 
//It always takes 0 second
}); 

Result:

The console will always first print "It is a small file" for 99999 times (it takes around 3 seconds to finish printing). Then, after they are all printed, the console does not immediately print "It is a large file". (It is always printed after 1 or 2 seconds).

My thought:

So, it seems that the first readFile() and second readFile() functions do not run in parallel. If the two readFile() functions ran in parallel, then I would expect that after "It is a small file" was printed for 99999 times, the first readFile() is finished reading way earlier (just 1 second) and the console would immediately print out the callback of the first readFile() (i.e. "It is a large file".)

My questions are :

(1a) Does this mean that the first readFile() will start to read file only after the callback of second readFile() has done its work?

(1b) To my understanding, in nodeJs, event loop passes the readFile() to Libuv multi-thread. However, I wonder in what order they are passed. If these two readFile() functions do not run in parallel, why is the second readFile() function always executed first?

(2) By default, Libuv has four threads for Node.js. So, here, do these two readFile() run in the same thread? Among these four threads, I am not sure whether there is only one for readFile().

Thank you very much for spending your time! Appreciate!


Solution

  • (1a) Does this mean that the first readFile() will start to read file only after the callback of second readFile() has done its work?

    No. Each readFile() actually consists of multiple steps (open file, read chunk, read chunk ... close file). The logic flow between steps is controlled by Javascript code in the node.js fs library. But, a portion of each step is implemented by native threaded code in libuv using a thread pool.

    So, the first step of the first readFile() will be initiated and then control is returned back to the JS interpreter. Then, the first step of the second readFile() will be initiated and then control returned back to the JS interpreter. It can ping pong back and forth between progress in the two readFile() operations as long as the JS interpreter isn't kept busy. But, if the JS interpreter does get busy for awhile, it will stall further progress when the current step that's proceeding in the background completes. There's a full step-by-step chronology at the end of the answer if you want to follow the details of each step.

    (1b) To my understanding, in nodeJs, event loop passes the readFile() to Libuv multi-thread. However, I wonder in what order they are passed. If these two readFile() functions do not run in parallel, why is the second readFile() function always executed first?

    fs.readFile() itself is not implemented in libuv. It's implemented as a series of individual steps in node.js Javascript. Each individual step (open file, read chunk, close file) is implemented in libuv, but Javascript in the fs library controls the sequencing between steps. So, think of fs.readfile() as a series of calls to libuv. When you have two fs.readFile() operations in flight at the same time, each will have some libuv operation going at any given time and one step for each fs.readFile() can be proceeding in parallel due to the thread pool implementation in libuv. But, between each step in the process, control comes back the JS interpreter. So, if the interpreter gets busy for some extended portion of time, then further progress in scheduling the next step of the other fs.readFile() operation is stalled.

    (2) By default, Libuv has four threads for Node.js. So, here, do these two readFile() run in the same thread? Among these four threads, I am not sure whether there is only one for readFile().

    I think this is covered in the previous two explanations. readFile() itself is not implemented in native code of libuv. Instead, it's written in Javascript with calls to open, read, close operations that are written in native code and use libuv and the thread pool.

    Here's a full accounting of what's going on. To fully understand, one needs to know about these:

    Main Concepts

    1. The single threaded, non-pre-emptive nature of node.js running your Javascript (assuming no WorkerThreads are manually coded here - which they aren't).
    2. The multi-threaded, native code of the fs module's file I/O and how that works.
    3. How native code asynchronous operations communicate completion via the event queue and how event loop scheduling works when the JS interpreter is busy doing something.

    Asynchronous, Non-Blocking

    I presume you know that fs.readFile() is asynchronous and non-blocking. That means when you call it, all it does is initiate an operation to read the file and then it goes right onto the next line of code at the top level after the fs.readFile() (not the code inside the callback you pass to it).

    So, a condensed version of your code is basically this:

    fs.readFile(x, funcA);
    fs.readFile(y, funcB);
    

    If we added some logging to this:

    function funcA() {
        console.log("funcA");
    }
    function funcB() {
        console.log("funcB");
    }
    
    function spin(howLong) {
        let finishTime = Date.now() + howLong;
        // spin until howLong ms passes
        while (Date.now() < finishTime) {}
    }
    
    console.log("1");
    fs.readFile(x, funcA);
    console.log("2");
    fs.readFile(y, funcB);
    console.log("3");
    spin(30000);         // spin for 30 seconds    
    console.log("4");
    

    You would see either this order:

    1
    2
    3
    4
    A
    B
    

    or this order:

    1
    2
    3
    4
    B
    A
    

    Which of the two it was would just depend upon the indeterminate race between the two fs.readFile() operations. Either could happen. Also, notice that 1, 2, 3 and 4 are all logged before any asynchronous completion events can occur. This is because the single-threaded, non-pre-emptive JS interpreter main thread is busy executing Javascript. It won't pull the next event out of the event queue until it's done executing this piece of Javascript.

    Libuv Thread Pool

    As you appear to already know, the fs module uses a libuv thread pool for running file I/O. That's independent of the main JS thread so those read operations can proceed independently from further JS execution. Using native code, file I/O will communicate with the event queue when they are done to schedule their completion callback.

    Indeterminate Race Between Two Asynchronous Operations

    So, you've just created an indeterminate race between the two fs.readFile() operations that are likely each running in their own thread. A small file is much more likely to complete first before the larger file because the larger file has a lot more data to read from the disk.

    Whichever fs.readFile() finishes first will insert its callback into the event queue first. When the JS interpreter is free, it will pick the next event out of the event queue. Whichever one finishes first gets to run its callback first. Since the small file is likely to finish first (which is what you are reporting), it gets to run its callback. Now, when it is running its callback, this is just Javascript and even though the large file may finish and insert its callback into the event queue, that callback can't run until the callback from the small file finishes. So, it finishes and THEN the callback from the large file gets to run.

    In general, you should never write code like this unless you don't care at all what order the two asynchronous operations finish in because it's an indeterminate race and you cannot count on which one will finish first. Because of the asynchronous non-blocking nature of fs.readFile(), there is no guarantee that the first file operation initiated will finish first. It's no different than firing off two separate http requests one after the other. You don't know which one will complete first.

    Step By Step Chronology

    Here's a step by step chronology of what happens:

    1. You call fs.readFile("./large.txt", ...);
    2. In Javascript code, that initiates opening the large.txt file by calling native code and then returns. The opening of the actual file is handled by libuv in native code and when that is done, an event will be inserted into the JS event queue.
    3. Immediately after that operation is initiated, then that first fs.readFile() returns (not yet done yet, still processing internally).
    4. Now the JS interpreter picks up at the next line of code and runs fs.readFile("./small.txt", ...);
    5. In Javascript code, that initiates opening the small.txt file by calling native code and then returns. The opening of the actual file is handled by libuv in native code and when that is done, an event will be inserted into the JS event queue.
    6. Immediately after that operation is initiated, then that second fs.readFile() returns (not yet done yet, still processing internally).
    7. The JS interpreter is actually free to run any following code or process any incoming events.
    8. Then, some time later, one of the two fs.readFile() operations finishes its first step (opening the file), an event is inserted into the JS event queue and when the JS interpreter has time, a callback is called. Since opening each file is about the same operation time, it's likely that the open operation for the large.txt file finishes first, but that isn't guaranteed.
    9. After the file open succeeds, it initiates an asynchronous operation to read the first chunk from the file. This again is asynchronous and is handled by libuv so as soon as this is initiated, it returns control back to the JS interpreter.
    10. The second file open likely finises next and it does the same thing as the first, initiates reading the first chunk of data from disk and returns control back to the JS interpreter.
    11. Then, one of these two chunk reads finishes and inserts an event into the event queue and when the JS interpreter is free, a callback is called to process that. At this point, this could be either the large or small file, but for purposes of simplicity of explanation, lets assume the first chunk of the large file finishes first. It will buffer that chunk, see that there is more data to read and will initiate another asynchronous read operation and then return control back to the JS interpreter.
    12. Then, the other first chunk read finishes. It will buffer that chunk and see that there is no more data to read. At this point, it will issue a file close operation which is again handled by libuv and control is returned back to the JS interpreter.
    13. One of the two previous operations completes (a second block read from large.txt or a file close of small.txt) and its callback is called. Since the close operation doesn't have to actually touch the disk (it just goes into the OS), let's assume the close operation finishes first for purposes of explanation. That close triggers the end of the fs.ReadFile() for small.txt and calls the completion callback for that.
    14. So, at this point, small.txt is done and large.txt has read one chunk from its file and is awaiting completion of the second chunk to read.
    15. Your code now executes the for loop that takes whatever time that takes.
    16. By the point that finishes and the JS interpreter is free again, the 2nd file read from large.txt is probably done so the JS interpreter finds it's event in the event queue and executes a callback to do some more processing on reading more chunks from that file.
    17. The process of reading a chunk, returning control back to the interpreter, waiting for the next chunk completion event and then buffering that chunk continues until all the data has been read.
    18. Then a close operation is initiated for large.txt.
    19. When that close operation is done, the callback for the fs.readFile() for large.txt is called and your code that is timing large.txt will measure completion.

    So, because the logic of fs.readFile() is implemented in Javascript with a number of discrete asynchronous steps with each one ultimately handled by libuv (open file, read chunk - N times, close file), there will be an interleaving of the work between the two files. The reading of the smaller file will finish first just because it has fewer and smaller read operations. When it finishes, the large file will still have multiple more chunks to read and a close operation left. Because the multiple steps of fs.readFile() are controlled through Javascript, when you do the long for loop in the small.txt completion, you are stalling the fs.readFile() operation for the large.txt file too. Whatever chunk read was in progress when that loop happened will complete in the background, but the next chunk read won't get issued until that small file callback completes.

    It appears that there would be an opportunity for node.js to improve the responsiveness of fs.readFile() in competitive circumstances like this if that operation was rewritten entirely in native code so that one native code operation could read the contents of the whole file rather than all these transitions back and forth between the single threaded main JS thread and libuv. If this was the case, the big for loop wouldn't stall the progress of large.txt because it would be progressing entirely in a libuv thread rather than waiting for some cycles from the JS interpreter in order to get to its next step.

    We can theorize that if both files were able to be read in one chunk, then not much would get stalled by the long for loop. Both files would get opened (which should take approximately the same time for each). Both operations would initiate a read of their first chunk. The read for the smaller file would likely complete first (less data to read), but actually this depends upon both OS and disk controller logic. Because the actual reads are handed off to threaded code, both reads will be pending at the same time. Assuming the smaller read finishes first, it would fire completion and then during the busy loop the large read would finish, inserting an event in the event queue. When the busy loop finishes, the only thing left to do on the larger file (but still something that can was read in one chunk) would be to close the file which is a faster operation.

    But, when the larger file can't be read in one chunk and needs multiple chunks of reading, that's why its progress really gets stalled by the busy loop because a chunk finishes, but the next chunk doesn't get scheduled until the busy loop is done.

    Testing

    So, let's test out all this theory. I created two files. small.txt is 558 bytes. large.txt is 255,194,500 bytes.

    Then, I wrote the following program to time these and allow us to optionally do a 3 second spin loop after the small one finishes.

    const fs = require('fs');
    
    let doSpin = false;              // -s will set this to true
    let fname = "./large.txt";      
    
    for (let i = 2; i < process.argv.length; i++) {
        let arg = process.argv[i];
        console.log(`"${arg}"`);
        if (arg.startsWith("-")) {
            switch(arg) {
                case "-s":
                    doSpin = true;
                    break;
                default:
                    console.log(`Unknown arg ${arg}`);
                    process.exit(1);
                    break;
            }
        } else {
            fname = arg;
        }
    }
    
    function padDecimal(num, n = 3) {
        let str = num.toFixed(n);
        let index = str.indexOf(".");
        if (index === -1) {
            str += ".";
            index = str.length - 1;
        }
        let zeroesToAdd = n - (str.length - index);
        while (zeroesToAdd-- >= 0) {
            str += "0";
        }
    
        return str;
    }
    
    let startTime;
    function log(msg) {
        if (!startTime) {
            startTime = Date.now();
        }
        let diff = (Date.now() - startTime) / 1000;    // in seconds
        console.log(padDecimal(diff), ":", msg)
    }
    
    
    function funcA(err, data) {
        if (err) {
           log("error on large");
           log(err);
           return;
        }
        log("large completed");
    }
    
    function funcB(err, data) {
        if (err) {
           log("error on small");
           log(err);
           return;
        }
        log("small completed");
        if (doSpin) {
            spin(3000);
            log("spin completed");
        }
    }
    
    function spin(howLong) {
        let finishTime = Date.now() + howLong;
        // spin until howLong ms passes
        while (Date.now() < finishTime) {}
    }
    
    log("start");
    fs.readFile(fname, funcA);
    log("large initiated");
    fs.readFile("./small.txt", funcB);
    log("small initiated");
    

    Then (using node v12.13.0), I ran it both with and without the 3 second spin. Without the spin, I get this output:

    0.000 : start
    0.015 : large initiated
    0.016 : small initiated
    0.021 : small completed
    0.240 : large completed
    

    This shows a 0.219 second delta between the time to complete small and large (while running both at the same time).

    Then, inserting the 3 second delay, we get this output:

    0.000 : start
    0.003 : large initiated
    0.004 : small initiated
    0.009 : small completed
    3.010 : spin completed
    3.229 : large completed
    

    We have the exact same 0.219 second delta between the time to complete the small and the large (while running both at the same time). This shows that the large fs.readFile() essentially made no progress during the 3 second spin. It's progress was completely blocked. As we've theorized in the previous explanation, this is apparently because the progression from one chunked read to the next is written in Javascript and while the spin loop is running, that progression to the next chunk is blocked so it can't make any further progress.

    How Big A File Makes Big File Finish Second?

    If you look in the code for fs.readFile() in the source for node v12.13.0, you can find that the chunk size it reads is 512 * 1024 which is 512k. So, in theory, it's possible that the larger file might finish first if it can be read in one chunk. Whether that actually happens or not depends upon some OS and disk implementation details, but I thought I'd try it on my laptop running a current version of Windows 10 with an SSD drive.

    I found that, for a 255k "large" file, it does finish before the small file (essentially in execution order). So, because the large file read is started before the small file read, even though it has more data to read, it will still finish before the small file.

    0.000 : start
    0.003 : large initiated
    0.003 : small initiated
    0.007 : large completed
    0.008 : small completed
    

    Keep in mind, this is OS and disk dependent so this is not guaranteed.