The core elements of the site architecture are: performance , availability , scalability , scalability, and Security, and performance is the most important This article briefly describes the site performance optimization needs to do some things;
1. Overview of website performance issues
Performance issues |
Description |
Cause |
Are mostly generated when users have high concurrent access . |
Main work |
Improve website access speed with high concurrent user access |
Main purpose |
Improve the user experience, so that users feel that the site quickly, all products must stand in the user's perspective to consider the problem |
2. Website Performance test
At different points of view, the performance of the site concerned is inconsistent:
Perspective |
Focus Point |
Description |
User perspective |
The user opens the browser webpage The response speed, the webpage can open in a long time, generally more than 3 seconds will feel more slowly |
User-perceived time mainly includes network communication, server processing, browser parsing time |
Development perspective |
Focuses on the performance of the application itself and its subsystems |
For example, the application itself is time consuming, concurrency, program stability, etc. |
Operation and Maintenance perspective |
More focused on infrastructure performance and resource utilization |
such as carrier bandwidth capability, server hardware configuration, network, server resource utilization, etc. |
Standing in the development, testing personnel perspective, performance testing the main indicators: response time , concurrency number , throughput , server performance indicators ;
Performance Index |
Description |
Test method |
Response time |
The time it took to start the request to receive the last response data |
Generally calculates the total response time spent on multiple repeated requests, divided by the number of requests |
Concurrency number |
The system can process the number of requests at the same time, and it also represents the number of simultaneous users |
Multithreaded simulation of concurrent users |
Throughput |
The number of requests processed per unit of time, reflecting the overall processing capacity of the system, usually using TPS (number of transactions per second), HPS (processing HTTP requests per second), QPS (number of processing queries per second) |
Increase the number of concurrent numbers to see the change curve of response time |
Server performance Metrics |
Includes system load, memory usage, disk usage, CPU usage, network I/O, etc. |
setting thresholds, exceeding threshold alarms |
Resource consumption and TPS performance graphs:
Concurrent user Access Response time graph:
3. Performance optimization
According to the performance test, locate the specific causes of performance problems, find the bottleneck point, and gradually optimize;
General performance optimization is divided into web front-end performance optimization, Application server performance optimization, storage server performance optimization;
Web Front End performance optimization
1. Browser Access Optimization
Optimization method |
Cause of performance problems |
Main means |
Reduce HTTP Requests |
HTTP stateless, each request server needs to start a separate thread to handle, the cost is large |
Merge css, js files, merge pictures (CSS offsets can be used to solve display problems) |
Using browser caching |
Static resource update frequency is low, should not be re-acquired every time |
Set the Cache-control and Expires properties in the HTTP header information to set the browser cache |
Compression |
Some useless spaces in static resources, such as carriage returns occupy a large number of bytes, resulting in each network transmission waste unnecessary traffic |
CSS, JS and other files can be compressed using gzip |
CSS and JS location |
Browser is to download the full CSS before the page rendering, loading JS immediately after the execution |
Generally put CSS on the top of the page, JS placed at the bottom of the page |
Reduce cookie Transmission |
Cookies are included in each request and response, and too large a cookie affects the data transfer |
Minimize the amount of information transmitted in a cookie, use a separate domain access for static resources, turn off cookies |
2. CDN Acceleration
Above said, theessence of CDN is still the cache , the data cached in the nearest user room, improve access speed, reduce the central room server pressure;
CDN can cache the general is static resources , tablets, files, videos, CSS,JS, etc., will visit the high frequency of static resources into the CDN;
3. Reverse Proxy
Reverse proxy action |
Description |
Secure Your site |
The first tier of all requests arrives is the reverse proxy server, which isolates the user and the Web server |
Cache |
Cache static resources on a reverse proxy server, easing Web server stress and increasing access speed |
Load Balancing |
Application server has more than one, using reverse proxy to do load balancing is a good choice, such as Nginx |
Application Server performance Optimization 1. Distributed cache
website Performance Optimization First Law : Prioritize using caching to optimize performance
The nature of the cache is the memory hash table, the data is stored in key/value form in the hash table, the time complexity O (1), the hash table is stored as shown in:
As long as the cache, it will involve cache misses and cache failures, so the data in the cache is generally read in a very high proportion, very little change of data;
Unreasonable use of the cache does not make sense and may degrade site performance, and the possible impact of unreasonable use of the cache is shown in the following table:
Unreasonable use of caching |
Reason |
Frequently-modified data |
Caching will soon expire, increasing the burden on the system |
No access to hotspots |
Do not follow the 28 law, all the data access frequency is basically the same situation, using the cache basically meaningless |
Inconsistent data and dirty reads |
The cache will set the expiration time, reload after time-out, and also cause data inconsistency in a short time, such as modifying the data, synchronizing the cache in real time, and causing large system overhead problems, which need to weigh |
Cache availability |
When the cache large area fails or the cache service crashes, it will cause sudden high concurrent access to the backend database, which may cause the database server to be down and cause avalanche, and can improve the availability of the cache through the distributed Cache server cluster; |
Cache penetration |
Improper business or attack, persistent request does not exist data, the cache does not have the data, all requests fall on the database server, resulting in an avalanche, can be no data is also cached, set to NULL to resolve the problem; |
Distributed Cache-memcached, distributed Memory object cache system, k/v storage, specific flow:
1. Check if the data requested by the client is present in the Memchahe, and if present, return the data directly; 2. If the request data is not in Memcache, the database is queried, the data obtained from the database is returned to the client, and the data is cached to memcache;3. Update the database at the same time and update the data in Memcache to ensure data consistency; 4. When the allocated space is used, replace the data with the LRU policy;
memcached distributed algorithm--consistent hash, no longer discussed, relatively simple;
Memcached service-side communication module based on Libevent (support event-triggered network communication Program Library), the server cluster between non-communication, can do linear scaling;
2. Asynchronous operations
can be usedMessage QueuingThe request is invoked asynchronously, the sending request is sent to the message queue and returned immediately, and the consumer process of Message Queuing obtains data from the message queue, asynchronously writes to the database; In high concurrency, the use of Message Queuing can effectively reduce the database server pressure, reduce the user-side response delay Message Queuing can eliminate high-concurrency access spikes, as shown in the peak effect: 3. Using the cluster
Under high concurrency, load balancing technology can be used to build application server clusters, distribute requests to multiple application servers to handle, reduce the pressure on a single server, and improve response speed;
4. Code optimization
Focus Point |
Description |
Optimization |
Multithreading |
Multithreading advantage is to make full use of CPU resources to speed up request processing speed; For Web applications, multi-threaded user requests are usually managed by the Web server container; The most important issue: multithreading security |
Optimal number of threads: [Task Execution time/(Task execution time-io wait time)] * CPU core count; Multithreading must be locked for resource modification |
Resource Reuse |
Minimizing costly system resource creation and destruction, such as database connections, network communication connections, threading, complex objects, etc.; |
Resource reuse is primarily used: 1. Singleton (for example, objects in spring that are constructed by default are singleton) 2. Object pooling (such as various connection pools, thread pools, because connections, threads are objects, in fact all pools are object pools) |
Data |
The use of good data structures and algorithms is the core of program performance Assurance |
—— |
Garbage collection |
Try to understand the garbage collection algorithm used in the language, understand its nature, in the design process can avoid some bad design, help program optimization and parameter tuning, write memory security code |
The popular garbage collection algorithm mainly consists of reference counting, mark clearing and generational recycling, etc. |
Storage server Performance Optimizations
In many cases, disk access speed becomes the bottleneck of the whole system, and the data on the disk is the most important asset of the website, so the fault tolerance and usability of the disk are very important;
- appropriate use of SSDs;
- Reasonable use of RAID (RAID0,RAID1,RAID10,RAID5,RAID6, etc.)
- Rational use of Distributed file systems such as HDFs
Performance optimization of website architecture