Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale PHP [closed]

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?

like image 334
Jeroen Avatar asked Jun 05 '12 19:06

Jeroen


People also ask

What is scalability in PHP?

So what actually do you mean by scalability? It's the ability of a website or web application to grow with the expanding business needs of the firm. PHP is quite scalable and works well with various frameworks like Symfony, Codeigniter, Zend, CakePHP and many more.

What is scalable PHP architecture?

It is the capability of a system either for automatically raise up or lower down the use of its resources (CPU, memory, etc.) depending on the level of demand required from it.


2 Answers

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

Here's a short list:

  1. Cache the output at the edges: Use Varnish or other reverse proxy cache.
  2. Cache byte code: Use APC or XCache PHP opcode cache.
  3. Cache and minimize database I/O: reduce database touches using memcached, redis, file caches, and application-level caches (ie. global vars)
  4. 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.
  5. Use a CDN, especially for delivering static assets
  6. Server tuning: Apache, MySQL, and Linux have lots of settings that affect performance, especially the timeout settings ought to be turned down.
  7. 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
  8. Load testing: DoS yourself. Stress test your stack to find bottlenecks and tune them out
  9. Remove unused modules: For each component in the stack unload any default modules that are not needed to deliver your service.
  10. Don't use ORMs and other dummy abstractions: Take off the training wheels and write your own queries.
  11. 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

like image 189
Jeroen Avatar answered Oct 24 '22 05:10

Jeroen


Scaling to several front-ends require also to replicate the root of your website and its dynamic content.

GlusterFS is a nice add, however it is unclear to me how well it scales up. Another alternative is Lsyncd. Other alternatives may include GIT for the code and a CDN for the dynamic content.

like image 27
Giuseppe Miriello Avatar answered Oct 24 '22 05:10

Giuseppe Miriello