Which statement best explains the main difference between using Node.js cluster module and a reverse proxy like Nginx for handling multiple requests?
Think about where the work happens: inside Node.js or outside it.
The cluster module creates multiple Node.js processes to utilize multiple CPU cores, improving concurrency within the same machine. A reverse proxy like Nginx forwards incoming requests to one or more backend servers but does not create Node.js processes itself.
Given the following Node.js cluster code, what will be the output when you run it on a machine with 2 CPU cores?
import cluster from 'cluster';
import os from 'os';
if (cluster.isPrimary) {
const cpuCount = os.cpus().length;
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
console.log(`Primary process started ${cpuCount} workers.`);
} else {
console.log(`Worker process ${process.pid} started.`);
}Remember that cluster.fork() creates a new worker process for each CPU core.
The primary process forks one worker per CPU core. Each worker logs its own start message. So you see one primary message and two worker messages on a 2-core machine.
Consider a reverse proxy setup with Nginx forwarding requests to two Node.js servers running on ports 3000 and 3001. If Nginx uses round-robin load balancing, what will be the sequence of server ports handling 4 consecutive requests?
Round-robin means cycling through servers one by one.
Round-robin load balancing sends each new request to the next server in the list, cycling through them evenly.
Which option correctly sets up a Node.js cluster that logs worker IDs when they start?
import cluster from 'cluster'; import os from 'os'; if (cluster.isPrimary) { const cpuCount = os.cpus().length; for (let i = 0; i < cpuCount; i++) { cluster.fork(); } } else { console.log(`Worker ${cluster.worker.id} started.`); }
Check the current Node.js cluster API for primary process detection.
In Node.js 20+, cluster.isPrimary replaces cluster.isMaster. The code uses the correct property and syntax.
You have a reverse proxy (Nginx) forwarding requests to a Node.js server on port 4000. The proxy returns 502 Bad Gateway errors. Which of the following is the most likely cause?
502 Bad Gateway means the proxy cannot reach the backend server.
A 502 error usually means the reverse proxy cannot connect to the backend server. If the Node.js server is not running or listening on the expected port, Nginx cannot forward requests.