This is part 6 of a multiple part series on web performance improvements. The first Introductory blog can be found here. In previous part we discussed benefits of cookie-free domains. In this part we will discuss the negative impact of redirects in web application performance.
Redirect is a phenomenon where user as routed from one URL to another. There can be various reasons for URL redirect, including web site redesign, tracking traffic flow, counting ad impressions, and creating URLs that are easier for users to remember.
There are different kinds of redirects.
- 300: Multiple Choices (based on Content-Type)
- 301: Moved Permanently
- 302: Moved Temporarily (a.k.a. Found)
- 303: See Other (clarification of 302)
- 304: Not Modified- It is not really a redirect—it’s used in response to conditional GET requests to avoid downloading data that is already cached by the browser
- 305: Use Proxy
- 306: No longer used
- 307: Temporary Redirect (clarification of 302)
Out of these 301 and 302 are the most popular. Redirects are usually done for HTML documents, but they may also be used when requesting components in the page (images, scripts, etc). Beside considering, the need to of redirect and their uses, biggest disadvantage of redirects is it makes your pages slower. Sometimes this can be total performance killer.
For example, suppose user request for a URL and receives following response from server then there will be a page redirect.
HTTP/1.1 301 Moved Permanently
In case of redirects the browser automatically takes the user to the URL specified in the Location field. All the information necessary for a redirect is present in the headers, as shown in above example. The body of the response is typically empty. Despite their names, neither a 301 nor a 302 response is cached in practice unless additional headers, such as Expires or Cache-Control, indicate that it should be.
If the head of the HTML document includes meta refresh tag a page redirect will happen after the number of seconds specified in the content attribute:
- meta http-equiv=”refresh” content=”0; url=http://abc2xyz.com/newuri”
How Redirects Hurt Performance
Below is an image is a snapshot of a browser request response, using Yslow. It shows how redirects slow down the user experience.
The first HTTP request is the redirect. Redirects delay the delivery of entire HTML document. Nothing is presented to the user until redirect and HTML document requests are completed. Nothing in the page can be rendered and no components can be downloaded until the HTML document has arrived. Inserting a redirect between the user and the HTML document delays everything in the page. The style-sheets are very much important for the page rendering. But due to redirect they are further delayed. In case of no redirect this html request could have been served to user much early.
Redirects are typically used with requests for the HTML document, but sometimes they are used for components in the page. Such redirects further delay the page output. Below figure shows the HTTP requests for Google Toolbar. It contains four redirects.
There can be various reasons (good ones) for URL redirect like
- Web site redesign
- Tracking traffic flow
- Counting ad impressions
- Creating URLs that are easier for users to remember
Though redirect can fix a number of issues as given above for business it may introduce a lot of issues for the end user. If business demands we cannot avoid many of the above mention situations too. So we need to find some alternative solutions which achieve our intended goal at the same time do not cost us on performance front.
So what options we have…
The following sections discuss some of the typical situations in which redirects are used, and alternatives that we can implement to make the user experience better.
Missing Trailing Slash
This kind of redirect happens when a trailing slash (/) is missing from a URL that should otherwise have one. For example,
when we type http://shine.yahoo.com/horoscope in browser it results in a 301 response containing a redirect to http://shine.yahoo.com/horoscope/. The only difference is the addition of a trailing slash (/).
- GET / HTTP/1.1
Sending a redirect when a trailing slash is missing is the default behaviour for many web servers, including Apache. The Alias directive is an easy workaround. Another alternative is to use the mod_rewrite module, but Alias is simpler. The problem with the Horoscope site could be resolved by adding the following to the Apache configuration:
- Alias /horoscope /usr/local/apache/htdocs/horoscope/index.html
Handler in Apache 2.0 provides cleaner solution in the form of DirectorySlash directive. Assuming there is a handler named horoscopehandler, the use of DirectorySlash would look as follows:
Moving between Web Sites/ pages
Consider a situation where
- The website is under re-engineering process. This may happen due changes in technology or any other reason. The URL’s in new system may be different than the existing system. An easy way to transition users from the old URLs to the new ones is to use redirects.
- Due to certain condition redirect is happening within parts of a web site (type of browser, type of user account, etc.).
Using a redirect to connect two web sites is simple and requires little additional coding. Although redirects reduce the complexity for developers, it degrades the user experience. There are alternatives for integrating two websites without degrading user experience:
- If the two websites reside on the same server, it’s possible the code itself could be linked. For example, the older handler code could call the new handler code programmatically.
- If the domain name changes, a DNS record that creates an alias pointing from one domain name to another (a CNAME) can be used to make both hostnames point to the same server.
- Alias, mod_rewrite, and DirectorySlash are simple solutions to fix this issue.
User Friendly URLs
Redirects are used to make URLs prettier and easier to remember for end users. Suppose the actual URL of the page earlier was:
It is not easy for a user to type or remember this URL. But if the URL is converted to:
then this is a easy to remember for any user. But internally this URL is linked to the previous URL, which this causes a redirect.
The key is to find a way to have these simpler URLs without the redirects. Rather than forcing users to undergo an additional HTTP request, it would be better to avoid the redirect using Alias, mod_rewrite, DirectorySlash, and directly linking code.