减少 PHP IO 负载
我有一个 PHP 脚本,可以处理许多大小高达 10mb 的较小文件 (>100,000)。它基本上将请求的文件加载到内存中并将其提供给客户端。由于访问控制,我无法直接通过 apache 提供这些文件,并且需要一个包含它的脚本。
如果流量很高(>150mbit),我的硬盘就会被大量使用,这代表了扩展的限制。我的想法是我可以使用 memcached 来减少硬盘负载,因为我有 10gig 的可用内存,但 memcached 的最大项目大小为 1MB。然后我想我可以使用 PHP-APC,但是如果缓存耗尽内存(完全重置),它的行为是不可接受的。
您会采取什么措施来减少 IO 负载?
谢谢
I have a PHP script that serves alot of smaller files (>100,000) with sizes up to 10mb. It basically loads the requested file into memory and serves it to the client. Because of access control I cannot serve these files by apache directly and need a script wrapped around it.
If there is high traffic (>150mbit) my hdd is heavily used and represents a limit for scaling. I had the idea that I could use memcached to reduce the hdd load since I have 10gig of ram available but memcached has a max item size of 1MB. Then I thought I could use PHP-APC but its behaviour if the cache runs out of memory (complete reset) isn't acceptable.
What would you do to reduce the IO load?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我自己从未使用过它,但是
X-Sendfile
方法可能有助于减轻一些负载。它将实际提供文件的任务传递回 Apache。
I have never worked with it myself, but the
X-Sendfile
method may be helpful for taking away a bit of the load. It passes the task of actually serving the file back to Apache.我认为你不能这样做,除非你有 2 个硬盘来分割这些文件。
I think you can't do this unless you have 2 HDD which would split these files.
我会使用 PHP-APC 将这些文件加载到缓存中。
apc_add()
、apc_fetch()
和apc_delete()
就是您想要的。您可以使用apc_cache_info()
确定可用内存级别,以确保不会溢出。您还可以设置apc.user_ttl
INI 设置以防止填充时完全清除缓存。在测试服务器上进行设置,使其承受高负载(使用
ab
等)并使用apc.php
检查统计信息。调整、调整、再调整!I would use PHP-APC to load these files into the cache.
apc_add()
,apc_fetch()
andapc_delete()
are what you want. You can ensure you don't overflow by usingapc_cache_info()
to determine free memory levels. You can also setapc.user_ttl
INI setting to prevent total cache clearing on fill.Set things up on a test server, subject it to high load (with
ab
or the like) and check your stats usingapc.php
. Tweak, tweak and tweak some more!您可以使用支持访问控制的 CDN。
不过,如果您想继续自己提供服务,可以采取多种方法。不过,您总是希望避免通过 PHP 提供文件,因为这是您的瓶颈。但这些都不是很优雅。
.htacess
文件中的重写规则来实现相同的效果。通过将最常访问的文件存储在 ramdisk 中来减少 IO 负载,通过一些安装魔法或符号链接将它们与常规文件系统集成,然后让 Apache 处理其余的事情。
You could use a CDN that supports access control.
If you want to continue serving it yourself, though, there are various approaches you could take. You'll always want to avoid serving the file through PHP though, because that is your bottleneck. None of these are very elegant though.
.htacess
file instead to achieve the same thing.Reduce IO load by storing the most frequently accessed files in a ramdisk, integrate them with the regular file system by some mounting magic or symlinks and then let Apache handle the rest.
您需要 mod_xsendfile for Apache2 或 带有 X-Accel-Redirect 的 nginx。 lighttpd 也有类似的解决方案。 Nginx 还可以从 memcached 提供服务。
如果您正在考虑将常用文件存储在 tmpfs 中,请不要这样做。这不是一个真正的解决方案,因为即使您直接从磁盘提供文件,后续请求也会访问系统缓存,并且您将获得与 tmpfs 类似的速度。
You need either mod_xsendfile for Apache2 or nginx with X-Accel-Redirect. There is also similar a solution for lighttpd. Nginx can also serve from memcached.
If you're thinking about storing frequently used files in tmpfs, don't. That's not a real solution because even if you serve files right from the disk subsequent requests will hit the system cache and you'll get similar to tmpfs speed.