How to optimize web requests

This article describes the various stages involved in a web request, as well as optimization steps you can take to improve performance.

Performing a DNS lookup

A DNS lookup is the process of converting a domain name (such as example.com) to an IP address (such as 93.184.216.119). For a web request, this means obtaining the IP address of the server that hosts the website for a given domain name.

Your browser starts a web request by querying its resolver where a domain name is. If the IP address is cached, the resolver returns the IP address immediately. If the IP address is not cached, or if the cache has expired (as determined by the TTL value in the domain's DNS zone), the resolver asks the root DNS servers for the IP address.

Improving performance

Using a reliable DNS server is essential for obtaining the quickest IP address resolution of a domain name. We recommmend you use A2 Hosting's name servers for your domain.

Additionally, setting the correct Time to Live (TTL) values for domain records in your DNS zone is a good way to ensure that DNS root servers do not constantly look up where your server is. The TTL value should be set to less than a day, but more than an hour.

  • If you know that you will be using a new IP address soon, you can lower the TTL value ahead of time so the move happens faster. After the move is complete, set the TTL value back to a value between 4 to 6 hours.
  • For information about how to manage DNS zone records using the A2 Hosting Customer Portal, please see this article.

Establishing a connection

A browser and a server must establish a connection before they can communicate with each other. This connection is known as a TCP handshake, which consists of a three-step process: SYN (synchronized), SYN-ACK (synchronized-acknowledgement), and ACK (acknowledgement). The browser sends a SYN, the server responds with a SYN-ACK, and the browser responds with an ACK. When this three-step process is finished, an Apache web server thread is assigned to the browser.

Improving performance

The best way to ensure a quicker connection time is to make sure that your server is located relatively close to your customer base. There are many packets traveling back and forth between the server and the browser, so if the physical distance between the browser and server is too great, the latency of each packet increases the connection time.

For example, if your customers are located in Europe, try to use a server located in Europe.

Negotiating the SSL handshake

During an SSL handshake, the browser and server establish encrypted communication between each other using a multi-step process:

  1. The browser sends the server information about its SSL version and settings that the server needs to open an SSL connection. The server sends the same information to the browser, as well as its certificate.
  2. The browser checks whether the server's certificate is valid. The browser then creates a temporary secret key encrypted with the server's public key, and then sends the encrypted key to the server.
  3. The server uses its private key to decrypt the temporary secret key generated by the browser, and then the client and server generate a master secret key from the temporary key.
  4. Both the browser and the server use the master secret key to generate the session keys, which are symmetric keys used to encrypt and decrypt information exchanged during the SSL session. These keys are also used to verify that data has not been altered during transit.
  5. The browser and server inform each other that from now on, all communication between them will be encrypted, SSL negotiation is complete, and requests can be sent.
Improving performance

As with connection times, the best way to improve SSL performance is to strategically position the server close to your customer base. For example, if your customers are located in Europe, try to use a server located in Europe.

Sending a request

The browser generates a request to the server, which includes the URL, cookies, sessions, and headers that describe what forms of responses the browser will accept from the server. The browser also sends any form data, including file uploads. If SSL is used for the connection, the request is encrypted.

The server then sends confirmation that it has received the request.

Improving performance

To improve request time performance, you can:

  • Limit the amount of information stored in cookies. This decreases the request size, when means it can be sent faster.
  • Only send necessary information in web forms; do not include unnecessary hidden fields. You can use session cookies to store user-specific information on the server between form requests, instead of passing variables along in web forms.

Wait time

Wait time is the time between the moment the server acknowledges the request, and the time that the browser receives the first byte which contains the start of the response headers from the server. This step includes script processing and database calls.

Some scripts return the response header before any processing or database calls; this is generally indicated by an extremely short wait time of less than 10 milliseconds. When headers are sent before content, the time that would normally be in the wait time is added to the receiving or download time.

Improving performance

You can make most improvements in wait time at the application level. How the page is rendered in the code and how the site runs database queries are the largest factors in improving performance.

If your site uses a popular CMS (content management system) like WordPress, Drupal, or Magento, make sure that you do not have any unnecessary plugins or extensions active on your site. On WordPress, for example, you can use GTMetrix tools to determine which plugins or theme extensions are slowing down your site. New Relic is another tool that you can use on any site to help Identify slow functions and database queries.

Regardless of your application's speed, the largest performance gain can be made by using a caching engine. Caching engines store a copy of the rendered HTML for each page on your site. They serve fresh content after a specified amount of time, or purge a cached copy of a page when the page has been modified. Remember that the first person to visit a page after a cache has expired may have a longer wait time while it serves the fresh content and saves a new copy for the cache.

Many CMS’s have plugins that can automatically cache a site's content:

  • W3 Total Cache for WordPress.
  • Boost extension for Drupal.
  • Cache Storage Management Panel for Magento.
  • Joomla has a built-in system cache plugin (from the Joomla administration page, click Extensions, click Plugin Manager, and then locate the System - Cache plugin).

Receiving a response

This is the amount of time it takes a browser to download the content generated by the server. Remember that this value can be larger than expected if the server sends information immediately as it is generated, instead of generating all of the content and then sending it.

Improving performance

To improve performance, enable minification in your source files and use Gzip compression.

You can run your HTML code through a minifier before you upload it to the web server. A minifier like HTMLTidy removes comments and unnecessary whitespace from a document to ensure that it is as compact as possible. CssTidy and JSTidy reduce the size of CSS and JavaScript files. In Wordpress, W3 Total Cache provides minification by combining all CSS files into a single CSS file, as well as JavaScript files. while removing whitespace and any duplicated rules.

To use Gzip commpression, make sure that you are using the mod_deflate module.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.