Tiny Bits of Information

Monday, March 27, 2006

Ways to save Bandwidth with HTTP Compression

One of the issues that I am facing on www.earthcountries.com and I am sure most webmasters will have to face at some point whith their websites, is monthly bandwidth. Bandwidth is expensive and and can be used up pretty quickly, forcing many of us to additional payments towards our webhosting companies.

Besides the bandwidth problem, there is also the problem of download speed. Especially on new generation websites, which are more "heavy" than older generation websites, a single page can easily be over 100 kb. Big deal you might think, but consider 2 facts. That your website has many pages (well most do!) and that there is a signifact number of users out there who are not on broadband connections and they are using Dialup or low bandwidth connections to the internet. On a 56 kbits dialup connection, with an average of 5kbyte download ratio at best, a 100 kb page would take about 20 seconds to load. Well that is a long time to wait! even on a broadband connection of lets say 256 kbits (25 kbyte average) it might take for the page 4 seconds to load.

There is a solution that can satisfy both the webmaster, that wants to speed up his website and cut down bandwidth usage by us much as 60% or more and the website users, that want fast loading websites with a lot of content. The magic word is Compression!
For compression to work, both the server and the client need to understand it and support it. There has been in our web browsers for years! support for compressed web pages, and as for the servers, there are a number of tools out there that can enable them to server compressed content to users.

The most widely used compression tool on webservers is mod_gzip for apache (there are also solutions available for Microsoft IIS servers, but I will talk about them on other guides if you wish, as they are commercial tools)

Mod_gzip is a module for apache (existing on both windows and unix/linux versions of apache) that can significantly lower the size of a webpage on the fly, when it is requested by a browser that can understand compressed content (almost all modern browsers do support compression). Remember, the great thing is that the compression on the server is activated only if the browsers requests compressed content, in case the browser does not understand compressed content or does not request for it, the server simply servers plain, uncompressed content!

Most people claim that setting up compression on the apache is a pain, but i think it is very easy and for webmasters, which have a basic level of knowledge about apache, it will be an easy task. I can write a guide if there is such request from the users on how to setup apache on unix/windows with mod_gzip enabled.
Also, besides mod_gzip there is also mod_deflate, which is similar to gzip but some people claim that it has even better compression ratios.

For those of you out there which do not have your own webservers, mod_gzip is most of the time supported by your provider servers. Just ask them to enable it. It is only logical that most providers do not enable it by default, as it will save you bandwidth (and loose them money since they charge you for that extra bandwidth). In case that your hosting provider does not enable mod_gzip for some reason, do not despair.

There is another magic word that can save your day!
The word is: PHP

Believe it or not, a single line of PHP on the header of a webpage can compress the whole page (just like mod_gzip would).
So, in order for the PHP compression to work, you should insert the following line on the header of your webpages, on the top that is of your page :

PHP code

Thats it! with that simple line, the html content of your webpage will be compressed on the fly! And the good news is that PHP is enabled on most of the provider servers out there (well if you have a joomla/mambo server that it definately is!)

Regarding CPU usage and the speed of compression, it is not something that you have to worry about if you use mod_gzip, mod_deflate or the php function above. In all cases, the CPU usage is minimal to zero, and the response time of your server will be even better than before. An 100 kbyte webpage, when compressed can go to almost 20 k or less, depending on the content, as the text has always very high compression ratios.

For real life examples, the 100 kbyte page that would take 20 seconds to load on a dialup user browser and 100 kbyte of your bandwidth, with compression would drop down to 20 kbyte of bandwidth and 4 seconds of loading time for the user! As for the broadband user? the page will load in 1 second! Pretty good for FREE dont you think?

Thats it for now! If you think that is guide was helpful, i might be able to write down a more detailed guide on how to implement and how to test compression functionality on your web servers.

Kensai,
www.earthcountries.com

Original Article found on www.earthcountries.com

0 Comments:

Post a Comment

<< Home