Skip to main content

What does round-robin scheduling do?

What Round-Robin Scheduling is

Round-Robin is an algorithm where tasks, requests or users are served in turn, evenly and cyclically, with no priority.

In other words:

"everyone gets a turn".

An example of how it works

Imagine you have 3 servers:

javascript
Server A Server B Server C

And a stream of requests arrives: R1, R2, R3, R4, R5, R6, ...

With Round-Robin, they distribute like this:

javascript
R1Server A R2Server B R3Server C R4Server A R5Server B R6Server C and so on

After the last server, the queue wraps back to the first, a "round".

1. Where Round-Robin is used

AreaApplication
Load balancingDistributes HTTP requests across servers
Operating systemsA task scheduler for processes and CPU threads
Node.js Cluster / PM2Distributes incoming connections across workers
Network load balancersNGINX, HAProxy, AWS ELB, and others
Databases and queuesEvenly spreads load across replicas

2. How Round-Robin works in Node.js Cluster

Node.js uses Round-Robin when balancing across workers (cluster or PM2):

  1. The master process listens for incoming connections (for example, HTTP port 3000).
  2. Every new request goes to the next worker in line.
  3. Once the queue reaches the end, the cycle starts over.

So:

Every worker gets roughly the same number of requests, which spreads CPU load evenly.

Example:

javascript
import cluster from 'cluster'; import http from 'http'; import os from 'os'; if (cluster.isPrimary) { const numCPUs = os.cpus().length; console.log(`Master ${process.pid} starting ${numCPUs} workers`); for (let i = 0; i < numCPUs; i++) cluster.fork(); } else { http.createServer((req, res) => { res.end(`Response from worker ${process.pid}`); }).listen(3000); }

Node.js distributes requests across workers round-robin on its own. For example:

javascript
Request 1PID 1001 Request 2PID 1002 Request 3PID 1003 Request 4PID 1001

3. Round-Robin in Nginx

nginx.conf

javascript
upstream backend { server 127.0.0.1:3001; server 127.0.0.1:3002; server 127.0.0.1:3003; } server { listen 80; location / { proxy_pass http://backend; } }

Nginx uses round-robin by default, so requests distribute the same way:

javascript
#13001 #23002 #33003 #43001 ...

4. Features and advantages

AdvantageDescription
SimplicityImplementable in a few lines
EvennessEvery node gets the same load
PredictabilityNo randomness or priority
StatelessNo client data needs to be stored
High speedMinimal computational overhead

5. Round-Robin's drawbacks

DrawbackDescription
Ignores server capacityA weaker server still gets the same share of requests
No sticky sessionsOne user can land on a different server on every request
Ignores real-time loadEven an overloaded server keeps getting new requests
A poor fit for long-lived connections (WebSocket)Can overload individual workers

6. Variants of the algorithm

To fix these issues, there are improved versions of Round-Robin:

AlgorithmThe difference
Weighted Round-RobinServers get a "weight" (more powerful ones get more traffic)
Dynamic Round-RobinAccounts for a server's current load
Least ConnectionsSends new requests to the server with the least activity
IP HashThe same client always lands on the same server (sticky sessions)

7. A real-life analogy

Picture a line of customers at a bank with 3 tellers:

  • the first serves customer 1,
  • the second serves customer 2,
  • the third serves customer 3,
  • the next customer goes back to the first teller.

That's Round-Robin, each teller (server) gets a customer in turn.

Summary

CriterionDescription
What it doesDistributes tasks or requests in turn and cyclically
Where it's usedLoad balancing, schedulers, operating systems
TypeA simple, deterministic algorithm
AdvantagesSimple, even, efficient
DrawbacksIgnores server capacity and load
Used inNode.js Cluster, PM2, Nginx, HAProxy, AWS ELB

Conclusion:

Round-Robin Scheduling is a "take turns" algorithm that evenly spreads load across every available node (process, thread, or server).

It's simple, fast and efficient, which is why it's the default in load balancers like Nginx, HAProxy, PM2 and Node.js Cluster.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.