Bird
Raised Fist0
Node.jsframework~10 mins

Cluster vs reverse proxy decision in Node.js - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a cluster that forks worker processes.

Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < [1]; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Hello from worker ' + process.pid);
  }).listen(8000);
}
Drag options to blanks, or click blank then click option'
Aprocess
Bcluster
Chttp
DnumCPUs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cluster' or 'http' instead of the CPU count variable.
2fill in blank
medium

Complete the code to set up a simple reverse proxy using the 'http-proxy' library.

Node.js
const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});

const server = http.createServer((req, res) => {
  proxy.web(req, res, { target: [1] });
});

server.listen(8000);
Drag options to blanks, or click blank then click option'
A'http://localhost:8000'
B'https://example.com'
C'http://localhost:3000'
D'http://127.0.0.1:8080'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same port as the proxy server or an unrelated URL.
3fill in blank
hard

Fix the error in the cluster code to properly listen on a port in each worker.

Node.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Worker ' + process.pid);
  }).listen([1]);
}
Drag options to blanks, or click blank then click option'
A'8000'
B8000
Cprocess.pid
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.pid or a string instead of a number for the port.
4fill in blank
hard

Fill both blanks to create a cluster that logs when a worker exits and forks a new one.

Node.js
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  for (let i = 0; i < [1]; i++) {
    cluster.fork();
  }

  cluster.on('[2]', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
    cluster.fork();
  });
}
Drag options to blanks, or click blank then click option'
AnumCPUs
Bexit
Cdisconnect
Donline
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong event names like 'disconnect' or 'online' for worker death.
5fill in blank
hard

Fill all three blanks to create a reverse proxy that logs requests and forwards them to a backend.

Node.js
const http = require('http');
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer({});

const server = http.createServer((req, res) => {
  console.log('Request URL:', [1]);
  proxy.web(req, res, { target: [2] });
});

server.listen([3]);
Drag options to blanks, or click blank then click option'
Areq.url
B'http://localhost:4000'
C8080
Dreq.headers
Attempts:
3 left
💡 Hint
Common Mistakes
Logging headers instead of URL, wrong target URL, or wrong listen port.

Practice

(1/5)
1. What is the main purpose of using a cluster in a Node.js application?
easy
A. To forward HTTP requests to different servers
B. To use multiple CPU cores by creating worker processes
C. To add security features like SSL termination
D. To cache static files for faster delivery

Solution

  1. Step 1: Understand what a cluster does in Node.js

    A cluster creates multiple worker processes to use all CPU cores efficiently.
  2. Step 2: Compare with other options

    Forwarding requests and adding security are tasks of a reverse proxy, not a cluster.
  3. Final Answer:

    To use multiple CPU cores by creating worker processes -> Option B
  4. Quick Check:

    Cluster = multiple CPU cores [OK]
Hint: Clusters = multiple CPU cores, reverse proxy = request forwarding [OK]
Common Mistakes:
  • Confusing cluster with reverse proxy functions
  • Thinking clusters handle security features
  • Assuming clusters cache files
2. Which of the following is the correct way to create a cluster in Node.js?
easy
A. const cluster = require('cluster'); cluster.fork();
B. const proxy = require('proxy'); proxy.create();
C. const http = require('http'); http.listenCluster();
D. const cluster = require('cluster'); cluster.createServer();

Solution

  1. Step 1: Recall Node.js cluster module usage

    The cluster module is required with require('cluster') and workers are created with cluster.fork().
  2. Step 2: Check other options for correctness

    There is no proxy module by default, http.listenCluster() and cluster.createServer() are invalid methods.
  3. Final Answer:

    const cluster = require('cluster'); cluster.fork(); -> Option A
  4. Quick Check:

    cluster.fork() creates workers [OK]
Hint: Use cluster.fork() to create workers in Node.js [OK]
Common Mistakes:
  • Using non-existent methods like cluster.createServer()
  • Confusing proxy module with cluster
  • Trying to call listenCluster() on http
