Skip to content

System Design

Load Balancer (Round Robin & Weighted)

O(1) time per request for both versions — just an index lookup, no matter how many servers you have.

The idea, in plain English

A load balancer works like a host seating guests at tables. The simplest version, round robin, seats guests at table 1, then table 2, then table 3, then back to table 1 — over and over. This spreads people out evenly. The weighted version knows some tables are bigger. It seats more guests at the big tables before coming back around. A server with more capacity gets more requests.

How it works

  1. 1Round robin: keep a list of servers and a counter. Send each request to servers[counter % numberOfServers]. Then increase the counter by one.
  2. 2Weighted round robin: give each server a weight — how many requests it should get per cycle. Build one expanded list where a server appears as many times as its weight. Then round-robin over that list.
  3. 3Either way, each incoming request goes to whichever server the pattern points to next. No server needs to know anything about the others.

When you'd use it

Use a load balancer when you have more traffic than one server can handle. You run several copies of your app behind one address, and something needs to decide which copy handles each incoming request.

Common beginner mistakes

  • Forgetting the modulo (%) wraparound. Without it, the counter runs off the end of the server list instead of looping back to the start.
  • Giving every server equal weight, even when some machines are far more powerful. Plain round robin then overloads your weakest server just as much as your strongest one.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Plain round robin (9 requests): server-A server-B server-C server-A server-B server-C server-A server-B server-C
Weighted round robin (10 requests, A gets 3x): server-A server-A server-A server-B server-C server-A server-A server-A server-B server-C

Not sure this is the right topic? See the learning paths → or where this leads →