Saturday, May 19, 2012

Response compression in Apache (using mod_deflate)


In any web application's popularity it's performance plays a prominent role. To make sure that application has optimal performance, developer need to review the application, environment and target customer’s environment. Some solutions, which give performance boost for high bandwidth client, might fall flat for low bandwidth clients. If we are tuning our application for low bandwidth clients we need to minimize the network overhead in our application.

Among multiple ways to minimize the network overhead, response compression is one of the solutions. As response compression is not part of application’s business logic, we should not mix it with our application code.

With the advent of multi-feature servers, response compression can be configured in the servers now. Even browser supports the compressed response. The complete life cycle is managed in below steps –
  1. Client (i.e. browser) sends the request to the Server
  2. Sever passes the request to the application
  3. Application generates the response and provide it to server to be sent to the client (i.e. browser)
  4. Server checks whether the compression option of the specific mime type, being sent as response, has been configured.
  5. If yes, server compresses the data, add compression info into header and sends the compressed response to client.
  6. Client (i.e. browser) checks the header and knows that response it compressed, it decompresses it and renders/processes
 Apache web server provides an extension called mod_deflate to be used to achieve response compression of  specific type of mime object. To use it, we need to add below mentioned configuration in httpd.conf configuration file.

LoadModule deflate_module modules/mod_deflate.so

<Location />
   AddOutputFilterByType DEFLATE text/html text/plain text/xml application/javascript text/css
</Location>

Effectiveness – We achieve almost 75% compression, using mod_deflate. Below screenshots depict sample of compression level achieved using mod_deflate.

Response size before compression

Response size after compression - 

Places to avoid mod_deflate –
  1. Once should not use mod_deflate to compress images or videos.
  2. In high bandwidth environment, mod_deflate’s effect on performance should be checked before sending it to production. Because overhead of compression and de-compression activity (few milliseconds) might surpass the benefit of data transfer reduction. 
References -
Apache mod deflate - http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

No comments:

Post a Comment