Intermediate LevelNode JS
How does libuv thread pool sizing (UV_THREADPOOL_SIZE) impact async file I/O and crypto operations?
❓ Interview Prompt: Explain libuv default thread pool (size 4), operations that use thread pool (fs, crypto, zlib, dns.lookup), and tuning `UV_THREADPOOL_SIZE`.
libuv maintains an internal thread pool (default 4 threads) to handle operations that OS kernels cannot execute asynchronously:
Thread Pool Operations:
•
File I/O ('fs.*')
•
Crypto ('crypto.pbkdf2', 'crypto.scrypt')
•
Compression ('zlib.*')
•
Hostname Resolution ('dns.lookup')
Tuning:
Set 'process.env.UV_THREADPOOL_SIZE = 128' (max 128) BEFORE invoking any async operations to eliminate thread contention bottlenecks under high concurrency!
Code Implementation Example
javascript
// MUST set before importing or running any async libuv operations!
process.env.UV_THREADPOOL_SIZE = '16';
import crypto from 'node:crypto';
// Executes across 16 parallel threads in libuv threadpool
for (let i = 0; i < 16; i++) {
crypto.pbkdf2('pass', 'salt', 100000, 512, 'sha512', () => {
console.log('Operation ${i} complete');
});
}💡Tuning UV_THREADPOOL_SIZE for parallel crypto operations.
🎯 Interview Key Takeaway
Increase UV_THREADPOOL_SIZE (up to 128) when your application executes heavy parallel file I/O or crypto operations.
#libuv#UV_THREADPOOL_SIZE#ThreadPool#Performance#Intermediate
Back to all Node JS Questions→