Home > PHP Framework > Laravel > Understanding load balancing, distribution, and clustering, and how to synchronize multiple server codes

Understanding load balancing, distribution, and clustering, and how to synchronize multiple server codes

步履不停
Release: 2019-07-03 14:42:18
Original
4459 people have browsed it

Understanding load balancing, distribution, and clustering, and how to synchronize multiple server codes

The following will explain the concepts of these terms

Cluster

If our project runs on a machine, if this machine fails If so, or the volume of user requests is relatively high and one machine cannot support it. Our website may not be accessible. So how to solve it? We need to use multiple machines and deploy the same program so that several machines can run our website at the same time. So how do we distribute requests to all our machines. So the concept of load balancing emerged.

Load Balancing

Load balancing refers to the ability to distribute all current requests to different servers based on a reverse proxy based on a specified policy algorithm. Commonly used to achieve load balancing can be nginx, lvs. But now there is also a problem, what should I do if there is a problem with the load balancing server? All the concepts of redundancy emerge.

Redundancy

Redundancy is actually two or more servers, a master server and a slave server. Suppose there is a problem with the load balancing server of a master server, and the slave server can replace the master server to continue load balancing. The way to achieve this is to use keepalive to seize the virtual host.

Distributed

Distributed is actually splitting a large project into separate parts and running it independently.

Take the above example. Let's say our traffic is particularly high. We can make it distributed, with the same mechanism as CDN. An identical cluster is built in three places: Beijing, Hangzhou, and Shenzhen. Users who are close to Beijing will access the cluster in Beijing, and users who are close to Shenzhen will access the cluster in Shenzhen. This splits our online battle into three areas, each of which is independent.

Another example is our redis distribution. Redis distribution distributes the data in redis to different servers. Each server stores different content, while mysql cluster stores the same data on each server. This also understands the concepts of distribution and clustering.

mysql master-slave

mysql The master server will write the sql operation log into the bin.log log. The slave server will read the master's bin.log log and then execute the sql statement.

The master and slave have the following problems.

1. The master server can write and read, but the slave can only write.

The data read by slave has not been written yet. How to solve this problem?

1. If cached, read from cache.
2. Force reading from master.
3. Using pxc cluster, any node is readable and writable, with strong consistency in reading and writing.

How laravel solves data inconsistencies

Set sticky to true in the config/database.php mysql configuration block

sticky is an optional value, which can be used for immediate reading Get the records that have been written to the database during the current request cycle. If the sticky option is enabled and a "write" operation was performed during the current request cycle, any "read" operations will use the "write" connection. This ensures that data written in the same request cycle can be read immediately, thereby avoiding the problem of data inconsistency caused by master-slave delay. Whether to enable it, however, depends on the application's needs.

How to synchronize our code to multiple servers?

Laravel provides us with the extension package laravel/envoy, which provides a set of concise and lightweight syntax for defining daily tasks of remote servers. Blade style syntax can be used to configure deployment tasks, execute Artisan commands, etc.

composer global require laravel/envoy
Copy after login

Envoy tasks should all be defined in Envoy.blade.php in the project root directory. Write the content

@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])

@task('deploy', ['on' => ['web-1', 'web-2']])
    cd site
    git pull origin {{ $branch }}
    composer update
    php artisan migrate
@endtask
Copy after login

The above code means that when envoy run deploy is executed on the command line, we will ssh to web-1 and web-2 for execution

    cd site
    git pull origin {{ $branch }}
    php artisan migrate
Copy after login

Of course, this premise is that we have joined ssh to the remote server.

For more Laravel related technical articles, please visit the Laravel Tutorial column to learn!

The above is the detailed content of Understanding load balancing, distribution, and clustering, and how to synchronize multiple server codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template