Function Flush and page performance

Hi there guys. So I was reading the Yahoo Dev tutorial on page optimization and I came across this rule: https://developer.yahoo.com/performance/rules.html#flush using the flush() function in PHP. So I tried to add it in my community at http://www.klayz.com/community. I approximately gain in page load time from 2.7 seconds to 2.4 secs (I tested it with GtMetrix, so this result in not precise at all, as I also have adsenses in my page… but just as an indication). So, my question is… do you use it too in every web project that you’re working on? If yes, how? Just a first <?php flush(); ?> below and before (like I’m doing now) or in multiple locations? I’m asking this because I sincerely haven’t not seen it mentioned in a lot of web tutorial on performance optimization… and also because I really have to better understand it and how to use it with multiple instances on a web page.

In a typical situation of rendering a web page I would advise against using flush() because it can actually slow things down. The reasoning in the linked article sounds sane at first sight but has one false reasoning:

[…] It allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page.

Indeed, the browser will start fetching the partially prepared page but the backend will not be busy with the rest of the page - it will pause and wait until the browser fetches the html chunk. It is only when the browser responds that it has fetched all that was sent does the server continue with PHP execution. Therefore, you will increase the number of round trips between the server and the browsers effectively prolonging rendering of the whole page.

By default on an Apache server, PHP will buffer all echo/print output and send them to the browser in chunks. How this works will depend much on the server configuration - for example, it is very common to have some form of gzip compression enabled, in which case usually the page is sent in one chunk after compressing the whole content.

Of course, there are some scenarios when you may want to flush partial page content - but don’t expect it will speed up the rendering of the whole page. To sum up - the yahoo article has generally bad advice (but see my last paragraph), especially flushing content after </head> doesn’t make sense since the browser will not have any content to show yet but an additional network round trip will be added to the total rendering time.

However - the flushing behaviour will depend a lot on the server setup - for example, if there is a nginx Reverse Proxy for Apache installed on the server (of maybe nginx alone as well?) then the script will not pause after flush() and in that case you can flush partial content multiple times throughout the page without any detrimental impact. If gzip is enabled then flushing will have no effect at all. Therefore, this behaviour is not portable across servers and you should run test scripts each time to determine how your server behaves - for example, flushing some text in a loop with a sleep() inside and measuring the time.

1 Like

Thank you a lot @Lemon_Juice.

What is this “html chunk”? I’m from Italy so I didn’t get exactly this term, sincerely speaking. :stuck_out_tongue:

A chunk is a portion of something - by default the buffered chunk is set to 16 KB - but this again may vary between servers and is irrelevant for gzipped content.

1 Like

Got it. Thank you again!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.