Bird
Raised Fist0
Nginxdevops~10 mins

Connection pooling to upstream in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Connection pooling to upstream
Client sends request
↓
Nginx receives request
↓
Check for idle connection in pool
↓
Reuse connection
↓
Send request to upstream server
↓
Receive response from upstream
↓
Return response to client
↓
Keep connection alive in pool for reuse
Nginx receives a client request and tries to reuse an existing connection to the upstream server from the pool. If none is available, it creates a new connection. After response, the connection is kept alive for future reuse.
Execution Sample
Nginx
upstream backend {
    server backend1.example.com;
    keepalive 32;
}

server {
    location / {
        proxy_pass http://backend;
    }
}
This config defines an upstream group with connection pooling of 32 connections and proxies client requests to it.
Process Table
StepActionConnection Pool StateConnection UsedResult
1Client sends first requestEmptyNo existing connectionNew connection created to backend1.example.com
2Nginx sends request upstream1 active connectionConnection #1Request sent successfully
3Response received1 active connectionConnection #1Response sent to client
4Connection #1 kept alive in pool1 idle connectionNoneConnection ready for reuse
5Client sends second request1 idle connectionReuse connection #1Request sent using pooled connection
6Response received1 active connectionConnection #1Response sent to client
7Connection #1 kept alive again1 idle connectionNoneConnection ready for reuse
8Client sends third request1 idle connectionReuse connection #1Request sent using pooled connection
9Response received1 active connectionConnection #1Response sent to client
10Connection #1 kept alive1 idle connectionNoneConnection ready for reuse
11No more requests1 idle connectionNoneConnections remain open until timeout or server closes
💡 No more client requests; connection remains idle in pool for reuse or timeout
Status Tracker
VariableStartAfter 1After 2After 3Final
connection_poolempty1 active connection1 idle connection1 active connection1 idle connection
connection_usednoneconnection #1noneconnection #1none
requests_handled01233
Key Moments - 3 Insights
Why does Nginx reuse the same connection instead of creating a new one for each request?
Because the connection pool has an idle connection available (see execution_table rows 4, 5, 7, 8), Nginx reuses it to save time and resources.
What happens if there are no idle connections in the pool when a new request arrives?
Nginx creates a new connection to the upstream server (see execution_table row 1), increasing active connections until the pool limit is reached.
Does Nginx close the connection immediately after sending the response?
No, it keeps the connection alive in the pool for reuse (see execution_table rows 4, 7, 10), improving performance for future requests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What connection does Nginx use to send the request?
AIt creates a new connection
BIt closes all connections
CIt reuses connection #1
DIt waits for a connection to free up
💡 Hint
Check the 'Connection Used' column at step 5 in the execution_table
At which step does Nginx first create a new connection to the upstream server?
AStep 1
BStep 4
CStep 5
DStep 11
💡 Hint
Look at the 'Result' column in the execution_table for the first new connection creation
If the keepalive value was set to 0, how would the connection_pool variable change after each request?
AIt would keep 1 idle connection
BIt would have no idle connections, closing after each request
CIt would create multiple idle connections
DIt would reuse connections indefinitely
💡 Hint
Refer to variable_tracker for connection_pool states and understand keepalive effect
Concept Snapshot
Nginx connection pooling keeps upstream connections alive for reuse.
Use 'keepalive N;' in upstream block to set pool size.
Nginx reuses idle connections to reduce latency.
New connections created only if pool is empty.
Connections close after timeout or server closes.
Full Transcript
This visual execution shows how Nginx manages connection pooling to upstream servers. When a client sends a request, Nginx checks if there is an idle connection in the pool. If yes, it reuses that connection to send the request upstream, saving time and resources. If no idle connection exists, Nginx creates a new connection. After receiving the response, Nginx keeps the connection alive in the pool for future reuse. This process repeats for subsequent requests, improving performance by avoiding connection setup overhead. The connection pool size is controlled by the 'keepalive' directive in the upstream block. Connections remain open until they time out or the server closes them. This efficient reuse of connections is key to fast proxying in Nginx.

Practice

(1/5)
1. What is the main purpose of using keepalive in an upstream block in nginx?
easy
A. To limit the number of client connections to nginx
B. To close all connections immediately after each request
C. To reuse open connections to backend servers and improve response speed
D. To enable caching of static files on the server

Solution

  1. Step 1: Understand the role of keepalive in upstream

    The keepalive directive in the upstream block tells nginx to keep a number of connections open to backend servers for reuse.
  2. Step 2: Identify the benefit of connection reuse

    Reusing connections avoids the overhead of opening new connections for each request, improving speed and reducing latency.
  3. Final Answer:

    To reuse open connections to backend servers and improve response speed -> Option C
  4. Quick Check:

    Connection pooling = reuse connections = To reuse open connections to backend servers and improve response speed [OK]
