Keepalive connections in Nginx - Time & Space Complexity
We want to understand how the time cost changes when nginx handles multiple requests using keepalive connections.
How does keeping connections open affect the work nginx does as requests grow?
Analyze the time complexity of the following nginx configuration snippet.
upstream backend {
server backend1.example.com;
server backend2.example.com;
keepalive 16;
}
server {
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
This config sets up a backend pool with 16 keepalive connections to reuse for multiple client requests.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming client request by reusing or opening backend connections.
- How many times: Once per client request, repeated for all requests.
As the number of client requests increases, nginx maintains a pool of up to 16 idle backend connections, reusing them for multiple requests.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 connection uses, mostly reused |
| 100 | Up to 16 new connections opened, 84 using reused connections |
| 1000 | Up to 16 new connections opened, 984 using reused connections |
Pattern observation: Connection reuse helps reduce work per request but total work still grows roughly linearly with requests.
Time Complexity: O(n)
This means the work nginx does grows directly with the number of requests, even with keepalive reuse.
[X] Wrong: "Keepalive connections make nginx handle any number of requests instantly without extra work."
[OK] Correct: Even with reuse, each request still needs processing; keepalive just reduces connection setup time, not total work.
Understanding how connection reuse affects request handling shows you can think about real server efficiency, a useful skill in many roles.
"What if we increased the keepalive value from 16 to 100? How would the time complexity change?"
Practice
keepalive connections in nginx?Solution
Step 1: Understand what keepalive connections do
Keepalive connections allow the same connection to be reused for multiple requests instead of opening a new one each time.Step 2: Identify the benefit of reusing connections
Reusing connections reduces the time and resources needed to establish new connections, thus improving speed and reducing server load.Final Answer:
They reuse the same connection for multiple requests, improving speed. -> Option DQuick Check:
Keepalive = reuse connection for speed [OK]
- Thinking keepalive closes connections immediately
- Believing keepalive increases open connections endlessly
- Confusing keepalive with caching
Solution
Step 1: Recall nginx directive syntax
Nginx directives use the format: directive_name value; without equal signs or colons.Step 2: Check the correct value format for time
Timeout values in nginx can be specified as numbers (seconds) without units or with units like 's'. Both are accepted, but the common practice is just a number for seconds.Final Answer:
keepalive_timeout 30; -> Option BQuick Check:
Directive syntax = name value; no '=' or ':' [OK]
- Using '=' sign in directive
- Adding colon ':' after directive name
- Adding units like 's' unnecessarily
keepalive_timeout 10; keepalive_requests 100;
What happens after 100 requests on the same keepalive connection?
Solution
Step 1: Understand keepalive_requests directive
This directive limits how many requests can be sent over one keepalive connection before it closes.Step 2: Apply the limit of 100 requests
After 100 requests, nginx closes the connection and opens a new one for the next request to avoid stale connections.Final Answer:
The connection is closed and a new one is opened for the next request. -> Option AQuick Check:
keepalive_requests = max requests per connection [OK]
- Thinking connection stays open forever
- Confusing timeout with request count
- Assuming connection closes after first request
keepalive_timeout 20; but clients report connections close too quickly. Which is the likely mistake?Solution
Step 1: Check common reasons for config changes not applying
If nginx is not reloaded or restarted after config changes, old settings remain active.Step 2: Confirm reload necessity
After changing keepalive_timeout, nginx must be reloaded withnginx -s reloador restarted to apply changes.Final Answer:
You forgot to reload nginx after changing config. -> Option CQuick Check:
Config changes need reload to apply [OK]
- Assuming timeout value alone fixes issue
- Ignoring need to reload nginx
- Blaming nginx version without checking
Solution
Step 1: Set keepalive_timeout to 15 seconds
This controls how long a connection stays open waiting for new requests.Step 2: Set keepalive_requests to 50
This limits the number of requests per connection before closing it.Step 3: Verify correct syntax and values
keepalive_timeout 15;\nkeepalive_requests 50; uses correct syntax and values matching the requirement: 15 seconds timeout and 50 requests limit.Final Answer:
keepalive_timeout 15;\nkeepalive_requests 50; -> Option AQuick Check:
Timeout 15 + Requests 50 = correct config [OK]
- Swapping timeout and requests values
- Adding units to keepalive_requests
- Using invalid directives like keepalive_disable
Ready to prove it?
Pass the test and this pattern moves unknown
