0
0
Node.jsframework~20 mins

Load balancing between workers in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Load Balancing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Node.js cluster module distribute incoming connections by default?
Consider a Node.js server using the cluster module with multiple worker processes. How are incoming connections distributed among workers by default?
AThe master process distributes connections in a round-robin fashion to workers.
BEach worker listens on the same port and the OS distributes connections in a round-robin fashion.
CThe master process sends all connections to the first worker until it is busy, then moves to the next.
DWorkers compete to accept connections and the fastest worker gets the connection.
Attempts:
2 left
💡 Hint
Think about how the cluster module balances load to avoid overloading a single worker.
📝 Syntax
intermediate
1:30remaining
Identify the correct way to create a worker in Node.js cluster
Which code snippet correctly creates a worker process using Node.js cluster module?
Node.js
const cluster = require('cluster');
if (cluster.isMaster) {
  // create worker
}
Acluster.newWorker();
Bcluster.createWorker();
Ccluster.fork();
Dcluster.spawn();
Attempts:
2 left
💡 Hint
Check the official cluster module API for the method to create a worker.
🔧 Debug
advanced
2:30remaining
Why does this cluster setup cause uneven load distribution?
Given the code below, why might the load not be balanced evenly across workers? const cluster = require('cluster'); const http = require('http'); if (cluster.isMaster) { for (let i = 0; i < 4; i++) { cluster.fork(); } } else { http.createServer((req, res) => { // simulate heavy work const start = Date.now(); while (Date.now() - start < 100) {} res.end('done'); }).listen(8000); }
AWorkers handle requests independently, but the OS assigns connections randomly, causing uneven load.
BThe blocking while loop in workers causes some workers to be busy longer, leading to uneven load.
CThe master process does not distribute connections; workers compete causing uneven load.
DThe cluster.fork() method is called incorrectly, so only one worker handles all requests.
Attempts:
2 left
💡 Hint
Consider how blocking code affects worker availability and load distribution.
state_output
advanced
1:30remaining
What is the output of this cluster worker count code?
What will be printed when running this code? const cluster = require('cluster'); if (cluster.isMaster) { cluster.fork(); cluster.fork(); console.log(Object.keys(cluster.workers).length); } else { console.log('worker running'); }
Aworker running
B0
Cundefined
D2
Attempts:
2 left
💡 Hint
Remember that cluster.workers is an object with worker IDs as keys.
🧠 Conceptual
expert
3:00remaining
Why might you choose a custom load balancing strategy over Node.js default cluster behavior?
Which reason best explains why a developer might implement a custom load balancing strategy instead of relying on Node.js cluster's default round-robin distribution?
ATo optimize resource usage by directing requests based on worker CPU load or memory usage.
BBecause the default cluster module does not support multiple workers.
CTo avoid using the cluster module entirely and handle all requests in a single process.
DBecause the default cluster module only works on Windows systems.
Attempts:
2 left
💡 Hint
Think about real-world scenarios where some workers might be busier or slower than others.