The soul of a large web site-performance

Source: Internet
Author: User
Tags prefetch website server

What is performance
Some people say that performance is the speed of access , which is the most intuitive statement, but also the user's real experience.
A user from the input URL to press ENTER to see the speed of the page, which is performance. For us, we need to dig into this process,
Because this determines how we do performance optimizations. What happened in the middle of this?

The entire process of user access to the site:

The user enters the site domain name----through DNS resolution--to locate the destination server IP--request data reaches the target server via the Internet--and the target server receives the request data and processes (executing program, accessing database, file server, etc.).

The processing is completed, the response data is returned to the user's browser via the Internet, and the browser gets the result to render the calculation rendering to the user.

We divide the whole process into three-segment paths:
    1, the first paragraph in the user and browser side, is mainly responsible for issuing user requests, and receiving response data for calculation rendering display to the user;
2, the second paragraph on the network, is responsible for the request data, the response data transmission;
3, the third paragraph in the website server side, is responsible for the request data processing (executes the program, accesses the database, the file and so on), and returns the result;
  

 First path:
The time taken by the first path includes the time the input domain name originated the request and the time that the browser calculated the render when it received the response.
Enter the domain name to initiate the request, the actual process is:
1, the user in the browser input to visit the website domain name;
2, the local DNS request the DNS server authorized by the site to resolve the domain name, and get the result of the resolution is the IP address (and the IP address cache).
3. Make a request to the destination IP address.

    

    From this process we can see that the optimization is mainly to reduce the number of DNS resolution, and if the user browser set the cache, then the second time to access the same domain name will not go to request the DNS server, directly with the IP address in the cache to make the request.

So this process depends primarily on the browser settings. Now the main browser defaults to the DNS prefetch feature (DNS Prefetch), of course, you can also proactively tell the browser my site needs to do DNS prefetching:

<meta http-equiv= "X-dns-prefetch-control" content= "on"/>

The process by which the browser evaluates the data for rendering:
1, browser parsing response data;
2, the browser to create a DOM tree;
3, the browser download CSS style, and apply to the DOM tree, to render;
4, the browser download JS file, start parsing execution;
5, display to the user.

    from this process, we can find a lot of places that can be optimized. First, we can try to control the page size, so that the browser parsing time is shorter, and the multiple CSS files, JS file file merge compression to reduce the number and size of file downloads, but also note that the CSS in front of the page,JS access to the page behind, This makes it easier for the page to render first, and then to execute the JS script for a better experience for the user. Finally, I can set up the browser cache and read the content from the cache on the next visit, reducing the HTTP request.

<meta http-equiv= "Cache-control" content= "max-age=5"/>

This code illustrates that the browser is enabled for caching and does not access the server again for 5 seconds. Note that the settings for the cache need to be configured appropriately with your business features. CSS styles are usually placed in front of HTML and merged, and most JS files are placed at the end of the page.

  Second path:
The second path on the network , the time spent also includes the transmission time of the request data and the transmission time of the response data, this two time depends on the speed of data transmission .
Here we are going to talk about a noun "bandwidth". What is bandwidth, we often say bandwidth 10M, 20M what mean? My bandwidth is 20M, what does that mean? We know that the bandwidth speed is divided into upstream and downstream speed, that is, upload and download speed. Bandwidth 20M for the user is the download speed of 20M (20x1024x1024 bit rate), converted into byte 20m/8=2.5m. That is to say, 20M bandwidth download speed theory can reach 2.5m/s, and for home users, upload speed is generally much smaller than the download speed, about less than one-tenth. For the Web server (Enterprise users), otherwise, the general upstream speed equals the download speed. This is also the operator according to the actual needs of distribution, after all, the main user needs is to download data, rather than upload data.
The whole process is viewed in terms of transmission:
The user sends the request data (upload), the Web server accepts the request data (download), the Web server returns the response data (upload), and the user accepts the response data (download). For the user, the upload data is very small (URL parameters), and the download data is large (response data), for the server, the download data is very small (URL parameters), the upload data is large (response data). Understanding this, we can explain why sometimes users reflect why they have enough bandwidth, but open some sites is still very slow, because although the user's download speed, but the site server upload speed is very slow, it is like a pipe and a water outlet, no matter the pumping pipe is big, but the pipe is very small, The same amount of water to be pumped is limited. Understand this principle we see how to improve the speed of data transmission, the first user upload, download speed We are unable to determine, we can determine the site server upload, download speed, so we can do is appropriate to increase the server bandwidth (bandwidth is very expensive, the blind increase will only increase the unnecessary cost).

