Is there any difference in performance between writing a file as a response and writing to a file as a part (buffer)?

Asked

Viewed 152 times

14

I have a t2.micro instance on Amazon where I use IIS 10 as server and webforms in the application (C#).

In a certain part of the application, I need to grab a list of image files from a certain folder, zip it and reply as download to the user.

The problem is that in a particular case, when the ZIP file has reached 1GB, the server simply hangs when the user clicks on the button to download that ZIP file.

I would like to ask some questions, not regarding the above problem, but a technical question regarding how download responses work, when you write it through a Stream or something like.

For the server, there is some difference between writing a response from a file directly and writing using buffer (using a while, for example)?

Example 1 - Directly:

// headers para download
readfile($filename);

Example 2 - By Buffer:

// headers para download

$handler = fopen($filename);

while(feof($handler) !== false) {
   echo fgets($handler, 4096);
}
  • 6

    Thank you downvoter, I would like a feedback

1 answer

11

There are differences yes.

In the first case the file will be loaded entirely into the server memory before it is sent to the client.

In the second case the file will be partially loaded in memory and being sent slowly to the client. This way you compromise the server memory less, because it can meet multiple requests consuming little memory for each of them.

About your problem, you are probably using the first case (loading the entire file into memory), causing the t2.micro server (which interestingly has 1 GB RAM memory) lock and stop answering.

In terms of performance, using the technique of the second case, the download will not only save memory but also start to be made available to the user earlier, because the time to load part of a file vs an entire file in the server memory are quite different.

There’s a partial implementation made in Java with Spring in which it is also possible to reproduce this behavior, also showing the two situations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.