How to scale PHP

php, scalability

Solution

Note: this wasn't written by me, but by Snorkel from YC news

Here's a short list:

- Cache the output at the edges: Use `Varnish` or other reverse proxy cache.

- Cache byte code: Use `APC` or `XCache` PHP opcode cache.

- Cache and minimize database I/O: reduce database touches using `memcached`, `redis`, file caches, and application-level caches (ie. global vars)

- Do event logging in local files, not to the database: Make all write operations as simple and fast as possible, any data that is not needed in realtime can be written to a plain old file and processed later.

- Use a CDN, especially for delivering static assets

- Server tuning: `Apache`, `MySQL`, and `Linux` have lots of settings that affect performance, especially the timeout settings ought to be turned down.

- Identify bottlenecks: At the system level use tools such `strace`, `top`, `iostat`, `vmstat`, and query logging to see which layer is using the most time and resources

- Load testing: DoS yourself. Stress test your stack to find bottlenecks and tune them out

- Remove unused modules: For each component in the stack unload any default modules that are not needed to deliver your service.

- Don't use ORMs and other dummy abstractions: Take off the training wheels and write your own queries.

- Make the entry pages fast, simple, and cacheable. Nobody is reading that silly news feed in bottom corner of your front page and it's killing your database, so take it out.

Most of the time a PHP slows down because each PHP process is blocked waiting for I/O from some other layer, either a slow disk, or overloaded database, or hung memcached process, or slow REST API call to a 3rd party service ... often just strace'ing a live PHP process will show you what its waiting for ... in short, blocking I/O slows down everything. The key to going faster is:

- keep it simple

- cache as much as possible in local memory

- do as few blocking I/O operations as possible per request

Problem

I'm creating a new web application in PHP and I'd like to create it in a way that scales well over time. What should or shouldn't I do? I know that I should cache, but what should I cache and how? What else can I do for the website to remain loading rapidly?

Original source

Related problems