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?"
Practice
sendfile directive in nginx?Solution
Step 1: Understand what
Thesendfiledoessendfiledirective allows nginx to send files directly from disk to the network socket without copying data to user space, which speeds up file sending.Step 2: Compare options with this behavior
Options B, C, and D describe unrelated features like compression, encryption, or connection limits, which are not the purpose ofsendfile.Final Answer:
To speed up file sending by avoiding extra copying between kernel and user space -> Option BQuick Check:
sendfile= avoid extra copying [OK]
- Thinking sendfile compresses data
- Confusing sendfile with encryption
- Assuming sendfile limits connections
tcp_nopush in nginx configuration?Solution
Step 1: Recall nginx directive syntax
In nginx, directives are set with the formatdirective value;. The correct value for enablingtcp_nopushison.Step 2: Check each option's syntax
tcp_nopush on; usestcp_nopush on;which is correct. Options A, C, and D use invalid keywords or syntax not supported by nginx.Final Answer:
tcp_nopush on; -> Option DQuick Check:
Enable directives withon;in nginx [OK]
- Using 'enable' instead of 'on'
- Adding '=' sign in directive
- Using 'true' instead of 'on'
sendfile on; tcp_nopush on;
What is the expected effect on static file delivery?
Solution
Step 1: Understand combined effect of
sendfileandtcp_nopushsendfile on;avoids extra copying, andtcp_nopush on;allows grouping data into bigger TCP packets, improving speed.Step 2: Analyze options
Files are sent faster by sending larger TCP packets correctly states files are sent faster by sending larger TCP packets. Options B, C, and D describe incorrect effects.Final Answer:
Files are sent faster by sending larger TCP packets -> Option AQuick Check:
sendfile + tcp_nopush = faster large packets [OK]
- Thinking tcp_nopush compresses data
- Assuming sendfile slows down transfer
- Confusing tcp_nopush with encryption
sendfile on; but forgot to enable tcp_nopush. What issue might you observe?Solution
Step 1: Understand the role of
tcp_nopushwithsendfiletcp_nopushhelps group data into larger TCP packets. Without it,sendfilesends smaller packets inefficiently.Step 2: Analyze consequences of missing
Small TCP packets causing inefficient network usage correctly identifies small TCP packets causing inefficient network usage. Options A, B, and D describe errors or corruption which do not happen just by missingtcp_nopushtcp_nopush.Final Answer:
Small TCP packets causing inefficient network usage -> Option CQuick Check:
Missing tcp_nopush = small packets [OK]
- Expecting syntax errors without tcp_nopush
- Thinking files get corrupted
- Assuming files fail to send
sendfile and tcp_nopush together?Solution
Step 1: Identify best practice for static file optimization
To optimize static file delivery,sendfileshould be enabled to avoid extra copying, andtcp_nopushenabled to send larger TCP packets.Step 2: Evaluate each configuration snippet
sendfile on; tcp_nopush on; enables bothsendfileandtcp_nopush, which is the correct combination. Options A, B, and C disable one or both, reducing efficiency.Final Answer:
sendfile on; tcp_nopush on; -> Option AQuick Check:
Enable both for best static file speed [OK]
- Disabling sendfile but enabling tcp_nopush
- Enabling sendfile but disabling tcp_nopush
- Disabling both directives
Ready to prove it?
Pass the test and this pattern moves unknown
