How PHP solves large traffic and high concurrency

Source: Internet
Author: User
Tags apc dedicated server hosting nginx server nginx reverse proxy

First, verify that the server hardware is sufficient to support the current traffic.
Ordinary P4 server can generally support up to 100,000 independent IPs per day, if the traffic is larger than this, then you must first configure a more high-performance dedicated server to solve the problem, or how optimization can not completely solve the performance problem.

Second, optimize database access. Full static at the front desk, of course, the best, can not access the database at all, but for frequently updated sites, static often does not meet certain functions.
Caching technology is another solution, that is, the dynamic data stored in the cache file, dynamic Web pages directly call these files, and no longer access to the database, WordPress and Z-blog are heavily used this caching technology.

If you do not avoid access to the database, you can try to optimize query SQL for the database. Avoid using a statement like Select * from, each time the query returns only the results you want, avoiding a large number of SQL queries over a short period of time.

Third, prohibit the external hotlinking.
External Web site picture or file hotlinking often bring a lot of load pressure, so should strictly restrict the external to their own picture or file hotlinking, fortunately, can now simply through refer to control the hotlinking, Apache itself can be configured to prohibit hotlinking, IIS also has some third-party ISAPI that can implement the same functionality. Of course, forged refer can also be implemented by code to achieve hotlinking, but the current deliberate forgery refer hotlinking is not much, you can first not consider, or use non-technical means to solve, such as the image of the watermark added.

Four, control the download of large files.
Large file downloads can consume a lot of traffic, and for non-SCSI hard drives, a large number of file downloads can consume CPU, resulting in reduced responsiveness. Therefore, try not to provide more than 2M of large file download, if required to provide, it is recommended to put large files on another server.

V. Use different hosts to divert the main flow
Put the files on different hosts and provide different images for users to download. For example, if you feel that the RSS files occupy a large amount of traffic, then use services such as FeedBurner or Feedsky to put the RSS output on other hosts, so that others access to the traffic pressure is mostly concentrated on FeedBurner host, RSS does not occupy too much resources.

VI, use traffic analysis statistics software.
Install a traffic analysis and statistics software on the website to instantly know which places are consuming a lot of traffic and which pages need to be optimized, so it is necessary to have accurate statistical analysis to solve the traffic problems. For example: Google Analytics.

Constraints for high concurrency and high load: hardware, deployment, operating system, Web server, PHP, MySQL, testing

Deployment: Server detach, DB cluster and library table hashing, mirroring, load balancing

Load Balancer Classification: 1), DNS round-robin 2) Proxy server load balancer 3) Address translation Gateway Load Balancer 4) NAT load Balancer 5) Reverse proxy load balancer 6) Hybrid load Balancing

Deployment Scenario 1:

Scope of application: static content as the main web site and application system, high security requirements for the system of Web sites and applications.

Main server: Primary servers

The main body of the hosting program is under pressure to handle dynamic requests in the Web site or application system;

Push a static page to multiple publishers;

Push the attachment file to the file server;

High security requirements, static-based Web site, the server can be placed in the network of external shielding access.

DB Server: Database servers

Load database reading and writing pressure;

Data volume Exchange only with the primary server, shielding extranet access.

File/video server: File/video Server

The data flow which occupies the system resource and the bandwidth resource is larger in the bearer system;

Storage and reading and writing warehouses as large attachments;

As a video server will have the ability to automate video processing.

Publishing Server Group:

Only responsible for the release of static pages, hosting the overwhelming majority of Web requests;

Load balanced deployment via Nginx.

Deployment Scenario 2:

Scope of application: Web site or application system with dynamic interactive content as the main body; a website or application system with large load-pressure and sufficient budget;

Web server Group:

The Web service has no master-slave relationship and is a parallel redundancy design;

Load balancing via front-end load balancing device or nginx reverse proxy;

Partition dedicated file server/video server effective separation of light/heavy bus;

Each Web server can be connected to all databases via Dec, while dividing the master and slave.

Database server Group:

Relatively balanced load-carrying database reading and writing pressure;

The data synchronization of multiple databases is realized through the mapping of database physical files.

Shared disk/disk array

Unified Read and write for data physical files

Storage warehouse for large attachments

Ensure the overall system IO efficiency and data security through the equalization and redundancy of its own physical disk;

Scenario Features:

Through the front-end load balancing, the reasonable distribution of web pressure;

Through the separation of the file/video server and the regular Web server, the reasonable allocation of light and heavy data flow;

Through the database server group, the reasonable allocation database IO pressure;

Each Web server is typically connected to only one database server, and with Dec heartbeat detection, it can automatically switch to redundant data in a very short period of time

