在你回答这个问题之前,我从来没有开发过任何流行到足以达到高服务器负载的东西。请把我当作(唉)一个刚刚登陆地球的外星人,尽管我知道PHP和一些优化技术。


我正在开发一个PHP工具,可以获得相当多的用户,如果它是正确的。然而,虽然我完全有能力开发程序,但当涉及到制作可以处理巨大流量的东西时,我几乎一无所知。所以这里有一些关于它的问题(也可以把这个问题变成一个资源线程)。

数据库

At the moment I plan to use the MySQLi features in PHP5. However how should I setup the databases in relation to users and content? Do I actually need multiple databases? At the moment everything's jumbled into one database - although I've been considering spreading user data to one, actual content to another and finally core site content (template masters etc.) to another. My reasoning behind this is that sending queries to different databases will ease up the load on them as one database = 3 load sources. Also would this still be effective if they were all on the same server?

缓存

我有一个用于构建页面和交换变量的模板系统。主模板存储在数据库中,每当一个模板被调用时,它的缓存副本(html文档)就会被调用。目前,我在这些模板中有两种类型的变量-静态变量和动态变量。静态变量通常是像页面名称,网站的名称-不经常改变的东西;动态变量是在每次页面加载时改变的东西。

我的问题是:

比如说我对不同的文章有评论。这是一个更好的解决方案:存储简单的注释模板,并在每次页面加载时呈现注释(来自DB调用),或者将注释页面的缓存副本存储为html页面——每次添加/编辑/删除注释时,页面都会被重新检索。

最后

有人有任何提示/指针运行一个高负载的PHP网站。我很确定这是一种可行的语言——Facebook和Yahoo!优先考虑——但有什么经验是我应该注意的吗?


当前回答

我在一些网站上工作过,这些网站都是由PHP和MySQL支持的,每个月都有数百万的点击率。以下是一些基本知识:

Cache, cache, cache. Caching is one of the simplest and most effective ways to reduce load on your webserver and database. Cache page content, queries, expensive computation, anything that is I/O bound. Memcache is dead simple and effective. Use multiple servers once you are maxed out. You can have multiple web servers and multiple database servers (with replication). Reduce overall # of request to your webservers. This entails caching JS, CSS and images using expires headers. You can also move your static content to a CDN, which will speed up your user's experience. Measure & benchmark. Run Nagios on your production machines and load test on your dev/qa server. You need to know when your server will catch on fire so you can prevent it.

我推荐阅读《构建可扩展的网站》,它是由Flickr的一位工程师写的,是一个很好的参考。

看看我关于可伸缩性的博客文章,它有很多关于多种语言和平台可伸缩性的演示文稿的链接: http://www.ryandoherty.net/2008/07/13/unicorns-and-scalability/

其他回答

一般

在开始看到真实世界的负载之前,不要尝试优化。你可能猜对了,但如果你猜错了,那你就是在浪费时间。 使用jmeter、xdebug或其他工具对站点进行基准测试。 如果加载开始成为一个问题,对象或数据缓存都可能涉及到,所以通常阅读缓存选项(memcached, MySQL缓存选项)

Code

对代码进行分析,以便了解瓶颈在哪里,以及它是在代码中还是在数据库中

数据库

Use MYSQLi if portability to other databases is not vital, PDO otherwise If benchmarks reveal the database is the issue, check the queries before you start caching. Use EXPLAIN to see where your queries are slowing down. After the queries are optimized and the database is cached in some way, you may want to use multiple databases. Either replicating to multiple servers or sharding (splitting the data over multiple databases/servers) may be appropriate, depending on the data, the queries, and the kind of read/write behavior.

缓存

Plenty of writing has been done on caching code, objects, and data. Look up articles on APC, Zend Optimizer, memcached, QuickCache, JPCache. Do some of this before you really need to, and you'll be less concerned about starting off unoptimized. APC and Zend Optimizer are opcode caches, they speed up PHP code by avoiding reparsing and recompilation of code. Generally simple to install, worth doing early. Memcached is a generic cache, that you can use to cache queries, PHP functions or objects, or entire pages. Code must be specifically written to use it, which can be an involved process if there are no central points to handle creation, update and deletion of cached objects. QuickCache and JPCache are file caches, otherwise similar to Memcached. The basic concept is simple, but also requires code and is easier with central points of creation, update and deletion.

杂项

考虑高负载的替代web服务器。像lighthttp和nginx这样的服务器可以用比Apache少得多的内存处理大量流量,如果你可以牺牲Apache的强大功能和灵活性(或者如果你不需要这些东西,通常情况下,你不需要)。 请记住,现在的硬件非常便宜,所以一定要花费精力来优化一大块代码,而不是“让我们购买一个巨型服务器”。 考虑将“MySQL”和“scaling”标签添加到这个问题中

我不认为自己会很快从MySQL转换过来——所以我想我不需要PDO的抽象功能。DavidM,谢谢你的文章,它们帮了我很多。

