CodePark/ questions / Runtime, V8 & libuv
START PRACTICINGPRACTICE
Backend question · Node.js › Runtime, V8 & libuv

What is UV_THREADPOOL_SIZE, and when would you change it?

UV_THREADPOOL_SIZE is an environment variable that sets the size of libuv's thread pool. libuv is the C library that gives Node its event loop and async I/O. The pool has 4 threads by default, and the whole process shares it. The pool runs this work:

  • file system operations
  • dns.lookup
  • async crypto, such as pbkdf2 and scrypt
  • zlib compression

Change it when an app does a lot of this work at the same time. For example, the app hashes passwords with scrypt, reads files and resolves hostnames at once. Then requests wait in a queue behind each other, even though the event loop is idle. A bigger pool lets more of that work run in parallel. For example: UV_THREADPOOL_SIZE=16 node server.js.

Set it in the environment before the process starts, because Node creates the pool the first time it is used.

Two limits:

  • More threads than CPU cores does not speed up CPU-bound work.
  • It does nothing for network I/O, because network I/O never uses the pool.
  • The default pool size is 4, and the whole process shares the pool.
  • It affects fs, dns.lookup, async crypto and zlib.
  • A full pool makes work wait in a queue, even when the event loop is idle.
  • Set it before startup, because the pool is created lazily on first use.
  • It has no effect on network sockets.