How Node runs your code · senior
The libuv thread pool
libuv keeps a small pool of threads (4 by default, changed with UV_THREADPOOL_SIZE) for work the operating system cannot do asynchronously, such as file system calls, dns.lookup and CPU-heavy crypto and zlib; network sockets never use it.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Uses the pool: all async fs APIs (except fs.watch), dns.lookup and dns.lookupService, crypto.pbkdf2, crypto.scrypt, crypto.randomBytes/randomFill, crypto.generateKeyPair, and async zlib.
- Does not use the pool: TCP/HTTP sockets, fetch, database drivers' network traffic, dns.resolve* (DNS queries over the network), timers.
- Default size is 4. If 4 slow tasks run (say 4 password hashes), the 5th task waits, and so do unrelated fs.readFile calls. Set UV_THREADPOOL_SIZE in the environment before Node starts.
- Native addons such as bcrypt and argon2 also hash on the pool, so a burst of logins can slow file reads and dns.lookup (which every outgoing HTTP call to a hostname uses).
- The pool runs native work only. Your JavaScript callback still runs on the main thread. CPU work written in JavaScript needs worker_threads.
- Symptom: fs or DNS latency rises under load while CPU is not at 100%. Measure first, then raise the pool size or move the work out.
The red pen
Common traps: mistake → fix
Mistake: All async I/O in Node uses the thread pool
Fix: network I/O uses the OS event mechanisms; the pool is for fs, dns.lookup, crypto and zlib.
Mistake: The thread pool runs my JavaScript callbacks
Fix: it runs the native work; the callback still runs on the main thread.
Mistake: A bigger pool always helps
Fix: beyond the number of CPU cores, CPU-bound pool tasks just compete; size it by measuring.
Easy to hard
Questions interviewers ask, easy to hard
- What is the libuv thread pool and how big is it?
- Which Node APIs use it? Does an HTTP request use it?
- Login (bcrypt) got slow at peak and file downloads slowed too. How could they be linked?
- How do you change the pool size, and when is that the wrong fix?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.