Senior LevelSystem Design & Architecture
How does Zero-Copy File Streaming (`sendfile`) eliminate CPU context switches in web servers?
β Interview Prompt: Explain Zero-Copy file streaming, OS `sendfile()` system call, bypassing user-space buffer copies, and high-speed static asset serving.
Standard file serving reads file bytes into a user-space application buffer before writing them back to a network socket buffer, requiring 4 CPU context switches and 4 data copies:
'sendfile()' Zero-Copy Solution:
The OS kernel transfers file data bytes directly from the Page Cache to the Network Card (NIC) buffer:
β’
Zero user-space CPU context switches and zero memory copying! Used by NGINX and Kafka to achieve maximum network throughput.
Code Implementation Example
bash
# Inspecting NGINX sendfile zero-copy configuration:
# sendfile on; # Enables OS kernel-level zero-copy file streamingπ‘Enabling kernel-level zero-copy file streaming in NGINX.
π― Interview Key Takeaway
Use sendfile zero-copy kernel APIs in high-throughput file servers to eliminate user-space memory copying.
#System Design#Zero-Copy#sendfile#Kernel#Senior
Back to all System Design & Architecture Questionsβ