PHP应用的水平扩展
英文原文:Horizontal Scaling of PHP Apps 结合原文和自己的理解,简单总结下,便于理解和日后查阅。 一个web网站,面对流量增长和其他性能瓶颈的挑战,我们应该如何去处理呢?两个方面- 优化和扩展 。 优化如升级php版本至最新版本、做好数据库索引、静态
英文原文:Horizontal Scaling of PHP Apps
结合原文和自己的理解,简单总结下,便于理解和日后查阅。
一个web网站,面对流量增长和其他性能瓶颈的挑战,我们应该如何去处理呢?两个方面-优化和扩展。
优化如升级php版本至最新版本、做好数据库索引、静态内容缓存等,比如一种常用的思路是利用Nginx做反向代理,具体可参考此处。扩展包括水平扩展和垂直扩展,垂直扩展简单说就是硬件的升级,以此获取更好的性能。水平扩展是用更多的服务器来做负载均衡,以此为网站提升更好的性能。负载均衡大的方面有两种实现,软件和硬件。软件可以用Nginx和Squid来实现,硬件可参考此列表。
水平扩展,多服务器实现负载均衡,其中面临一个重要的问题,这就是各服务器间的数据共享。比如PHP中的Session数据,如何实现不同服务器间共享Session数据呢?
一种办法是用Persistent load balancer,所谓Persistent load balancer是指负载均衡会记住某一用户的前一次请求,下次请求会路由到同一个服务器上,这样Session数据就不会丢失了。但问题是如果该台服务器挂了怎么办?结果就是用户的Session数据会丢失。另外这样做会增加load balancer的压力,因为除了路由,还要做额外的记忆工作,这样又会形成一个新的瓶颈。
另外一种办法就是共享本地数据。虽说此办法需要对应用做更改,但不会有性能瓶颈,而且容错性相对较好,一台服务器挂掉不会影响其它服务器。在PHP中,我们可以自定义方法去处理Session,具体可参考PHP Session。
我们可以用数据库来保存所有的Session数据,数据库放在一个单独的服务器上。这样做的一个缺点是会形成单点故障,数据库挂了则全挂了。当我们用传统的Mysql、Postgre或类似数据库时,所有服务器连接同一台数据库,同时进行读写操作,会造成负载过大,这样会形成新的瓶颈。
还可以用共享文件系统来实现共享本地数据。不过与数据库类似,同样会有单点故障,而且速度很慢,不推荐用此方法。不过其实现简单,只需要更改php.ini配置文件中的session.save_path即可。如果硬要尝试,推荐GlusterFS。
目前用的较多的实现办法应该是memcached。在内存中保存共享数据,php中可做如下配置:
<code>session.save_handler = memcache session.save_path = "tcp://path.to.memcached.server:port"</code>Copy after login
另外,正在发展中的Redis Cluster,也是一个不错的解决方案。还有ZSCM, Scache, Cassandra, Couchbase等实现方案,供参考。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
