0
0
Nginxdevops~5 mins

Streaming and chunked transfer in Nginx - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Streaming and chunked transfer
O(n)
Understanding Time Complexity

When nginx streams data using chunked transfer, it sends pieces of data as they become ready.

We want to understand how the time to send data grows as the data size increases.

Scenario Under Consideration

Analyze the time complexity of the following nginx configuration snippet.


location /stream {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    chunked_transfer_encoding on;
}
    

This config enables chunked transfer encoding to stream data from the backend server to the client in chunks.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Sending each chunk of data as it arrives.
  • How many times: Once per chunk, which depends on the total data size divided by chunk size.
How Execution Grows With Input

As the total data size grows, the number of chunks sent grows roughly in proportion.

Input Size (n bytes)Approx. Number of Chunks
10 KBFew chunks (small number)
100 KBAbout 10 times more chunks
1 MBAbout 100 times more chunks

Pattern observation: The number of operations grows linearly with data size.

Final Time Complexity

Time Complexity: O(n)

This means the time to send data grows directly in proportion to the data size.

Common Mistake

[X] Wrong: "Chunked transfer sends all data instantly, so time does not grow with size."

[OK] Correct: Even though data is sent in chunks, each chunk takes time to send, so total time grows with total data size.

Interview Connect

Understanding how streaming scales helps you explain real-world server behavior clearly and confidently.

Self-Check

"What if chunk sizes were doubled? How would the time complexity change?"