library server;

The introduction of disk array greatly improves the efficiency of system IO, and enhances the data security.

Web server:

A large portion of the Web server's resource footprint comes from processing Web requests, which, in general, is the pressure that Apache generates, and in the case of high concurrent connections, Nginx is a good substitute for Apache servers. Nginx ("Engine X") is a high-performance HTTP and reverse proxy server written by the Russians. In China, has Sina, Sohu Pass, NetEase news, NetEase Blog, Jinshan Carefree net, Kimshan word bully, Xiaonei, Yupoo album, Watercress, Thunder look at a number of websites, channels using Nginx server.

The advantages of Nginx:

High concurrent connections: The official test can support 50,000 concurrent connections, running in the actual production environment to the number of concurrent connections.

Low memory consumption: With 30,000 concurrent connections, the 10 nginx processes that are open consume 150M of memory (15m*10=150m).

Built-in Health check function: If a WEB server on the backend of Nginx proxy goes down, it will not affect the front-end access.

Strategy: Compared to the old Apache, we choose lighttpd and Nginx, which have smaller resource occupancy and higher load capacity of the Web server.

Mysql:

MySQL itself has a very strong load capacity, MySQL optimization is a very complex task, because this ultimately requires a good understanding of system optimization. We all know that database work is a large number of short-term query and read and write, in addition to program development needs to pay attention to build indexes, improve query efficiency and other software development skills, from the perspective of hardware facilities to affect the efficiency of MySQL is mainly from the disk search, disk IO level, CPU cycles, memory bandwidth.

Perform MySQL optimizations based on the hardware and software conditions on the server. The core of MySQL optimization is the allocation of system resources, which is not equal to the unlimited allocation of more resources to MySQL. In the MySQL configuration file we introduce several of the most notable parameters:

Changing the index buffer length (key_buffer)

Change the length of the table (read_buffer_size)

Sets the maximum number of open tables (Table_cache)

Set a time limit for slow queries (Long_query_time)

If the conditions allow, the general MySQL server is best installed in the Linux operating system, rather than installed in FreeBSD.
Strategy: MySQL optimization requires different optimization scenarios based on the database read-write characteristics and server hardware configuration of the business system, and can deploy MySQL's master-slave structure as needed.

Php:

1, loading as few modules as possible;

2, if it is under the Windows platform, as far as possible to use IIS or Nginx to replace our usual Apache;

3, install the accelerator (all by caching the PHP code precompiled results and database results to improve the execution speed of PHP code)
Eaccelerator,eaccelerator is a free open source PHP accelerator, optimized and dynamic content caching, improving the performance of PHP script cache performance, so that PHP script in the state of compilation, the cost of the server almost completely eliminated.

apc:alternative PHP Cache (APC) is a free, publicly available, optimized code cache for PHP. It is used to provide free, open and robust architecture to cache and optimize PHP's intermediate code.

Memcache:memcache is a high-performance, distributed memory object caching system developed by Danga Interactive for reducing database load and increasing access speed in dynamic applications. The main mechanism is to maintain a unified huge hash table in memory, memcache can be used to store data in various formats, including images, videos, files, and database retrieval results.

Xcache: The Chinese developed the cache,

Strategy: Install the Accelerator for PHP.

Proxy server (cache server):

Squid cache (squid) is a popular free software (GNU General Public License) proxy server and Web cache server. Squid has a wide range of uses, from caching related requests as Web server cache servers to increasing the speed of Web servers, to sharing network resources for a group of people, caching the World Wide Web, domain name systems and other network searches, to help network security by filtering traffic, to LAN via proxy network. Squid is primarily designed to operate on Unix-type systems.

Strategy: Install Squid reverse proxy server, can greatly improve server efficiency.

Stress testing: Stress testing is a basic quality assurance behavior that is part of every important software testing effort. The basic idea of stress testing is simple: not to run manual or automated tests under normal conditions, but to run tests with fewer computers or poor system resources. The resources that are typically used for stress testing include internal memory, CPU availability, disk space, and network bandwidth. Usually use concurrency to do stress testing.

Pressure test tools: webbench,apachebench, etc.

Vulnerability testing: Vulnerabilities in our system include: SQL injection vulnerability, XSS cross-site scripting attacks, and so on. Security aspects also include system software, such as operating system vulnerabilities, MySQL, Apache and other vulnerabilities, can generally be resolved through the upgrade.

Vulnerability Test Tool: Acunetix Web Vulnerability Scanner

Original: http://my.oschina.net/u/992626/blog/114422

How PHP solves large traffic and high concurrency

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.