Bird
0
0

Consider this Node.js cluster code snippet:

medium📝 component behavior Q13 of 15
Node.js - Cluster Module
Consider this Node.js cluster code snippet:
const cluster = require('cluster');
const http = require('http');

if (cluster.isMaster) {
  cluster.fork();
  cluster.fork();
} else {
  http.createServer((req, res) => {
    res.end(`Worker ${process.pid} handled request`);
  }).listen(8000);
}
What will happen when you send multiple requests to port 8000?
ARequests will be handled by both worker processes, distributing load
BOnly the first worker will handle all requests, the second is idle
CThe master process will handle requests directly
DThe server will crash because multiple workers listen on the same port
Step-by-Step Solution
Solution:
  1. Step 1: Understand cluster.fork() behavior

    Calling cluster.fork() creates worker processes that share the same server port.
  2. Step 2: Analyze request handling in workers

    Both workers listen on port 8000 and Node.js load balances requests between them automatically.
  3. Final Answer:

    Requests will be handled by both worker processes, distributing load -> Option A
  4. Quick Check:

    Multiple workers share port and balance requests [OK]
Quick Trick: Multiple forks share port and balance requests [OK]
Common Mistakes:
  • Thinking only one worker handles all requests
  • Assuming master handles requests directly
  • Believing server crashes due to port sharing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes