How PHP solves the problem of high website traffic and high concurrency

巴扎黑
Release: 2023-03-03 12:06:02
Original
1953 people have browsed it

First, confirm whether the server hardware is sufficient to support the current traffic.
Ordinary P4 servers can generally support up to 100,000 independent IPs per day. If the number of visits is greater than this, you must first configure a higher-performance dedicated server to solve the problem. Otherwise, no matter how you optimize, it is impossible to completely solve the performance problem. .

Secondly, optimize database access. It is of course best to achieve complete staticization of the front desk, as there is no need to access the database at all. However, for websites that are frequently updated, staticization often cannot satisfy certain functions.
Caching technology is another solution, which is to store dynamic data in cache files, and dynamic web pages directly call these files without having to access the database. Both WordPress and Z-Blog use this caching technology extensively.

If it is indeed unavoidable to access the database, you can try to optimize the query SQL of the database. Avoid using statements such as Select * from. Each query only returns the results you need, avoiding a large number of SQL queries in a short period of time.

Third, external hotlinking is prohibited.
Hotlinking of pictures or files from external websites often brings a lot of load pressure, so external hotlinking of one's own pictures or files should be strictly restricted. Fortunately, at present, hotlinking can be controlled simply through refer, and Apache itself can Configure to prohibit hot links. IIS also has some third-party ISAPIs that can achieve the same function. Of course, fake referrals can also be used to achieve hotlinking through code. However, there are currently not many people who deliberately fake referrals to hotlink. You can ignore it for now, or use non-technical means to solve it, such as adding watermarks to pictures.

Fourth, control the download of large files.
Downloading large files will occupy a lot of traffic, and for non-SCSI hard drives, downloading a large number of files will consume the CPU and reduce the website's responsiveness. Therefore, try not to provide downloads of large files exceeding 2M. If you need to provide them, it is recommended to place the large files on another server.

Fifth, use different hosts to divert the main traffic
Place files on different hosts and provide different images for users to download. For example, if you feel that RSS files take up a lot of traffic, then use services such as FeedBurner or FeedSky to place the RSS output on other hosts. In this way, most of the traffic pressure of other people's access will be concentrated on FeedBurner's host, and RSS will not occupy too many resources.

Sixth, use traffic analysis and statistics software.
Install a traffic analysis and statistics software on the website, and you can instantly know where a lot of traffic is consumed and which pages need to be optimized. Therefore, accurate statistical analysis is required to solve the traffic problem. For example: Google Analytics.

High concurrency and high load constraints: hardware, deployment, operating system, web server, PHP, MySQL, testing

Deployment: server separation, database cluster and library table hashing, mirroring, load balancing

Load Balancing classification: 1) DNS round robin 2) Proxy server load balancing 3) Address translation gateway load balancing 4) NAT load balancing 5) Reverse proxy load balancing 6) Hybrid load balancing

Deployment plan 1:

Scope of application : Websites and application systems with static content as the main body; websites and application systems with high system security requirements.

Main Server: The main server

carries the main operating pressure of the program and handles dynamic requests in the website or application system;

pushes static pages to multiple publishing servers;

pushes attachment files to the file server;

security For websites with higher requirements and mainly static, the server can be placed on the internal network to block access from the external network.

DB Server: Database server

carries the pressure of database reading and writing;

only exchanges data volume with the main server and blocks external network access.

File/Video Server: File/Video Server

Carrying data streams that occupy large system resources and bandwidth resources in the system;

Served as a storage and read-write warehouse for large attachments;

As a video server, it will have automatic video processing ability.

Publishing server group:

is only responsible for publishing static pages and carries the vast majority of web requests;

performs load balancing deployment through Nginx.

Deployment plan 2:

Scope of application: websites or application systems with dynamic interactive content as the main body; websites or application systems with heavy load pressure and sufficient budget;

Web server group:

Web service without owner From the relationship, it is a parallel redundant design;

Achieve load balancing through front-end load balancing equipment or Nginx reverse proxy;

Divide dedicated file servers/video servers to effectively separate light/heavy buses;

Each web server can pass DEC It can connect all databases and divide master and slave at the same time.

Database server group:

bears relatively balanced database read and write pressure;

achieves data synchronization of multiple databases through the mapping of database physical files.

Shared disk/disk array

will be used for unified reading and writing of data physical files

Used for the storage warehouse of large attachments

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

Program features:

