0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lesson

Weighted Load Balancing & Backup Servers

Distribute traffic unevenly across servers using weights, and add backup servers that only take over when the primary pool fails.

Weighted Load Balancing & Backup Servers is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Servers Are Not Equal

Real upstream pools often mix machines of different capacity. A new 16-core server should not get the same share as an old 4-core box.

Nginx lets you bias traffic with weights so stronger servers handle more requests.

The weight Parameter

Add weight=N to a server. The default weight is 1. A server with weight=3 receives roughly three times as many requests as a weight=1 server.

upstream app {
    server 10.0.0.1 weight=3;
    server 10.0.0.2 weight=1;
}

How the Ratio Works

With weights 3 and 1, out of every 4 requests, 3 go to the first server and 1 to the second. Weights are relative, so 6 and 2 produce the same split as 3 and 1.

Weights with Other Algorithms

Weights work with round robin (the default) and least connections. They do not apply to ip_hash, which selects a server purely from the client IP.

upstream app {
    least_conn;
    server 10.0.0.1 weight=5;
    server 10.0.0.2 weight=2;
}

Introducing Backup Servers

A server marked backup receives no traffic while any primary server is available. It activates only when all primaries are down or unreachable.

upstream app {
    server 10.0.0.1;
    server 10.0.0.2;
    server 10.0.0.9 backup;
}

Why Use a Backup

Backups give you a graceful fallback, for example a maintenance page server or a secondary data center, without sending it normal traffic during healthy operation.

Marking a Server Down

Use down to permanently remove a server from rotation, handy during planned maintenance without deleting the line.

upstream app {
    server 10.0.0.1;
    server 10.0.0.2 down;
}

Failure Detection Parameters

max_fails sets how many failed attempts mark a server unavailable, and fail_timeout sets the window and how long it stays out.

server 10.0.0.1 max_fails=3 fail_timeout=30s;

Combining Weight and Backup

You can mix weights for primaries and keep a single backup for the whole pool. The backup ignores weight until it is the only option.

upstream app {
    server 10.0.0.1 weight=4;
    server 10.0.0.2 weight=1;
    server 10.0.0.9 backup;
}

Limiting Connections

The max_conns parameter caps simultaneous connections to a server, protecting a weaker box even if its weight is high.

server 10.0.0.2 weight=2 max_conns=100;

Testing the Distribution

Fire many requests and check your access logs per upstream to confirm the ratio matches the weights. Adjust weights based on observed CPU and latency.

for i in $(seq 1 100); do curl -s http://localhost/ > /dev/null; done

Quick Check

A server is marked backup. When does it receive requests?

Recap

You learned to fine-tune traffic distribution:

  • weight=N biases traffic toward stronger servers
  • Weights are relative and apply to round robin and least_conn
  • backup servers activate only when primaries fail
  • down, max_fails, and max_conns control availability

Together these give you precise control over an uneven server fleet.

Frequently asked questions

Is the “Weighted Load Balancing & Backup Servers” lesson free?

Yes — the full text of “Weighted Load Balancing & Backup Servers” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.

What will I learn in “Weighted Load Balancing & Backup Servers”?

Distribute traffic unevenly across servers using weights, and add backup servers that only take over when the primary pool fails. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Weighted Load Balancing & Backup Servers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?

Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Load Balancing Algorithms
  2. Health Checks & Server Monitoring
  3. Sticky Sessions & Session Persistence
  4. Weighted Load Balancing & Backup Servers
← Back to API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)