已经给出了很多很好的答案,但我想向您介绍另一种称为XCache的操作码缓存。它是由一个轻量级贡献者创建的。

此外,如果你将来可能需要负载平衡你的数据库服务器,MySQL代理可以很好地帮助你实现这一点。

这两种工具都可以很容易地插入到现有的应用程序中,因此可以在需要时进行优化,而不需要太多麻烦。

看来我错了。MySQLi仍在开发中。但是根据这篇文章,PDO_MySQL现在由MySQL团队贡献。摘自文章:

The MySQL Improved Extension - mysqli - is the flagship. It supports all features of the MySQL Server including Charsets, Prepared Statements and Stored Procedures. The driver offers a hybrid API: you can use a procedural or object-oriented programming style based on your preference. mysqli comes with PHP 5 and up. Note that the End of life for PHP 4 is 2008-08-08. The PHP Data Objects (PDO) are a database access abstraction layer. PDO allows you to use the same API calls for various databases. PDO does not offer any degree of SQL abstraction. PDO_MYSQL is a MySQL driver for PDO. PDO_MYSQL comes with PHP 5. As of PHP 5.3 MySQL developers actively contribute to it. The PDO benefit of a unified API comes at the price that MySQL specific features, for example multiple statements, are not fully supported through the unified API. Please stop using the first MySQL driver for PHP ever published: ext/mysql. Since the introduction of the MySQL Improved Extension - mysqli - in 2004 with PHP 5 there is no reason to still use the oldest driver around. ext/mysql does not support Charsets, Prepared Statements and Stored Procedures. It is limited to the feature set of MySQL 4.0. Note that the Extended Support for MySQL 4.0 ends at 2008-12-31. Don't limit yourself to the feature set of such old software! Upgrade to mysqli, see also Converting_to_MySQLi. mysql is in maintenance only mode from our point of view.

对我来说,这篇文章似乎偏向MySQLi。我想我偏向于PDO。 我真的很喜欢PDO胜过MySQLi。这对我来说很简单。这个API更接近于我编写的其他语言。OO数据库接口似乎工作得更好。

我还没有遇到过任何PDO无法提供的MySQL特性。如果有的话,我才会惊讶呢。

我是一个拥有超过1500万用户的网站的首席开发人员。我们很少遇到规模问题,因为我们很早就计划好了,并且经过深思熟虑。以下是我根据自己的经验提出的一些策略。

模式 首先,去规范化您的模式。这意味着您不应该使用多个关系表,而应该选择使用一个大表。通常,连接会浪费宝贵的DB资源,因为多次准备和排序会消耗磁盘I/O。尽量避免使用。

这里的权衡是您将存储/提取冗余数据,但这是可以接受的,因为数据和笼内带宽非常便宜(更大的磁盘),而多个准备I/O则要昂贵几个数量级(更多的服务器)。

索引 确保您的查询使用了至少一个索引。但是要注意的是,如果频繁地编写或更新索引将会使您付出代价。有一些实验性的技巧可以避免这种情况。

您可以尝试添加其他未索引的列,这些列与已索引的列并行运行。然后,您可以有一个脱机进程,批量地在已索引的列上写入未索引的列。这样,你可以更好地控制mySQL何时需要重新计算索引。

像避免瘟疫一样避免计算查询。如果必须计算查询,请尝试在写入时执行一次。

缓存 我强烈推荐Memcached。它已经被PHP堆栈上最大的玩家(Facebook)证明了,而且非常灵活。有两种方法可以做到这一点,一种是在数据库层缓存,另一种是在业务逻辑层缓存。

DB层选项需要缓存从DB检索的查询结果。您可以使用md5()散列SQL查询,并在进入数据库之前将其用作查找键。这样做的好处是它很容易实现。缺点(取决于实现)是您失去了灵活性,因为您在缓存过期方面对所有缓存都一视同仁。

在我工作的车间中,我们使用业务层缓存,这意味着系统中的每个具体类都控制自己的缓存模式和缓存超时。这对我们来说工作得很好,但是要注意从DB中检索到的项可能与从缓存中检索到的项不一样,所以你必须同时更新缓存和DB。

数据分片 复制只能让你到此为止。很快,写操作就会成为瓶颈。为了弥补这一点,请确保尽早支持数据分片。如果你不这样做,以后你可能会想开枪自杀。

它的实现非常简单。基本上,您希望将密钥权限与数据存储分离。使用全局DB存储主键和集群id之间的映射。您可以查询此映射以获得一个集群,然后查询集群以获得数据。您可以缓存这个查找操作,这将使它成为一个可以忽略不计的操作。

这样做的缺点是可能很难从多个碎片中拼凑出数据。但是,你也可以设计自己的方法。

离线处理 不要让用户等待你的后端,如果他们没有必要的话。构建一个作业队列,并将任何处理移至脱机状态,将其与用户的请求分开。