Node.js: Why Async File Calls Get Stuck
Outline
Transcript
0:00 Node.js, which runs JavaScript outside the browser, can keep running your code while an async file call waits for its answer. The request that needs the file can still be slow. We'll trace that wait, then change one setting to see what moves. Take our local demo. We asked when a file was last modified. The timer fired after 28 milliseconds. That little piece of file information came back after 176 milliseconds. We followed the Node.js source and the source for libuv, the library handling the background work.
0:33 The source links are in the description. My first thought was to blame the disk. Could the request be waiting before it even reaches the disk? Before that lookup can run, it needs a worker. A worker runs one job at a time in the background. We sent four matching password calculations first, then used stat to get the file's information. Five jobs for four workers. Those workers make up the worker pool. Meanwhile, the event loop takes ready JavaScript code and gives it a turn. For example, the timer can fire while all four workers are busy.
1:09 Our stat call gives JavaScript a promise it can use to get the file information when it's ready. That explains the timer. The part we had to trace was how password calculations could hold up a request for a file's date. I'd expect the file to go straight through if it had its own workers. Where do the requests meet? Follow the file path on screen into libuv. First, Node hands stat to the part of the library that asks the operating system, the software that manages the machine's files, about the file.
1:42 The password calculation comes in through a different part of the code. Follow both arrows down. They meet in the queue, the line of jobs waiting for an available worker. For instance, stat is job five. Every worker can be busy while the program goes on. We asked for a date and got in line behind four password calculations. If you assume an async job starts as soon as you call it, it's easy to look in the wrong place. I didn't realize how long the wait could be. Once a worker finishes stat, how does it deliver the answer?
2:17 The answer comes back through the loop. When one password job finishes, its worker can take stat and look up when the file last changed. Libuv signals the loop that made the request, where Node delivers the result through the promise. Look at the first password result and the file result in the opening run. Both show up at the same rounded millisecond. Did the file lookup itself take that long? We timed when JavaScript got the answer. That includes waiting for a worker, looking up the file, and getting the result back.
2:52 The timer ran before we saw either result. So to test where the delay comes from, we ran the same program with workers to spare. Same password jobs and same file. We kept the timer for the next run. Then we restarted Node with eight workers. Pool size was the only change. We sent it the same four password jobs. Across five runs on each side, the timer still fired in the high twenties. The file went from over 160 milliseconds every time to about one. Zero on the chart means the time rounded down.
3:26 So the file's delay moves and the timer barely changes. That gives us a way to test what made the call wait without guessing about a slow disk. I'd want that same distinction if a live service suddenly started taking longer to answer. What would you measure to tell where the time went? First I'd measure event-loop delay, how late JavaScript gets its turn. Then I'd time the whole file call. Imagine a login service with a password calculation on each of four workers. Would limiting it to three at once help?
4:01 We tried it on our machine. When we sent three password jobs to four workers, the file's details came back in about a millisecond each time. On a busy server, another job could still use that 4th worker. So I'd measure how long logins take as well. The file could come back quicker while people wait longer to log in. A separate running copy of Node gives the password jobs their own pool, and both copies still share the machine's processor and memory. Thanks for listening to Learning Podcasts.