Use etags to reduce web application bandwidth and load page 1/2

Source: Internet
Author: User
Tags md5 hash
ArticleDirectory
    • Code

Introduction

Recently, the public has been strongly interested in the rest-style application architecture, indicating that the elegant design of the Web has begun to attract attention. Now, we gradually understand the scalability and elasticity inherent in the architecture of the World Wide Web, and further explore ways to use its paradigm. In this article, we will explore a little-known tool that can be used by web developers. The unnoticeable "etag Response Header )", and how to integrate it into dynamic Web applications based on spring and Hibernate to improve the applicationProgramPerformance and scalability.

The Spring framework application we will use is based on petclinic. The downloaded file contains instructions on how to add necessary configurations and source code. You can try it on your own.

What is "etag "?

The HTTP specification defines etag as the entity value of the requested variable (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html -- Chapter 14.19 ). In other words, etag is a token that can be associated with web resources ). A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is separately responsible for determining what the mark is and its meaning, and transmitting it to the client in the HTTP response header.

How does etag help improve performance?

Smart server developers will use etags together with the "if-None-match" header of the GET request, so that the cache of the client (such as the browser) can be used. Because the server generates etag first, the server can use it later to determine whether the page has been modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.

The process is as follows:

    1. The client requests a page ().
    2. The server returns to page A and adds an etag to page.
    3. The client displays the page and caches the page together with etag.
    4. The customer requests page a again and passes the etag returned by the server in the last request to the server.
    5. The server checks the etag and determines that the page has not been modified since the last client request. The server returns the response 304 (not modified -- not modified) and an empty response body.

The rest of this article will show two ways to use etag in spring-based Web applications, which use spring MVC. First, we will use the servlet 2.3 filter and the MD5 checksum of the display view (rendered view) to generate the etag (implemented by a "simple" etag ). The second method uses a more complex method to track the model used in the view to determine the etag validity (an "in-depth" etag implementation ). Although we use spring MVC, this technology can be applied to any MVC-style Web framework.

Before we proceed, we will emphasize the technologies that are presented here to improve the dynamic generation of page performance. The existing optimization technology should also be considered as part of the overall optimization and application performance characteristics adjustment analysis. (See below ).

Top-down Web Cache

This article mainly involves using HTTP Cache Technology for dynamically generated pages. To improve the performance of Web applications, we should adopt a holistic, top-down approach. For this purpose, it is very important to understand the layers through which HTTP requests pass. The appropriate technologies to be applied depend on the hot spots you are concerned about. For example:

  • Use Apache as the front-end of the servlet container to process static files such as slices and JavaScript scripts. You can also use the fileetag command to create the etag response header.
  • Use Optimization Techniques for JavaScript files, such as merging multiple files into one file and compressing spaces.
  • Use gzip and Cache Control headers ).
  • To determine the pain points of your spring framework application, consider using jamonjavascemonitorinterceptor.
  • Make sure that you fully utilize the cache mechanism of the orm tool, so objects do not need to be frequently regenerated from the database. It is worthwhile to take the time to determine how to make the query cache work for you.
  • Make sure youMinimize the amount of data obtained in the database, Especially the large list. If each page only requests a small subset of the large list, the data of the large list should be obtained once by a page.
  • Minimize the amount of data put into the http session. In this way, the memory is released, and it will be helpful when the application cluster is deployed.
  • UseDatabase profiling)Tool to check what indexes are used during the query. During the update, the entire table is not locked.

Of course, the principle of Application Performance OptimizationFamous sayingYes: two measurements, one cropping (Measure twice, cut once ). Oh, wait. This is for woodworking! That's right, but it applies here too!

Etag filter content body

The first method we need to consider is to create a servlet filter that generates its etag mark based on the page ("View" in MVC) content. At first glance, any performance improvement obtained by using this method seems to be contrary to intuition. We still have to generate pages and increase the computing time for generating tags. However, the idea here is to reduce bandwidth usage. In the case of a large response time, such as the distribution of your host and client on both ends of the planet, this is largely beneficial. I have seen an application hosted on a server in the Tokyo office in New York. Its response time is 350 ms. As the number of concurrent users increases, this will become a huge bottleneck.

Code

The technology we use to generate a mark is to calculate the MD5 hash value from the page content. This is achieved by creating a package on top of the response. The package uses byte arrays to store the generated content. After processing the filter chain, we use the MD5 Hash Value of the array to calculate the mark.

The implementation of the dofilter method is as follows.
Public void dofilter (servletrequest req, servletresponse res, filterchain chain) throws ioexception,
Servletexception {
Httpservletrequest servletrequest = (httpservletrequest) req;
Httpservletresponse servletresponse = (httpservletresponse) RES;

Bytearrayoutputstream baos = new bytearrayoutputstream ();
Etagresponsewrapper wrappedresponse = new etagresponsewrapper (servletresponse, baos );
Chain. dofilter (servletrequest, wrappedresponse );

Byte [] bytes = baos. tobytearray ();

String token = '"' + etagcomputeutils. getmd5digest (bytes) + '"';
Servletresponse. setheader ("etag", token); // always store the etag in the header

String previoustoken = servletrequest. getheader ("If-None-match ");
If (previoustoken! = NULL & previoustoken. Equals (token) {// compare previous token with current one
Logger. debug ("etag match: Returning 304 not modified ");
Servletresponse. senderror (httpservletresponse. SC _not_modified );
// Use the same date we sent when we created the etag the first time through
Servletresponse. setheader ("last-modified", servletrequest. getheader ("If-modified-since "));
} Else {// first time through-set last modified time to now
Calendar Cal = calendar. getinstance ();
Cal. Set (calendar. millisecond, 0 );
Date lastmodified = Cal. gettime ();
Servletresponse. setdateheader ("last-modified", lastmodified. gettime ());

logger. debug ("Writing body content");
servletresponse. setcontentlength (bytes. length);
servletoutputstream SOS = servletresponse. getoutputstream ();
SOS. write (bytes);
SOS. flush ();
SOS. close ();
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.