How to optimize website and build high Performance website

Source: Internet
Author: User
Tags array length garbage collection memcached message queue send cookies serialization browser cache

When it comes to website optimization is actually a very big concept, site optimization means too much, a small code merging is also considered the optimization of the site, below I from the front end of the site to the back end of each part of the optimization method.
optimization of browser access
1. Reduce HTTP Requests
HTTP protocol is a stateless protocol working at the application layer, which means that every HTTP request must establish a communication link to carry out data transmission, and on the server side, each HTTP request should initiate an independent thread to process. The cost of these communications and services is expensive, and reducing the number of HTTP requests can improve access performance effectively.
We can reduce the number of HTTP requests by merging JS, CSS, and pictures. If more than one picture, each picture has a different hyperlink, you can through the CSS offset response to mouse clicks, the construction of different URLs.
2. Using Browser caching
By setting the Cache-control and Expires properties in the HTTP header, you can set the browser cache, but note that when we update a large number of static resources, for example, we update 10 files to update all 10 files at once, but one for each update, In order to avoid a large number of user browser cache failure, centralized update cache, resulting in server load surges, network congestion.
3. Enable compression
File compression can reduce the number of data communications, the text compression rate can reach more than 80%, so HTML, JS, CSS enable gzip compression can achieve better results. But compression creates pressure on the server and browsers, and when the communication is good and server resources are low, you need to weigh
4, CSS on the top of the page, JavaScript placed in the bottom of the page
The browser will not render the page until it has finished downloading all of the CSS. JavaScript, in contrast, is executed immediately after the browser loads JavaScript, potentially blocking the entire page, causing the page to display slowly. But if the page parsing requires JavaScript, put it in the end is not appropriate, we adapt to local conditions, make reasonable arrangements
5, reduce the transmission of cookies
Cookies are included in every request and response, too large cookies can seriously affect data transmission, should be as much as possible the transmission of cookies. In addition, for some static resources to access, send a cookie does not make sense, you can consider static resources using independent domain name access, to avoid requesting static resources to send cookies, reduce the number of cookies transmitted.
CDN Acceleration
The nature of CDN (content Distribute network) is still a cache, and caching data in places closest to the user, speeding up user access and reducing load pressure on the data center.
Reverse Proxy
The traditional proxy server is located on the user's browser side, the proxy browser sends the HTTP request to the Internet, and the reverse proxy server is located on the computer room side, and the proxy Web server receives the HTTP request.

The reverse proxy server, like the traditional proxy server, also has the role of protecting network security, and requests from the Internet must go through a proxy server, which is equivalent to adding a barrier between the Web server and the network.
In addition to security features, the reverse proxy service can also cache static resources, speeding up the user's access speed
performance optimization for application servers
Optimization methods: Caching, clustering, asynchronous
1. Distributed caching
When the speed of our website access bottlenecks, we first think of the use of caching, caching exists in every corner of our site, page caching, static resource caching, data caching, file caching, etc.
So why is caching so fast, the complexity of getting data from the cache must be very low, we know the map's Get method time complexity is O (1), no matter how large map I get the data is always so fast. The nature of the cache and the map type are a hash table of memory, which is stored in a hash table in the form of a pair of key and value. The time complexity of data reading and writing of hash table is O (1).
The hash table index corresponding to the hashcode of the key in the KV pair is computed, and the data in the hash table can be accessed quickly. The Java Hashcode method is contained in object, returns the value int, then calculates the index subscript of the hash table by Hashcode, the simplest is the remainder method, uses the hash table array length to hashcode, the remainder is the index subscript of the hash table.

