Use of AJAX To Conserve Bandwidth and Processing
Ever noticed in Gmail, Twitter or Facebook how the last portion of the URL (the so-called “hash”) changes with each navigation, while the actual URL remains the same?
This unique AJAX technique, employed by some of the top current Web 2.0 apps, has many benefits besides usability: it actually saves the bandwidth of the site by an astounding amount, and defers the latency of the new page’s loading to the responsibility of the client-side web browser’s script compilation instead of downloading a new, full page each change in navigation.
The Basis of the Technique
If you want to see this in action, simply click on your replies within Twitter’s web client, do anything in Gmail/Google Wave, or click a profile/photo in Facebook – watch the URL, particularly the last part (everything past the ‘#’ character, called the “hash” by many.)
Also, it updates the URL’s post-hash characters to make the “new page” bookmark-friendly in case a user wants to go directly to that page as if it were static HTML instead of a dynamic client-side scripting-dependent page instance.
Likewise, each time a page within Gmail (or whatever app) is loaded, it checks for hash characters and will load the data into the page, transparently to the user. Because of this, most of the time the characters after the hash is simply the latter part of a URL to be inserted into the AJAX function to load the page, e.g. the characters necessary to download a page called “http://www.gmail.com/”+hashtag etc.
Benefits of this System: bandwidth
Most professionals use this simple AJAX technique to simplify the navigation of their site and to track user activity, but there is an often-overlooked advantage to using this model: bandwidth conservation and faster loading speed via deferred processing.
Here’s a rudimentary example: when your web app doesn’t use AJAX-loading and instead relies on unique page loads each and every time the user navigates to a new page, here is what is sent to the client’s web browser each time, following server-side processing:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!– keep in mind that each time this full page is loaded, server-side scripting is executed for tasks like query-string/post-data processing and file inclusion – therefore heavy processing amounts and file system I/O is required –>
<link rel="stylesheet" src="/style.css" type="text/css" />
<meta name="description" content="every little character counts!" />
<!– site-wide navigation bars, formatting dividers, and other common page elements go here, followed by the actual page-specific content that could have alternatively been loaded directly via AJAX –>
Keep in mind that the example provided only a small subset of what most sites, especially web applications, usually entail within a typical page: most of the time, heavy server-side processing and many scripts/stylesheets are included in every page load along with the sizable chunk of site-wide formatting elements (i.e. div’s etc.)
Even if the browser caches these files and page snippets, the inclusion statement still represents an unnecessary amount of bandwidth when multiplied by the number of concurrent users.
However, if a page uses AJAX-loading to load new content into the existing page, all of the above example code would only be loaded once, including site-wide elements such as navigation menus and general formatting sections.
Instead, the AJAX would only request “page-specific” content such as a particular email’s content (in the case of Gmail) and insert it within the existing page, which only had to load once.
This way, once the common site elements are loaded into the browser, only the changing content needs to… well… change. This can lead to tremendous savings on bandwidth, which is important when many users are hitting the site at once.
(props for still reading this longer-than-normal article – hang in there!)
Another benefit: Processing speed
So, when the initial page is loaded, the included AJAX script for loading future content is compiled and cached, making it’s future execution very fast. This lowers the latency involved on the client-side when downloading future content to be loaded into the running page, and since bandwidth consumption is kept to that of the content’s usage only, the process is extremely fast compared to loading a full page each time.
If the server and web browser are configured properly, then the Apache mod_gzip/mod_deflate could further boost the speed of this process. This puts more of the loading process in the realm of processing and conserves bandwidth more, and qualifies as an enhancement since bandwidth is more of a scarce resource than processing power.
In other words, you’re more likely to see a quad-core on a DSL or shared cable connection than a Pentium III hooked into a T3 line, so lower bandwidth in favor of processing time in most situations while employing sniffing practices to ensure mobile accessibility.
Since only a negligible amount of users fall into that category anyway (including Googlebot and other crawlers), the loss of performance due to a few client requiring full page loads is relatively small.
So, as seen in the examples above, the use of AJAX content loading within web apps, as employed by Gmail and Facebook, has numerous advantages to the site and its usability while remaining transparent to the users.
Best of all, if done correctly, it can be unobtrusive and therefore non-impacting to a site’s SEO or accessibility.
Almost everything discussed here neglects the role and possible optimization in the server-side scripting involved in all this, but each web app is unique and therefore can be optimized to best suit the situation as far as server-side optimizations go: just keep the main page’s inclusion rate low so the initial page loads fast, and keep the “dirty work” required by the server-side AJAX handler to a minimum.
Also look into distributing the load between several scripts to handle different types of AJAX requests, reducing query-string parameters and the conditionals involved in their processing, and even look into distributed computing (mod_proxy and it’s family) for self-hosted web apps with large scalability requirements.
In addition, the AJAX-handlers can be easily made into a rudimentary API for a web app you may want to make available for an iPhone/Blackberry app, or if you wish to open it up for any client as Twitter and FMyLife.com have done.
I hope this will convince more app authors to move to this system of page loading. Until next time…
There are no comments yet, add one below.