如何对简单的 PHP 应用程序进行负载平衡(扩展)?
我经常在 Internet 上了解到正确构建 PHP 应用程序以便它们可以扩展的重要性。
我构建了一个用 PHP 编写的简单/小型 CMS(类似于 Wordpress,但更简单)。
我基本上有这样的网址: http://example.com/?page_id=X 其中 < code>X 是我的 MySQL 数据库中包含页面内容的 id
。
我如何配置我的应用程序以实现负载平衡,而我只是执行 PHP 读取活动。
像 Nginx 这样的前门设置将流量路由到运行相同代码的多节点来处理 example.com/?page_id=X
是否足以“负载平衡” “我的网站?
显然,MySQL 在这种情况下没有进行负载平衡,尽管为了简单起见 - 这超出了这个问题的范围。
I constantly read on the Internet how it's important to correctly architect my PHP applications so that they can scale.
I have built a simple/small CMS that is written in PHP (think of Wordpress, but waaaay simpler).
I essentially have URLs like such: http://example.com/?page_id=X where X
is the id
in my MySQL database that has the page content.
How can I configure my application to be load balanced where I'm simply performing PHP read activities.
Would something like Nginx as the front door setup to route traffic to multi-nodes running my same code to handle example.com/?page_id=X
be enough to "load balance" my site?
Obviously, MySQL is not being load balanced in this situation, though for simplicity - that makes that out of scope for this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些是一些众所周知的扩展此类应用程序的技术。
减少数据库点击
大多数情况下,瓶颈将是您的数据库,因此缓存最近的页面,以便减少数据库活动,也许是在像 memcached 这样的东西中。
设计您的架构,使其可分区。
在最简单的情况下,将数据分成逻辑分区,并将每个分区存储在单独的 mysql 数据库中。例如,Craigslist 按城市划分数据,在某些情况下,按城市中的部分划分数据。在您的情况下,您可以非常简单地按 Id 进行分区。
管理 php 会话
如果使用会话,将 ngnx 放在 php 网站前面将不起作用。负载平衡 php 确实存在问题,因为会话保留在本地存储上。因此,您需要显式地进行会话管理。传统的解决方案是使用 memcached 来存储和查找某种 cookie。
不要过早优化。
专注于推出您的应用程序,以便下一阶段的当前用户获得最佳体验。
注意:此处讨论了您的主要潜在痛点
These are some well known techniques for scaling such an app.
Reduce DB hits
Most often the bottle neck will be your DB, so cache recent pages so that you reduce DB activity, perhaps in something like memcached.
Design your schema such that it is partition-able.
In the simplest case, separate your data into logical partitions, and store each partition in a separate mysql DB. Craigslist, for example, partitions data by city, and in some cases, by section within that. In your case, you could partition by Id quite simply.
Manage php sessions
Putting ngnx in front of a php website will not work if you use sessions. Load balancing php does have issues as sessions are persisted on local storage. Therefore you need to do session management explicitly. The traditional solution is to use memcached to store and look up some kind of cookie.
Don't optimize prematurely.
Focus on getting your application out so that the next magnitude of current users gets the optimal experience.
Note: Your main potential pain points are discussed here on SO
不,如果不需要,扩展应用程序一点也不重要。
我对此的看法是:
然后,如果您的流量太大以致于您的系统无法处理它,并且您已经把(合理的)金钱可以买到的所有硬件投入其中,然后你需要扩展。不早了。
是的,扩展读取工作负载相对容易,因为您可以简单地对只读数据库副本执行读取。挑战在于扩展写入工作负载。
许多网站的写入量很少,即使它们真的很忙。
No, it is not at all important to scale your application if you don't need to.
My view on this is:
Then, if you have to so much traffic that your system cannot handle it, AND you've already thrown all the hardware that (sensible) money can buy at it, then you need to scale. Not sooner.
Yes it is relatively easy to scale read-workloads, because you can simply perform reads against readonly database replicas. The challenge is to scale write-workloads.
A lot of sites have few writes, even if they're really busy.
正确的方法是使用某种负载均衡器,例如:
http://www.softwareprojects.com/resources/programming/t-how-to-install-and-configure-haproxy-as-an-http-loa -1752.html
它的作用是将某个用户会话仅转发到某个服务器,因此您根本不必担心会话及其存储位置。您需要担心的是,如果两台服务器运行在两台不同的计算机上,尤其是在您大量使用文件系统的情况下,如何分配文件系统。希望以上文章对您有所帮助...
The correct approach is to use some kind of load balancer such as:
http://www.softwareprojects.com/resources/programming/t-how-to-install-and-configure-haproxy-as-an-http-loa-1752.html
What this does is forward a certain user session only to a certain server, hence you dont have to worry about sessions and where they are stored at all. What you do have to worry is how to distribute the filesystem if the 2 servers are running on two different machines, especially if you make heavy use of the filesystem. Hope this article above helps...