The cache typically holds data that is very high in reading and writing and has little change. The site's data usually follows the 28 law, 80% of the access is located in 20% of the data, the 20% of the data cache can be very high to improve system performance, improve reading speed and reduce storage pressure.
Let's talk about the issues that should be noted in cache usage
Frequently modified data is not suitable for caching
Non-hotspot data is not suitable for caching
Inconsistent data for a period of time, such as when we modify the product attributes, the cache in the validity period of the user read is always the old data, which requires us in our own business scene in the reasonable control.
Cache high availability, when a cache server fails, should access the data of another standby, and should not allow users to request a large number of access to the database, resulting in a short time to the database pressure or even downtime.
Cache warming
The cache is stored in hot data, and the hotspot data is filtered through the cached LRU (the most recently unused algorithm) to the data being accessed. If no data in the newly-launched cache is in the process of rebuilding the cached data, the system's performance and database load are not very good, so it is best to load the hotspot data when the caching system is started
Speaking of caching, a brief introduction to Memcached
Memcached Simple communication Protocol, remote communication design to consider two factors, one is communication protocol, even if the choice of TCP protocol or UDP protocol or HTTP protocol; First, communication serialization protocol, the two ends of the data transmission must use each other identifiable data serialization mode to make the communication complete, For example: XML, JSON, and other text serialization protocols, or Google's Protobuffer binary serialization protocol. Memcached uses the TCP protocol (also supported UDP) protocol, whose serialization protocol rules are a set of text-based custom protocols. For example, read a data command get (key).
Memcached High Performance network communication, based on Libevent, an event-triggered network communication program library.
Memcached's efficient memory management, the most vexing problem in memory management is memory fragmentation management. This problem also exists in JVM garbage collection, in order to solve this problem the JVM adopts algorithms such as compression and replication. and memcached used in the fixed space allocation.
Memcached divides the memory space into a group of slab (slices), each slab contains a set of chunk (blocks), the slab size in the same chunk is fixed, chunk with the same size slab is organized together, called Slab_class, When storing data, look for a minimum chunk that is greater than size to write data, depending on the size of the data. This memory management avoids the problem of memory fragmentation management, and the allocation and release of memory is chunk. Like other caches, Memcached uses the LRU algorithm to free up the data space, releasing the chunk that are marked unused and waiting for the next appropriate data to be written.
Of course this way will also bring the problem of memory waste, data can only be deposited in a larger than his chunk, the remaining space is wasted, if the startup parameter configuration unreasonable found that the amount of data memory has been depleted.
The memcached cluster data allocation (consistent hashing algorithm) is described in detail later.
2. Asynchronous operation
Use of Message Queuing asynchrony can significantly improve web site performance and throughput
Especially in high concurrency, a large number of users request data if the direct storage of the database will cause great pressure, if you use the asynchronous user request after the user to process, and then consumer message queue data warehousing
3. Using cluster
In the context of high concurrency in a web site, the use of load balancing technology to build a cluster of multiple servers for an application and distribute requests to multiple servers for processing. To avoid a single server due to high load and slow response to the user to bring a very poor experience, the Internet to fight is speed.
4, code optimization
4.1 Multi-Threading programming
Designing an object as a stateless object (object without member variables, such as a servlet) seems to violate object-oriented design concepts
Working with local objects
Use locks when concurrent access
4.2 Resource Reuse
The system runs as much as possible to minimize the creation and destruction of expensive system resources, such as database connections, network communication connections, threads, complex objects, and so on. There are generally two ways to reuse a resource: A single case (singleton) and an object pool.
4.3 Data structure
The reading and writing of the hash table depends on the higher the random read-write performance of Hashcode,hashcode
4.4 Garbage Collection
Garbage collection can have a significant impact on the performance of your system. Understanding the garbage collection mechanism helps with program optimization and parameter tuning and code to write memory security.
In the case of the JVM, memory can be divided into heaps (heap) and stacks (stack). The stack is used to store contextual information, such as method parameters, local variables, and so on. The heap stores the memory space of the object, and the creation and release of the object's garbage collection is done here. Most objects have a short-lived lifecycle, and the garbage generated by this part of the object should be collected faster to free up memory, which is the JVM's generational garbage collection.
In the JVM generational garbage collection mechanism, the heap space available to the application is divided into young generation (younger Generation) and older generations (old Generation), and the young generation is divided into the Eden, the From and to areas, and new objects are created in the Eden area, When the Eden space is full, a GC is triggered, and the objects that are also used are copied to the from area so that the entire Eden area continues to be created, and the Eden area is again filled, triggering a young GC, and the Eden and from areas are copied to the to area, and the next young The GC copies Eden and the to area to the from area. After several young GC, some objects are copied multiple times between the from and to areas, and if more than one of the threshold objects has not been released, the object is copied to the old generation. If the old generation is also used up, triggering the full gc,full GC has a greater impact on system performance and avoids it as much as possible.

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.