Avoiding redirects

Redirects can be a common reason why websites feel slow. A single redirect can slow down an initial request with around 300-500ms and even more on a mobile device. This is close to unacceptable, especially when during this time nothing happens. The user starts to feel that loading is delayed. The first half second is very important when we need to draw something on screen as fast as possible. A site that reacts fast, but loads subsequent resources more slowly and transparently or only as needed may be perceived as being faster compared to one that is very slow to react, but then utilizes the available bandwidth better do download and present everything at once. As always, this depends on a many factors like the number of (blocking) resources, the availability of CDN, the state of the cache and others. Drawing on screen as fast as possible ensures that engagement is kept with the loading process. We may think that 300ms isn't much, but when we consider that one of the slowest parts of a site—the establishment of a connection to the database—can take around 40ms (PHP/MySQL, single connection) or that requiring a single file of 1000 lines with PHP can take around 10ms, whereas some built-in functions can execute in 1-2ms, we start to see the picture. Doing extra things on the network isn't the best use of our time budget. In fact, avoiding unnecessary requests remains one of the most efficient ways to speed up a site today.

Redirects may not always be obvious and even when we encounter them, we still may not know which part of the code is causing them, although we can assume that this will be an equivalent of the PHP's header function. To avoid a redirect, we can make the request more precise by using the right parameters, but this will needlessly grow our code and make it less portable. The slowest redirect is probably the one that comes from our own code as it gives a signal for the whole request body to be reloaded.

When a user types an address without the www prefix, the browser adds it, which can initiate a permanent (301) or temporary (302) redirect as the use of the Chrome add-on "Redirect Path" shows. This is a redirect initiated by the browser, but it happens so fast, that we barely notice it.

We can also think of URL rewriting as a form of redirect—one that happens on every request. Although the result is a clean URL with no evidence of the underlying technology, the use of regular expressions for matching and replacement can be slow (up to tens of ms) and adding more bytes to the .htaccess file makes the site slower while not adding much value to it. This is a redirect initiated by the web server. We should not forget that many people prefer clicking on links rather than specifying the correct URL structure. The latter makes them think and also adds to the perception of how fast the site feels.

Redirects take many forms and we must learn to recognize and avoid them. The purpose of a website is to deliver the content, not to seek detours to do the same. Not considering the value we provide through our websites is a harmful thought redirect.

bit.ly/18pT40a