Purchasing the right bandwidth depends on the business features, size, and experience of the operator. An algorithm that can usually be considered, that is, based on the size of a single response data, multiplied by the number of PV, divided by the corresponding peak time period, so as to roughly estimate the site bandwidth requirements.
Let's go further into the second path:

Represents the general situation of the network when the user accesses the Web server, it can be seen from the diagram that assuming that the Web server from the telecommunications network access, and user A as a telecommunications broadband users, you can quickly access the backbone of the telecommunications network to the Web server. User B, user C as mobile and Unicom users
The server needs to be accessed through a longer path through the operator's interconnection.
 

For this scenario, we can use the following methods to optimize:
    1, in the developed regions of the operators IDC (Internet Data Center, can be understood as a computer room) to deploy the Web server, the user of each operator can access the server through their own backbone network.

2, the purchase of agent services, that is, the original Unicom users need to pass through the backbone of Unicom--unicom interconnection routers---telecom backbone---Web site server process. Through the proxy service, the proxy server connects directly to the telecom backbone and accesses the website server.

2, in the major regions of the city to purchase CDN services, cache the corresponding data, users can first obtain the request data from the nearest CDN operator.

Third path:
The third path is the process of internal processing of the Web server , including the execution of programs, access to files, databases and other resources.
This is the best place for us to play:
1, using the cache, depending on the need to use local cache or distributed cache;
2, using asynchronous operation, this way can not only improve performance, but also improve the scalability of the system;
3, code optimization;
4, storage optimization;
Caching: If you have less cache data, you can use Oscache to implement a local cache:

    

When there is too much cache data, use memcached to implement distributed caching:

    

Memcached implement distributed cache, the cache server is non-communication between, that is, we can easily increase the Memcached server to expand the system.

Asynchronous operation

    

The use of synchronous requests, in high concurrency, can put a lot of stress on the database and make the user feel a long response time. Asynchronous request mode, you can quickly respond to the user, and the specific database operation request, the Message Queuing server sent to the database server, do a specific insert operation. The result of the insert operation has otherwise been notified to the client. For example, in a booking system, the ticketing is done asynchronously, and the final result is communicated to the user by email or other means.

Storage optimization
Large-scale web site in the vast amount of data read and write to the disk caused great pressure, the system's biggest bottleneck is the disk read and write. Consider using disk arrays, distributed storage to improve storage performance.
Performance indicators and tests above through the analysis of user access to the site process to think about how to improve user-perceived performance, for the user to say performance is fast and slow. But for us, it can't be described in such a nutshell, we need to quantify him and measure it with some data metrics.

Here are a few nouns: concurrency, throughput, response time.
Concurrency: How many user requests can be processed by the system at the same time;
Throughput: The number of requests processed by the system within a unit of time;
Response Time: is the time when the user makes a request to receive the response data;


In order to understand these three concepts in a popular way, we take the toll station of Expressway as an example:

 response time: refers to a car through the toll station time, that is, the vehicle from entering the toll station, pay, opening, leaving the toll station time;

  Concurrency: refers to the toll station at the same time the number of vehicles can be understood as a toll station export quantity.

  throughput: refers to how many cars the toll station leads to over a period of time.


Describes the process of performance testing:

    

The left graph shows the two-dimensional coordinate graph of response time and concurrent user volume, which can be seen from the diagram:
The concurrent user volume increases, the response time is very short, and there is not too much fluctuation, this means that the system is currently in the daily running period, can quickly process the user request (before A point), with the increase in concurrency, the system is in peak demand, but still can be ordered to process user requests, ring
The time is higher than usual (A, b); When concurrency increases to a certain number, the system is at the brink of collapse (between B and C), and the response time is too long until the system crashes, exceeding the system's load capacity.
The graph on the right represents a two-dimensional coordinate graph of throughput and concurrent user volume, as you can see:
As the number of concurrent users increases, the throughput increases gradually, and as the concurrency reaches a certain amount, the throughput is slowed by the maximum system processing power, and when the concurrency exceeds the system load (E-point), the system processing power begins to decline, and the throughput is reduced without requesting additional user requests.

Summary
This article through the user visits the website process, has analyzed the three path process to raise the high performance the idea and the method, finally introduced the description performance index, and has made the brief explanation to the performance test.

The soul of a large web site-performance

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.