Hint: Remember: keepalive means keep connections open for reuse [OK]
Common Mistakes:
  • Thinking keepalive limits client connections
  • Confusing keepalive with caching
  • Assuming keepalive closes connections immediately
2. Which of the following is the correct syntax to enable connection pooling with 32 keepalive connections in an nginx upstream block?
easy
A. upstream backend { keepalive 32; server backend1.example.com; }
B. upstream backend { keepalive_connections 32; server backend1.example.com; }
C. upstream backend { keepalive=32; server backend1.example.com; }
D. upstream backend { keepalive 32 servers; server backend1.example.com; }

Solution

  1. Step 1: Recall the correct directive syntax

    The correct directive to set connection pooling count is keepalive followed by the number, ending with a semicolon.
  2. Step 2: Check each option's syntax

    upstream backend { keepalive 32; server backend1.example.com; } uses keepalive 32; which is correct. Options A, B, and C use incorrect directive names or syntax.
  3. Final Answer:

    upstream backend { keepalive 32; server backend1.example.com; } -> Option A
  4. Quick Check:

    Correct syntax = keepalive 32; = upstream backend { keepalive 32; server backend1.example.com; } [OK]
Hint: Use 'keepalive' followed by number and semicolon inside upstream [OK]
Common Mistakes:
  • Using incorrect directive names like keepalive_connections
  • Adding extra words after number
  • Using equal sign instead of space
3. Given this nginx configuration snippet, what will be the effect on connection reuse?
upstream backend {
    server backend1.example.com;
    keepalive 16;
}

server {
    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}
medium
A. Connections will be closed immediately after each request
B. Connections will not be reused because proxy_http_version is 1.1
C. The configuration will cause a syntax error and fail to start nginx
D. Connections to backend will be reused up to 16 times, improving performance

Solution

  1. Step 1: Analyze upstream keepalive setting

    The keepalive 16; directive allows nginx to keep up to 16 idle connections open to the backend for reuse.
  2. Step 2: Check proxy settings for connection reuse

    Setting proxy_http_version 1.1; and proxy_set_header Connection ""; enables connection reuse by preventing the 'Connection: close' header.
  3. Final Answer:

    Connections to backend will be reused up to 16 times, improving performance -> Option D
  4. Quick Check:

    keepalive + proxy_http_version 1.1 + empty Connection header = reuse [OK]
Hint: Use HTTP/1.1 and clear Connection header to enable reuse [OK]
Common Mistakes:
  • Assuming HTTP/1.1 disables reuse
  • Not clearing Connection header disables pooling
  • Confusing keepalive with client connections
4. You configured connection pooling with keepalive 10; in your upstream block, but nginx is still opening new connections for every request. What is the most likely cause?
medium
A. You forgot to set proxy_http_version 1.1; and clear the Connection header
B. The keepalive directive only works with HTTP/2
C. You need to increase keepalive to at least 100
D. The backend server does not support keepalive connections

Solution

  1. Step 1: Check proxy settings required for connection reuse

    For connection pooling to work, nginx must use HTTP/1.1 and clear the Connection header to avoid closing connections.
  2. Step 2: Identify common misconfiguration

    If proxy_http_version 1.1; and proxy_set_header Connection ""; are missing, nginx will close connections after each request despite keepalive.
  3. Final Answer:

    You forgot to set proxy_http_version 1.1; and clear the Connection header -> Option A
  4. Quick Check:

    Missing proxy_http_version 1.1 + Connection header clear = no reuse [OK]
Hint: Always set proxy_http_version 1.1 and clear Connection header [OK]
Common Mistakes:
  • Believing keepalive works without proxy_http_version 1.1
  • Thinking backend server must be changed first
  • Assuming keepalive number is too low
5. You want to optimize nginx to reuse connections to multiple backend servers with different keepalive limits. Which configuration correctly sets 20 keepalive connections for backend1 and 10 for backend2?
hard
A. upstream backend { server backend1.example.com; keepalive 20; server backend2.example.com; keepalive 10; }
B. upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; }
C. upstream backend { server backend1.example.com keepalive=20; server backend2.example.com keepalive=10; }
D. upstream backend { server backend1.example.com; server backend2.example.com; keepalive 30; }

Solution

  1. Step 1: Understand keepalive scope

    The keepalive directive applies per upstream block, not per server inside it.
  2. Step 2: Check options for setting different keepalive values

    upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } defines two separate upstream blocks, each with its own keepalive setting, which is correct. Options B, C, and D try to set different keepalive values inside one upstream block, which is invalid.
  3. Final Answer:

    upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } -> Option B
  4. Quick Check:

    Separate upstream blocks for different keepalive values = upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } [OK]
Hint: Use separate upstream blocks to set different keepalive values [OK]
Common Mistakes:
  • Trying to set keepalive per server inside one upstream
  • Using invalid syntax with equal signs
  • Setting one keepalive for multiple servers expecting different limits
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