0
0
Nginxdevops~10 mins

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

Choose your learning style9 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.