Bird
Raised Fist0
Nginxdevops~10 mins

Why load balancing distributes traffic in Nginx - Visual Breakdown

Choose your learning style10 modes available
Process Flow - Why load balancing distributes traffic
Client sends request
↓
Load Balancer receives request
↓
Select backend server based on algorithm
↓
Forward request to selected server
↓
Server processes request and responds
↓
Response sent back to client
Traffic from clients goes to the load balancer, which picks a backend server to handle each request, then forwards the request and returns the server's response.
Execution Sample
Nginx
upstream backend {
    server 192.168.1.10;
    server 192.168.1.11;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This Nginx config defines two backend servers and forwards incoming requests to them using load balancing.
Process Table
StepClient RequestLoad Balancer ActionSelected ServerRequest ForwardedResponse Sent
1Request 1Receives request192.168.1.10Forwarded to 192.168.1.10Response from 192.168.1.10
2Request 2Receives request192.168.1.11Forwarded to 192.168.1.11Response from 192.168.1.11
3Request 3Receives request192.168.1.10Forwarded to 192.168.1.10Response from 192.168.1.10
4Request 4Receives request192.168.1.11Forwarded to 192.168.1.11Response from 192.168.1.11
5Request 5Receives request192.168.1.10Forwarded to 192.168.1.10Response from 192.168.1.10
ExitNo more requestsStops forwarding---
💡 No more client requests to distribute
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Selected Server-192.168.1.10192.168.1.11192.168.1.10192.168.1.11192.168.1.10-
Key Moments - 2 Insights
Why does the load balancer pick different servers for each request?
The load balancer uses a round-robin method (see execution_table rows 1-5) to evenly distribute requests between servers, preventing overload on one server.
What happens if one server is slow or down?
Nginx can detect server health and skip slow or down servers, so requests go only to healthy servers, ensuring smooth traffic flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which server handles the 3rd request?
A192.168.1.11
B192.168.1.10
C192.168.1.12
DNo server
💡 Hint
Check the 'Selected Server' column at Step 3 in the execution_table
At which step does the load balancer stop forwarding requests?
AExit step
BStep 5
CStep 4
DStep 3
💡 Hint
Look at the 'Step' column and the exit_note in the execution_table
If a third server 192.168.1.12 is added, how would the selected server sequence change?
AIt would pick servers randomly
BIt would still use only the first two servers
CIt would alternate between the three servers in order
DIt would send all requests to 192.168.1.12
💡 Hint
Think about round-robin distribution and how adding a server affects the sequence in variable_tracker
Concept Snapshot
Load balancing in Nginx forwards client requests to multiple backend servers.
It uses algorithms like round-robin to distribute traffic evenly.
This prevents any single server from getting overloaded.
If a server is down, Nginx skips it to keep traffic flowing.
Config uses 'upstream' block and 'proxy_pass' directive.
Full Transcript
Load balancing distributes incoming client requests across multiple backend servers. The load balancer receives each request and selects a server to handle it, often using a round-robin method to keep traffic balanced. This prevents any one server from becoming overloaded and improves reliability. In Nginx, you define backend servers in an upstream block and forward requests using proxy_pass. The load balancer forwards the request to the chosen server, which processes it and sends back the response. If a server is down, Nginx can detect this and skip it, ensuring smooth service. The execution table shows how requests are distributed step-by-step, alternating between two servers.

Practice

(1/5)
1. Why does Nginx use load balancing to distribute traffic across multiple servers?
easy
A. To increase the size of the server's hard drive
B. To prevent any single server from becoming overloaded and improve response time
C. To reduce the number of servers needed
D. To make the website design look better

Solution

  1. Step 1: Understand the purpose of load balancing

    Load balancing spreads incoming requests across multiple servers to avoid overloading one server.
  2. Step 2: Recognize the benefit of spreading traffic

    By distributing traffic, servers handle requests faster and the service stays stable.
  3. Final Answer:

    To prevent any single server from becoming overloaded and improve response time -> Option B
  4. Quick Check:

    Load balancing improves speed and stability [OK]
Hint: Load balancing shares work to keep servers fast and stable [OK]
Common Mistakes:
  • Thinking load balancing increases server storage
  • Believing it reduces the number of servers needed
  • Confusing load balancing with website design
2. Which of the following is the correct syntax to define a load balancing group in Nginx?
easy
A. server_group backend { 192.168.1.10; 192.168.1.11; }
B. loadbalance backend { server 192.168.1.10; server 192.168.1.11; }
C. upstream backend { server 192.168.1.10; server 192.168.1.11; }
D. proxy_pass backend { server 192.168.1.10; server 192.168.1.11; }

Solution

  1. Step 1: Identify Nginx load balancing syntax

    Nginx uses the upstream block to define a group of servers for load balancing.
  2. Step 2: Check the correct block and server entries

    The correct syntax is upstream backend { server IP; server IP; } to list servers.
  3. Final Answer:

    upstream backend { server 192.168.1.10; server 192.168.1.11; } -> Option C
  4. Quick Check:

    Use upstream for server groups [OK]
Hint: Remember: 'upstream' defines server groups in Nginx [OK]
Common Mistakes:
  • Using 'loadbalance' instead of 'upstream'
  • Writing 'server_group' which is invalid
  • Misusing 'proxy_pass' inside server group
3. Given this Nginx config snippet, what happens when a client sends a request?
upstream backend {
  server 192.168.1.10;
  server 192.168.1.11;
}
server {
  location / {
    proxy_pass http://backend;
  }
}
medium
A. The request is distributed between 192.168.1.10 and 192.168.1.11
B. The request is sent to only 192.168.1.10 every time
C. The request is blocked because of missing proxy settings
D. The request is sent to 192.168.1.11 only

Solution

  1. Step 1: Understand the upstream block

    The upstream backend defines two servers to share traffic.
  2. Step 2: Analyze proxy_pass usage

    The proxy_pass http://backend; sends requests to the upstream group, distributing load.
  3. Final Answer:

    The request is distributed between 192.168.1.10 and 192.168.1.11 -> Option A
  4. Quick Check:

    Requests split across servers [OK]
Hint: proxy_pass to upstream sends requests to all servers [OK]
Common Mistakes:
  • Thinking requests always go to first server
  • Assuming proxy_pass blocks requests without extra config
  • Believing requests go to only one fixed server
4. You configured Nginx load balancing but all traffic goes to one server only. What is the likely cause?
medium
A. The upstream block has only one server listed
B. The load balancing method is set to 'ip_hash'
C. The servers are offline
D. The proxy_pass URL does not match the upstream name

Solution

  1. Step 1: Check upstream and proxy_pass names

    If proxy_pass points to a different name than the upstream block, load balancing won't work.
  2. Step 2: Understand traffic routing

    Requests will not be distributed if proxy_pass does not reference the correct upstream group.
  3. Final Answer:

    The proxy_pass URL does not match the upstream name -> Option D
  4. Quick Check:

    proxy_pass must match upstream name [OK]
Hint: proxy_pass name must match upstream block name exactly [OK]
Common Mistakes:
  • Ignoring mismatch between proxy_pass and upstream name
  • Assuming offline servers cause all traffic to one server
  • Confusing load balancing methods with traffic distribution issues
5. You want to ensure Nginx distributes traffic evenly but also keeps a user's requests on the same server. Which load balancing method should you use?
hard
A. ip_hash
B. least_conn
C. random
D. round_robin

Solution

  1. Step 1: Understand load balancing methods

    round_robin distributes evenly but does not keep user sessions on one server.
  2. Step 2: Identify method that keeps user on same server

    ip_hash uses client IP to send requests to the same server, maintaining session consistency.
  3. Final Answer:

    ip_hash -> Option A
  4. Quick Check:

    ip_hash keeps user on same server [OK]
Hint: Use ip_hash to stick users to one server [OK]
Common Mistakes:
  • Choosing round_robin for session persistence
  • Confusing least_conn with user stickiness
  • Thinking random method keeps sessions consistent
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard