sendfile and tcp_nopush in Nginx - Time & Space Complexity
We want to understand how enabling sendfile and tcp_nopush affects the speed of sending files in nginx.
How does the number of operations change when serving files with these settings?
Analyze the time complexity of this nginx configuration snippet.
sendfile on;
tcp_nopush on;
location /files/ {
root /var/www/html;
}
This config enables zero-copy file sending and TCP packet optimization for serving static files.
Look for repeated work when sending file data over the network.
- Primary operation: Reading file data and sending it over TCP.
- How many times: Once per file chunk until the whole file is sent.
As file size grows, the number of chunks to send grows roughly in proportion.
| Input Size (n KB) | Approx. Operations (file chunks) |
|---|---|
| 10 | 10 chunks |
| 100 | 100 chunks |
| 1000 | 1000 chunks |
Pattern observation: The operations grow linearly with file size.
Time Complexity: O(n)
This means the time to send a file grows directly with the file size.
[X] Wrong: "Turning on sendfile and tcp_nopush makes sending files instant regardless of size."
[OK] Correct: These settings reduce CPU and system calls but the data still must travel over the network, so time still grows with file size.
Understanding how nginx handles file sending helps you explain real-world server performance and network efficiency in interviews.
"What if tcp_nopush was turned off but sendfile stayed on? How would the time complexity change?"