In the past, running a large web application meant running 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. This is a more feasible way to minimize hardware costs.
The 'more servers' mode has more advantages than the 'large server' mode in the past. it is 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.
So, seize this opportunity :). of course, the price is that it requires a little more complexity for your application development. This is the content to be covered in this article.
Then you may say to yourself, 'but how do I know I'm 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.
Before proceeding to the following content, I would like to point out that this article mainly describes PHP server load balancer. In the future, I may write text about data load balancing, but now you have to wait.
Note: I have always mentioned "web applications" instead of website. this is to differentiate 'web applications' from complex websites that often involve server-side programming and databases, instead of displaying simple static content like website.
1. php file
The first question is, 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:
1. upload all files to each server separately. this method causes the following problems: 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.
2. use 'rsync' (or similar software). tools like this can synchronize files in local directories and multiple remote host directories.
3. 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.
4. 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. file Upload
When there is only one server, 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:
1. 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.
2. stores 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.
3. design your own Upload mechanism to transfer files to each 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, it stores session data in temporary files 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.
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.