
Set 4 is about making a Node service fast and keeping it healthy when traffic goes up. It covers the libuv threadpool, worker threads and cluster, stream backpressure, hunting memory leaks, caching with Redis and CPU profiling. Each explanation tells you what is really going on inside the process.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Topics covered in this set
- libuv threadpool and UV_THREADPOOL_SIZE
- worker_threads, transferable objects and SharedArrayBuffer
- cluster and sticky sessions
- write(), drain and highWaterMark
- Memory leaks, heap snapshots and heap limits
- Redis caching, stampedes and CPU profiling
Node.js MCQs with answers and explanations
Q1. What is the default size of the libuv threadpool in Node.js?
a) 1
b) 4
c) The number of CPU cores
d) 16
Q2. Which of these operations does NOT use the libuv threadpool?
a) Reading data from a TCP socket in an HTTP server
b) crypto.pbkdf2
c) fs.readFile
d) zlib.gzip
Q3. Eight crypto.pbkdf2 calls of equal cost are started at once with the default threadpool. What timing pattern do you expect?
a) Four finish at about time T, then the other four at about 2T
b) All eight finish at the same time
c) They block the event loop until all are done
d) They finish one by one at equal intervals
Q4. What happens to an ArrayBuffer listed in the transfer list of worker.postMessage(buf, [buf])?
a) It is converted to a string
b) Ownership moves to the worker without copying, and the buffer becomes unusable (detached) in the sender
c) It is shared by both threads
d) It is deep-copied to the worker
Q5. When two worker threads share a SharedArrayBuffer, what should they use to update shared values safely?
a) process.nextTick
b) setTimeout
c) JSON.stringify
d) The Atomics API
Q6. How does the cluster module differ from worker_threads?
a) They are identical
b) worker_threads share the server port, cluster does not
c) cluster runs separate processes with separate memory that share a server port, while worker_threads run threads inside one process
d) cluster only works on Windows
Q7. A Socket.IO app is run with cluster and clients keep reconnecting. What is the usual cause?
a) Requests from one client reach different workers, so sticky sessions and a shared adapter (such as Redis) are needed
b) Socket.IO does not support Node.js
c) The threadpool is too small
d) highWaterMark is too low
Q8. What should code do when writable.write(chunk) returns false?
a) Nothing; false means success
b) Destroy the stream
c) Retry the same chunk immediately
d) Stop writing and wait for the "drain" event before writing more
Q9. Which statement about a stream's highWaterMark is correct?
a) It is a hard limit; writes beyond it are dropped
b) It only applies to HTTP streams
c) It is a threshold for buffering that controls when write() returns false and when reading pauses
d) It sets the maximum file size
Q10. Node prints "MaxListenersExceededWarning: 11 listeners added". What does this usually indicate?
a) Listeners are being added repeatedly, often per request, without being removed, which is a common memory leak
b) The CPU is overloaded
c) Too many HTTP routes
d) A crash is about to happen
Q11. What is a reliable way to find which objects are leaking in a Node.js process?
a) Restart the server more often
b) Increase the threadpool size
c) Take heap snapshots before and after some load and compare them to see which objects keep growing
d) Run console.log on every object
Q12. What does --max-old-space-size=4096 do?
a) Limits the process to 4096 objects
b) Raises the V8 old-generation heap limit to about 4 GB
c) Fixes memory leaks automatically
d) Sets the Redis cache size
Q13. In the cache-aside pattern with Redis, what happens on a cache miss?
a) The cache is cleared
b) The request fails
c) Redis queries the database itself
d) The app reads from the database, stores the result in Redis with a TTL, and returns it
Q14. A popular cache key expires and hundreds of requests hit the database at once. What is this called and how is it reduced?
a) Cache stampede; use request coalescing or a lock so one request rebuilds the value, and add jitter to TTLs
b) N+1 problem; add an index
c) Backpressure; add more streams
d) Event loop starvation; use setImmediate
Q15. In a CPU flame graph from node --cpu-prof or Clinic Flame, what does a wide bar mean?
a) The function has many lines of code
b) The function was called recently
c) The function threw an error
d) The function (with its callees) spent a large share of CPU time
Quick revision notes
- The libuv threadpool has 4 threads by default. Network sockets do not use it.
- Transfer ArrayBuffers to avoid copying. For shared memory, use SharedArrayBuffer with Atomics.
- cluster means separate processes sharing one port. Real-time apps need sticky sessions.
- When write() returns false, stop and wait for drain.
- Compare heap snapshots to find leaks. Cache-aside with TTL jitter helps avoid stampedes.
Frequently asked questions
Should I increase UV_THREADPOOL_SIZE in production?
Only if profiling shows threadpool work (file I/O, dns.lookup, crypto) is waiting in a queue. A bigger pool uses more memory, and the extra threads fight for the same cores.
How do I know if the event loop is blocked?
Measure event loop delay with perf_hooks.monitorEventLoopDelay or a monitoring tool. If the delay stays high, some synchronous code is holding up requests.
Is an in-memory cache in Node.js a bad idea?
A small cache with a size limit (like an LRU) is fine. A Map with no limit grows forever and is a common leak. Also, it is not shared between cluster workers.
Comments
Post a Comment