PHP server load balancer _ PHP Tutorial

Source: Internet
Author: User
Tags php session php load balancing svn update version control system
PHP load balancing. In the past, when running a large web application, it was necessary to run a large web server. Because your application attracts a large number of users, you will have to add it to your server. when you run a large web application, it means you need to run a large web server. Because your application attracts a large number of users, you will have to add more memory and processors to your server. Today, the "large server" model has passed, replacing it with a large number of small servers, using a variety of load balancing technologies.

The advantages of "more small servers" over the "large servers" model in the past are reflected in two aspects:
 
1. if the server goes down, the server load balancer system will stop sending requests to the Down Server and distribute the load to other normal servers.

2. it is easier to expand your server. All you have to do is add a new server to the server load balancer system. You do not need to interrupt the running of your application. Therefore, seize this opportunity. Of course, the cost is that this requires a little more complexity during application development. This is the content to be covered in this article.

Then you may say to yourself, "but how do I know I am using server load balancer ?". The most honest answer is that if you are asking this question, most of you are not using the server load balancer system and your system does not need to consider this question. In most cases, server load balancer needs to be explicitly proposed and set up when the application grows to a large enough scale. However, I also occasionally see virtual host companies doing load balancing for their applications, or as described below. Note: I have always mentioned "web applications" instead of "website". this is to distinguish "web applications" from complicated websites that often involve server-side programming and databases, instead of displaying simple static content like website.
1. The first problem with PHP files is that if you have a large number of small servers, how do you upload your php files to all servers? There are the following methods for your reference:
◆ Upload all files to each server separately. this method brings about the problem: Imagine that you have 20 servers, which may easily lead to errors during the upload process, during the update, files of different versions may exist on different servers.
◆ Use 'rsync' (or similar software). This tool can synchronize files in the local directory and multiple remote host directories.
◆ Use version control software (such as subversion). This is my favorite method. It can well maintain my code. when I publish my application, I can run svn update command synchronization on every server. This method also makes it easier to switch the server code to a previous version.
◆ Use a file server (you may find NFS is very suitable for doing this ). this method uses a file server to store your web applications. of course, if your file server goes down, so many of your sites will not be available. In this case, you need to spend more money to recover it. The method you choose depends on your needs and skills. If you use a version control system, you may have to plan a method if you execute an update command to update the code on all servers at the same time. However, if you use a file server, you need to implement some failure recovery mechanisms to prevent requests from failing in case of server downtime.
2. when there is only one server for file upload, file upload is not a problem. But when we have multiple servers, how should we store the uploaded files? The problem of file uploading is similar to that of cross-server PHP file storage. The following are several possible solutions:
◆ Store files in the database. Most data can be stored in binary data. When you request a file to be downloaded, the access data outputs the binary data and the corresponding file name and type to the user. Before using this solution, you should consider how the database stores your files. The problem with this method is that if the database server goes down, the file will be unavailable.
◆ Store uploaded files on a file server. as described above, you need to install a file server to share all web servers, upload all uploaded files here, and all uploaded web servers can use it. However, if the file server goes down, the image file download may be interrupted.
◆ Design your own Upload mechanism to transfer files to every server. this method does not have any defect in a single file server or database solution, but will increase the complexity of your code. For example, if the server goes down while uploading data to multiple servers, what should you do? It is a good solution to store and upload files in a database but design a File Cache mechanism. When the server receives a file download request, it first checks whether the file exists in the cache system. If yes, it downloads the file from the cache system. Otherwise, it reads the file from the database and caches it to the file system.
3. session (Sessions) if you are familiar with php session processing, you may know that by default, session data is stored in a temporary file on the server. In addition, this file is only on the server where your request is processed, but the next request may be processed by another server, which will generate a new session on the other server. As a result, the session is frequently not identified. for example, a logon user always needs to log on again.

The recommended solution is to either re-store session data to the database through the php built-in session processing mechanism, or implement your own mechanism to ensure that one user's request is sent to the same server.
4. Configuration)
Although this topic is not particularly related to php, I feel it is necessary to mention it. When running a cluster server, it is a good idea to use some method to synchronize configuration files between servers. If the configuration files are inconsistent, it may lead to some very strange intermittent behavior, which makes it difficult to troubleshoot these problems.

I recommend that you use the version control system to manage them separately. In this way, you can install and store different php configuration files for different projects, or synchronize all server configuration files.
5. Logging)
Like configuration problems, logging is not only related to php. But it is still very important to keep the server healthy. Without the correct logging system, how do you know that if PHP code starts to generate errors (you always disable display_errors settings when the system is running, aren't you ?)

You can implement logging in several ways:
1. log on each server.

This is the simplest method. Each machine records only one file. The advantage is that it is simple and may only require few configurations. However, as the number of servers increases, it is very difficult to monitor log files on each server.
2. record logs to a shared method. every server still has this log file, but they are stored on a central file server through the sharing mechanism, which makes monitoring logs easier. The problem with this solution is that if the file server is unavailable, a simple log cannot be written and the entire application crashes.
3. log recording to the logging server you can use a logging software, such as syslog, to write all the logs to a central server. Although this method requires more configurations, it also provides the most robust solution.

Bytes. Because your application attracts a large number of users, you will have to add more in your server...

Related Article

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.