Use Etags to reduce Web application bandwidth and load 1th/2 page _php tips

Source: Internet
Author: User
Tags md5 md5 hash wrapper advantage

Introduced

The public has recently shown a strong interest in the rest-style application architecture, suggesting that the web's elegant design is beginning to be noticed. Now we have come to understand the scalability and resilience inherent in the3W Architecture (Architecture of the World Wide Web), and to further explore ways to apply its paradigm. In this article, we'll explore a little-known tool that can be exploited by web developers, an unobtrusive "ETag response header (ETag Response header)", and how to integrate it into dynamic Web applications based on spring and hibernate, To improve application performance and scalability.

The Spring framework application we are going to use is based on the "Pet Clinic" (Petclinic). The download file contains instructions on how to add the necessary configuration and source code, and you can try it yourself.

What is "ETag"?

The HTTP protocol specification defines etag as the entity value of the requested variable (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html -chapter 14.19). Alternatively, ETag is a token (token) that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for determining what the token is and what it means and transferring it to the client in the HTTP response header.

How does ETag help improve performance?

Smart server developers use the "If-none-match" headers of etags and get requests, which can take advantage of caching by clients, such as browsers. Because the server first generates ETag, the server can use it later to determine if the page has been modified. Essentially, the client requires the server to authenticate its (client) cache by passing the token back to the server.

The process is as follows:

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

The remainder of this article will show two ways to leverage ETag in a Web application based on the spring framework, which uses spring MVC. First we will use the servlet 2.3 Filter, using the MD5 checksum (checksum) of the presentation view (rendered view) to implement the method of generating ETag ( an "obvious" ETag implementation ). The second approach uses more sophisticated methods to track the model used in view to determine etag validity ( an "in-depth" ETag implementation ). Although we are using spring MVC, the technology can be applied to any MVC-style web framework.

Before we go on, let's emphasize what's shown here is the technology that promotes dynamic page performance. Existing optimization techniques should also be considered as part of the overall optimization and application performance tuning analysis. (see below).

top-down Web caching

This article mainly involves the use of HTTP caching techniques for dynamically generated pages. When considering improving the performance of Web applications, you should take a holistic, top-down approach. For this purpose, it is important to understand the layers that the HTTP request passes through, and the appropriate technologies to apply depend on the hotspots you focus on. For example:

  • Use Apache as the front end of the servlet container to handle static files such as pictures and JavaScript scripts, and you can also create ETag response headers using the fileetag Directive .
  • Use optimization techniques for JavaScript files, such as merging multiple files into a single file and compressing spaces.
  • Use the gzip and cache control headers (Cache-control headers).
  • To determine the pain point of your Spring framework application, consider using jamonperformancemonitorinterceptor.
  • Make sure you take full advantage of the ORM tool's caching mechanism, so objects do not need to regenerate frequently from the database. Taking the time to determine how to make the query cache work for you is worthwhile.
  • Make sure you minimize the amount of data you get in the database, especially the large list. If each page requests only a small subset of the large list, then the data for the large list should be obtained once by one of the pages.
  • Minimizes the amount of data put into the HTTP session. This frees up memory and helps when the cluster is applied.
  • Use the database profiling tool to see what indexes are used at the time of the query, and the entire table is not locked at the time of the update.

Of course, the wisdom of application can be optimized: two measurements, one clipping (measure twice, cut once). Oh, wait, this is for carpentry! Yes, but it works here too!

ETAG Filter Content Body

The first way to consider this is to create a servlet Filter that produces its etag notation based on the content of the page ("View" in MVC). At first glance, any performance gains that you get with this approach seem counterintuitive. We still have to produce the page, and also increase the calculation time of the notation. However, the idea here is to reduce bandwidth usage. In a large response time scenario, if your host and client are distributed on both ends of the planet, this is largely beneficial. I have seen the Tokyo office using a New York server hosted on the application, and its response time reached the MS. This will become a huge bottleneck as the number of concurrent users grows.

Code

The technique we use to generate tokens is based on computing MD5 hash values from the page content. This is done by creating a wrapper over the response. The wrapper uses a byte array to hold the resulting content, and after the filter chain is processed, we use the MD5 hash value of the array to compute the notation.

The implementation of the Dofilter method is shown below.
Public voidDofilter (servletrequest req, servletresponse Res, filterchain chain)throws IOException,
servletexception {
HttpServletRequest ServletRequest = (httpservletrequest) req;
HttpServletResponse servletresponse = (httpservletresponse) res;

BytearrayoutputstreamBAOs =New Bytearrayoutputstream();
Etagresponsewrapper wrappedresponse = new Etagresponsewrapper (Servletresponse, BAOs);
Chain.dofilter (ServletRequest, wrappedresponse);

byte[] bytes = Baos.tobytearray ();

Stringtoken ='"'+ etagcomputeutils.getmd5digest (bytes) + ' ";
Servletresponse.setheader ("ETag", token);///Always store the ETag in the header

StringPrevioustoken = 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
Servletresponse.setheader ("Last-modified", Servletrequest.getheader ("If-modified-since"));
}Else{///Through-set Last modified
CalendarCal =Calendar. getinstance ();
Cal.set (Calendar. Millisecond, 0);
DateLastModified = 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 ();
}
}

Current 1/2 page 12 Next read the full text

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.