Reasonably distribute Web pressure through front-end load balancing;

Reasonably distribute light and heavy data flows through the separation of file/video servers and regular Web servers;

Reasonably distribute database IO pressure through database server groups;

Each Web server Usually only one database server is connected, and through DEC's heartbeat detection, it can automatically switch to a redundant database server in a very short time;

The introduction of disk arrays not only greatly improves the system IO efficiency, but also greatly enhances data security. .

Web server:

A large part of the resource usage of the web server comes from processing web requests. Under normal circumstances, this is the pressure generated by Apache. In the case of high concurrent connections, Nginx is a good alternative to the Apache server. Nginx ("engine x") is a high-performance HTTP and reverse proxy server written in Russia. In China, many websites and channels such as Sina, Sohu Pass, NetEase News, NetEase Blog, Kingsoft Xiaoyao.com, Kingsoft iPowerWord, Xiaonei.com, YUPOO Photo Album, Douban, Xunlei Kankan, etc. use Nginx servers.

Advantages of Nginx:

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

Low memory consumption: Under 30,000 concurrent connections, 10 Nginx processes started consume only 150M of memory (15M*10=150M).

Built-in health check function: If a web server in the backend of Nginx Proxy goes down, front-end access will not be affected.

Strategy: Compared with the old Apache, we choose Lighttpd and Nginx, web servers with smaller resource usage and higher load capacity.

Mysql:

MySQL itself has a strong load capacity. MySQL optimization is a very complicated task, because it ultimately requires a good understanding of system optimization. Everyone knows that database work involves a large number of short-term queries, reads and writes. In addition to software development techniques such as indexing and improving query efficiency that need to be paid attention to during program development, the main impact on MySQL execution efficiency from the perspective of hardware facilities comes from the disk. Search, disk IO levels, CPU cycles, memory bandwidth.

 Perform MySQl optimization based on the hardware and software conditions on the server. The core of MySQL optimization lies in the allocation of system resources, which does not mean allocating more resources to MySQL without limit. In the MySQL configuration file, we introduce some of the most noteworthy parameters:

Change the index buffer length (key_buffer)

Change the table length (read_buffer_size)

Set the maximum number of open tables (table_cache)

Right Slow long query sets a time limit (long_query_time)

If conditions permit, it is generally best to install the MySQL server in the Linux operating system rather than in FreeBSD.
Strategy: MySQL optimization requires formulating different optimization plans based on the database reading and writing characteristics of the business system and the server hardware configuration, and the master-slave structure of MySQL can be deployed as needed.

PHP:

1. Load as few modules as possible;

2. If it is under the windows platform, try to use IIS or Nginx to replace the Apache we usually use;

3. Install accelerator (all through Caching PHP code precompiled results and database results to improve the execution speed of PHP code)
eAccelerator, eAccelerator is a free and open source PHP accelerator that optimizes and dynamic content caching, improves the caching performance of PHP scripts, and makes PHP scripts compile faster In this state, the overhead on the server is almost completely eliminated.

Apc: Alternative PHP Cache (APC) is a free and public optimized code cache for PHP. It is used to provide a free, open and robust framework for caching and optimizing PHP intermediate code.

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

Xcache: a cache developed by Chinese people,

Strategy: Install an accelerator for PHP.

Proxy Server (Cache Server):

Squid Cache (Squid for short) is a popular free software (GNU General Public License) proxy server and web caching server. Squid has a wide range of uses, from acting as a front-end cache server for web servers to increase the speed of web servers by caching relevant requests, to caching the World Wide Web, Domain Name System, and other web searches for a group of people to share network resources, to helping the network by filtering traffic. Security, to LAN through proxy network. Squid is primarily designed to run on Unix-like systems.

Strategy: Installing 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: instead of running manual or automated tests under normal conditions, you run tests under conditions where the number of computers is small or system resources are scarce. Resources typically subject to stress testing include internal memory, CPU availability, disk space, and network bandwidth. Concurrency is generally used for stress testing.

Stress testing tools: webbench, ApacheBench, etc.

Vulnerability testing: Vulnerabilities in our system mainly include: sql injection vulnerabilities, xss cross-site scripting attacks, etc. Security also includes system software, such as operating system vulnerabilities, vulnerabilities in mysql, apache, etc., which can generally be solved through upgrades.

Vulnerability testing tool: Acunetix Web Vulnerability Scanner


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!