3. Given this setup: a Node.js app uses a cluster with 4 workers and a reverse proxy in front. What is the main benefit of this combination?
medium
A. The cluster manages security, and the reverse proxy manages CPU usage
B. The cluster handles SSL termination, and the reverse proxy creates workers
C. The reverse proxy caches data, and the cluster forwards requests
D. The reverse proxy balances traffic, and the cluster uses all CPU cores

Solution

  1. Step 1: Understand roles of cluster and reverse proxy

    The cluster allows Node.js to use multiple CPU cores by creating workers. The reverse proxy balances incoming traffic among servers.
  2. Step 2: Eliminate incorrect roles

    SSL termination and security are usually handled by reverse proxies, not clusters. Clusters do not forward requests or cache data.
  3. Final Answer:

    The reverse proxy balances traffic, and the cluster uses all CPU cores -> Option D
  4. Quick Check:

    Cluster = CPU cores, Reverse proxy = traffic balance [OK]
Hint: Cluster for CPU, reverse proxy for traffic control [OK]
Common Mistakes:
  • Swapping roles of cluster and reverse proxy
  • Thinking cluster handles SSL or security
  • Assuming reverse proxy creates workers
4. You wrote this code snippet to create a cluster but it crashes immediately:
const cluster = require('cluster');
cluster.fork();
require('http').createServer((req, res) => res.end('Hello')).listen(3000);
What is the likely cause?
medium
A. The worker process does not listen on a port
B. Missing a callback function in cluster.fork()
C. Not checking cluster.isMaster before forking
D. No error handling for server creation

Solution

  1. Step 1: Analyze cluster usage

    The code calls cluster.fork() without checking if (cluster.isMaster). Both master and worker processes fork additional processes and attempt to bind to port 3000, causing port conflicts (EADDRINUSE) and crashes.
  2. Step 2: Identify the problem

    The missing if (cluster.isMaster) check before forking leads to repeated forking and server creation attempts, causing the crash.
  3. Final Answer:

    Not checking cluster.isMaster before forking -> Option C
  4. Quick Check:

    Check cluster.isMaster before fork [OK]
Hint: Always check cluster.isMaster before forking [OK]
Common Mistakes:
  • Calling cluster.fork() without isMaster check
  • Assuming fork needs a callback
  • Ignoring server listen port
5. You want to improve your Node.js app's performance and reliability. You decide to use both a cluster and a reverse proxy. Which setup best achieves this goal?
hard
A. Use a cluster to run multiple workers on all CPU cores, and a reverse proxy to distribute incoming requests and handle SSL
B. Use a reverse proxy to create worker processes, and a cluster to forward requests to other servers
C. Use a cluster to cache static files, and a reverse proxy to manage CPU usage
D. Use a reverse proxy to run multiple Node.js instances, and a cluster to balance traffic

Solution

  1. Step 1: Identify cluster's role in performance

    Clusters run multiple worker processes to use all CPU cores, improving performance and reliability.
  2. Step 2: Identify reverse proxy's role in traffic and security

    Reverse proxies distribute incoming requests, handle SSL termination, and add security features.
  3. Step 3: Evaluate options

    Only Use a cluster to run multiple workers on all CPU cores, and a reverse proxy to distribute incoming requests and handle SSL correctly assigns cluster to CPU usage and reverse proxy to traffic distribution and SSL.
  4. Final Answer:

    Use a cluster to run multiple workers on all CPU cores, and a reverse proxy to distribute incoming requests and handle SSL -> Option A
  5. Quick Check:

    Cluster = CPU workers, Reverse proxy = traffic & SSL [OK]
Hint: Cluster for CPU workers, reverse proxy for traffic & SSL [OK]
Common Mistakes:
  • Assigning reverse proxy to create workers
  • Confusing caching with cluster role
  • Swapping roles of cluster and reverse proxy