8. HTTP Compression
HTTP compression allows content to be compressed on the server before transmission to the client. For resources such as text this can significantly reduce the size of the response message, leading to reduced bandwidth requirements and download times.
Some content types such as zip files or gif images are already compressed and do not respond well to compression. In fact, attempting to compress them can increase the size of the response message and waste CPU time on the server.
Compression is particularly useful where secure SSL connections are used, because it reduces the amount of content that has to be encrypted on the server and decrypted by the client.
Two compression algorithms are commonly used - deflate and gzip. HTTP clients indicate their support of compression using the Accept-Encoding header as shown here:
Accept-Encoding: gzip, deflate
A server will only compress content for clients that support compression and will set the Content-Encoding header so that the client knows which algorithm to use when reading the response body:
If you look at the response message for a compressed page you will see that the response headers are in clear, uncompressed text and the HTML content is in a compressed binary format:
HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html Content-Encoding: gzip Content-Length: 1253 Date: Wed, 25 Feb 2015 12:00:00 GMT [1253 bytes of binary compressed HTML data starts here]
Question: How much difference did compression* make to each of the following?
A. This page HTML page?
B. This image (smallpic.jpg)?
(* if you are accessing this page through a proxy server it may remove compression)
|A. This Page:||66%||
The textual HTML for this page compressed well using gzip and lead to a saving of about 66% (i.e. from 15 KB to 5 KB)
|B. Image: (smallpic.jpg)||-0.2%||
The jpeg image is already compressed and did not benefit from compression. In fact, it increased slightly in size from 2457 bytes to 2462 bytes)
Using HttpWatch with Example 8
To view the effect of HTTP compression on this page:
- Open the HttpWatch window by clicking on the HttpWatch icon on the toolbar
- Click on Record to start logging requests in HttpWatch
- Add a filter to capture only gzip compressed pages by adding a 'Response Headers Contain' condition with the value "gzip".
- Click on the Refresh this page button above
- You can see the raw response message by clicking on the Stream tab and the compression savings are displayed on the Content tab