Connection pooling to upstream in Nginx - Time & Space Complexity
We want to understand how the time to handle requests changes when using connection pooling in nginx.
Specifically, how does reusing connections affect the work nginx does as requests increase?
Analyze the time complexity of the following nginx configuration snippet.
upstream backend {
server backend1.example.com;
server backend2.example.com;
keepalive 32;
}
server {
location / {
proxy_pass http://backend;
}
}
This config sets up connection pooling with 32 keepalive connections to upstream servers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming request by reusing or opening connections to upstream servers.
- How many times: Once per request, but connection reuse reduces repeated connection setup overhead.
As the number of requests grows, nginx reuses existing connections up to the keepalive limit, reducing repeated setup work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 request handlings, few new connections |
| 100 | 100 request handlings, many reuse existing connections |
| 1000 | 1000 request handlings, most reuse connections, few new setups |
Pattern observation: The work per request stays mostly steady because connections are reused, not reopened each time.
Time Complexity: O(n)
This means the total work grows linearly with the number of requests, but connection reuse keeps each request efficient.
[X] Wrong: "Connection pooling makes handling requests constant time regardless of request count."
[OK] Correct: Each request still needs processing, so total work grows with requests, but pooling reduces repeated connection setup time.
Understanding how connection pooling affects request handling time shows you can reason about real server efficiency and resource use.
"What if the keepalive value was set to 1 instead of 32? How would the time complexity change?"
Practice
keepalive in an upstream block in nginx?Solution
Step 1: Understand the role of
Thekeepalivein upstreamkeepalivedirective in theupstreamblock tells nginx to keep a number of connections open to backend servers for reuse.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.Final Answer:
To reuse open connections to backend servers and improve response speed -> Option CQuick Check:
Connection pooling = reuse connections = To reuse open connections to backend servers and improve response speed [OK]
- Thinking keepalive limits client connections
- Confusing keepalive with caching
- Assuming keepalive closes connections immediately
upstream block?Solution
Step 1: Recall the correct directive syntax
The correct directive to set connection pooling count iskeepalivefollowed by the number, ending with a semicolon.Step 2: Check each option's syntax
upstream backend { keepalive 32; server backend1.example.com; } useskeepalive 32;which is correct. Options A, B, and C use incorrect directive names or syntax.Final Answer:
upstream backend { keepalive 32; server backend1.example.com; } -> Option AQuick Check:
Correct syntax = keepalive 32; = upstream backend { keepalive 32; server backend1.example.com; } [OK]
- Using incorrect directive names like keepalive_connections
- Adding extra words after number
- Using equal sign instead of space
upstream backend {
server backend1.example.com;
keepalive 16;
}
server {
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}Solution
Step 1: Analyze upstream keepalive setting
Thekeepalive 16;directive allows nginx to keep up to 16 idle connections open to the backend for reuse.Step 2: Check proxy settings for connection reuse
Settingproxy_http_version 1.1;andproxy_set_header Connection "";enables connection reuse by preventing the 'Connection: close' header.Final Answer:
Connections to backend will be reused up to 16 times, improving performance -> Option DQuick Check:
keepalive + proxy_http_version 1.1 + empty Connection header = reuse [OK]
- Assuming HTTP/1.1 disables reuse
- Not clearing Connection header disables pooling
- Confusing keepalive with client connections
keepalive 10; in your upstream block, but nginx is still opening new connections for every request. What is the most likely cause?Solution
Step 1: Check proxy settings required for connection reuse
For connection pooling to work, nginx must use HTTP/1.1 and clear theConnectionheader to avoid closing connections.Step 2: Identify common misconfiguration
Ifproxy_http_version 1.1;andproxy_set_header Connection "";are missing, nginx will close connections after each request despitekeepalive.Final Answer:
You forgot to setproxy_http_version 1.1;and clear theConnectionheader -> Option AQuick Check:
Missing proxy_http_version 1.1 + Connection header clear = no reuse [OK]
- Believing keepalive works without proxy_http_version 1.1
- Thinking backend server must be changed first
- Assuming keepalive number is too low
backend1 and 10 for backend2?Solution
Step 1: Understand keepalive scope
Thekeepalivedirective applies perupstreamblock, not per server inside it.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 ownkeepalivesetting, which is correct. Options B, C, and D try to set different keepalive values inside one upstream block, which is invalid.Final Answer:
upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } -> Option BQuick 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]
- Trying to set keepalive per server inside one upstream
- Using invalid syntax with equal signs
- Setting one keepalive for multiple servers expecting different limits
Ready to prove it?
Pass the test and this pattern moves unknown
