Updated with links of .NET Best Practices No.: 1, 2, 3, 5, and video.
Bandwidth performance is one of the critical requirements for every website. In today’s time, a major cost of the website is not hard disk space but its bandwidth. So transferring maximum amount of data over the available bandwidth becomes very critical. In this article we will see how we can use IIS compression to increase bandwidth performance.
Please feel free to download my free 500 questions and answers videos which covers Design Patterns, UML, Function Points, Enterprise Application Blocks, OOP, SDLC, .NET, ASP.NET, SQL Server, WCF, WPF, WWF, SharePoint, LINQ, SilverLight, .NET Best Practices @ these videos http://www.questpond.com/.
How does IIS compression work?
Note: All examples shown in this article use IIS 6.0. The only reason we have used IIS 6 is because 7.0 is still not that common.
Before we move ahead and talk about how IIS compression works, let’s try to understand how normally IIS works. Let’s say the user requests a ‘Home.html’ page which is 100 KB in size. IIS serves this request by passing the 100 KB HTML page over the wire to the end user browser.
When compression is enabled on IIS the sequence of events changes as follows:
- User requests for a page from the IIS server. While requesting for the page the browser also sends what kind of compression types it supports. Below is a simple request sent to the browser which says its supports ‘gzip’ and ‘deflate’. We have used fiddler (http://www.fiddler2.com/fiddler2/version.asp) to get the request data.
GET /questpond/index.html HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel */*
Accept-Encoding: gzip, deflate
Depending on the compression type support sent by the browser IIS compresses data and sends the same over the wire to the end browser.
The browser then decompresses the data and displays the same on the browser.
Compression fundamentals: Gzip and deflate
IIS supports two kinds of compression: Gzip and deflate. Both are more or less the same, where Gzip is an extension over deflate. Deflate is a compression algorithm which combines LZ77 and Huffman coding. In case you are interested to read more about LZ77 and Huffman, you can refer to http://www.zlib.net/feldspar.html.
Gzip is based on the deflate algorithm with extra headers added to the deflate payload.
Below are the header details which are added to the deflate payload data. It starts with a 10 byte header which has a version number and a time stamp followed by optional headers for the file name. At the end it has the actual deflate compressed payload and 8 byte check sum to ensure data is not lost in transmission.
Google, Yahoo!, and Amazon use gzip, so we can safely assume that it’s supported by most browsers.
Enabling IIS compression
Till now we have done enough of theory to understand IIS compression. Let’s get our hands dirty to see how we can actually enable IIS compression.
Step 1: Enable compression
The first step is to enable compression on IIS. So right click on websites -> properties and click on the Service tab. To enable compression we need to check the below two text boxes from the Service tab of IIS website properties. The below figure shows the location of both the checkboxes.
Step 2: Enable metabase.xml edit
Metadata for IIS comes from ‘Metabase.xml’ which is located at “%windir%\system32\inetsrv\”. In order that compression works properly we need to make some changes to this XML file. In order to make changes to this XML file we need to direct IIS to gives us edit rights. So right click on your IIS server root -> go to properties and check the ‘Enable direct metabase edit’ check box as shown in the below figure.
Step 4: Set the compression level and extension types
The next step is to set the compression levels and extension types. The compression level can be defined between 0 to 10, where 0 specifies a mild compression and 10 specifies the highest level of compression. This value is specified using the
HcDynamicCompressionLevel property. There are two types of compression algorithms: ‘deflate’ and ‘gzip’. This property needs to be specified in both the algorithms, as shown in the below figures.
We need to also specify which file types need to be compressed.
HcScriptFileExtensions helps us to specify that. For the current scenario we specified that we need to compress ASPX outputs before they are sent to the end browser.
Step 5: Does it really work?
Once you are done with the above four steps, it’s time to see if the compression really works. So we will create a simple C# ASP.NET page which will loop “10000” times and send some kind of output to the browser.
protected void Page_Load(object sender, EventArgs e)
for (int i; i < 10000; i++)
Response.Write("Sending huge data" + "<br>");
In order to see the difference between before compression and after compression, we will run the fiddler tool as we run our ASP.NET loop page. You can download fiddler from http://www.fiddler2.com/fiddler2/version.asp.
The below screen shows data captured by fiddler without compression and with compression. Without compression data is “80501 bytes” and with compression it comes to “629 bytes”. I am sure that’s a great performance increase from a bandwidth point of view.
0, 1, 2, 3, 4…10 IIS compression levels
In our previous section we set
HcDynamicCompressionLevel to a value of 4. More the compression level value, the less the data size will be. As we increase the compression level the downside is more CPU utilization. One of the big challenges is to figure out the optimum compression level. This depends on a lot of things: type of data, load, etc. In our following section we will try to derive which is the best compression level for different scenarios.
Three point consideration for IIS compression optimization
Many developers just enable IIS compression with the below default values. But default values do not hold good for every environment. It depends on many other factors the content type your site is serving. If your site has only static HTML pages then compression levels will be different as compared to a site serving mostly dynamic pages.
|Compression Options|| File Type|| Default Configuration|
|File types compressed ||Static||.txt, .htm, and .html|
| ||Dynamic|| |
|Compression schemes||Static||Both gzip and deflate|
| ||Dynamic||Both gzip and deflate|
|Compression level ||Static||10|
The above table is taken from http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/25d2170b-09c0-45fd-8da4-898cf9a7d568.mspx?mfr=true.
If your site is only serving compressed data like ‘JPEG’ and ‘PDF’, it’s probably not advisable to enable compression at all as your CPU utilization increases considerably for small compression gains. On the other side we also need to balance compression with CPU utilization. The more we increase the compression levels the more CPU resources will be utilized.
Different data types need to be set to different IIS compression levels for optimization. In the following section we will take different data types, analyze them with different compression levels, and see how CPU utilization is affected. The below figure shows different data types with some examples of file types.
Static data compression
Let’s start with the easiest one, static content types like HTML and HTM. If a user requests for static page from IIS with compression enabled, IIS compresses the file and puts it in the ‘%windir%\IIS Temporary Compressed Files’ directory.
Below is a simple screen which shows the compressed folder snapshot. Compression happens only for the first time. On subsequent calls, the same compressed data is picked from the compressed files directory.
Below are some sample readings we have taken for HTML files of size ranging from 100 KB to 2048 KB. We have set the compression level to ‘0’.
|Actual KB||Compressed in KB|
|Compression level set to ‘0’|
You can easily see that with the least compression level enabled, the compression is almost five times.
As the compression happens only for the first time, we can happily set the compression level to ‘10’. The first time we will see a huge CPU utilization, but on subsequent calls the CPU usage will be small as compared to the compression gains.
Dynamic data compression
Dynamic data compression is a bit different from static compression. Dynamic compression happens every time a page is requested. We need a balance between CPU utilization and compression levels.
In order find the optimized compression level, we did a small experiment as shown below. We took five files in the range of 100 KB to 2 MB. We then changed compression levels from 0 to 10 for every file size to check how much the data was compressed. Below are compressed data readings in bytes.
|100 KB||200 KB||300 KB||1 MB||2 MB|
The above readings do not show anything specific, it's a bit messy. So what we did is plot the below graph using the above data and we hit the sweet spot. You can that see even after increasing the compression level from 4 to 10 the compressed size has no effect. We experimented this on 2 to 3 different environments and it always hit the value ‘4’, the sweet spot.
The conclusion we draw from this is, setting value ‘4’ compression level for dynamic data pages is an optimized setting.
Compressed file and compression
Compressed files are files which are already compressed. For example, files like JPEG and PDF are already compressed. So we did a small test by taking JPEG compressed file and below are our readings. The compressed files after applying IIS compression did not change much in size.
|Actual compressed file size||File size after IIS compression|
When we plot a graph you will see that the compression benefits are very small. We may end up utilizing more CPU processor resource and gaining nothing in terms of compression.
So the conclusion we can draw for compressed files is that we can disable compression for already compressed file types like JPEG and PDF.
CPU usage, dynamic compression, and load testing
One of the important points to remember for dynamic data is to optimize between CPU utilization, compression levels, and the load on the server.
We used WCAT to do a stress test with 100 concurrent users. For file size ranging from 100 KB to 2 MB we recorded CPU utilization for every compression level. We recorded processor time for the W3WP exe using a performance counter. To add this performance counter you can go to process -> select processor time -> select w3wp.exe from instances.
If we plot a graph using the above data we hit the sweet spot of 6. Till IIS compression level 6, CPU utilization was not really affected.
TTFB and Compression levels
TTFB, also termed as time to first byte, gets the number of milliseconds that have passed before the first byte of the response was received. We also performed a small experiment on 1MB and 2 MB dynamic pages with different compression levels. We then measured the TTFB for every combination of compression level and file size. WCAT was used to measure TTFB.
File size in MB
When we plot the above data, we get value ‘5’ as a sweet spot. Until the value reaches ‘5’ TTFB remains constant.
Print shot of WCAT output for TTFB measurement:
IIS 7.0 and CPU roll off
All the above experiments and conclusion are done on IIS 6.0. IIS 7.0 has a very important property, i.e. CPU roll-off. CPU roll-off acts like a cut off gateway so that CPU resources are not consumed unlimitedly.
When CPU gets beyond a certain level, IIS will stop compressing pages, and when it drops below a different level, it will start up again. This is controlled using the
dynamicCompressionDisableCpuUsage attributes. It’s like a safety valve so that your CPU usage does not come by surprise.
- If the files are already compressed do not enable compression on those files. We can safely disable compression on EXE , JPEG , PDF, etc.
- For static pages, compression level can be set to 10 as the compression happens only once.
- Compression level range can be from ‘4’ to ‘6’ for dynamic pages depending on the server environment and configuration. The best way to judge which compression level suits best is to perform TTFB, CPU utilization, and compression test as explained in this article.
If you want to do a sanity check please refer to this article: http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx. I agree my results do not match exactly with Scott's but I think we are very much on the same page.
Below are some known issues of IIS compression:
My other .NET best practices article
Thanks, Thanks, and Thanks
Links for further reading