views:

31

answers:

2

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 "l*oad 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.

+2  A: 

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

tholomew
A: 

No, it is not at all important to scale your application if you don't need to.

My view on this is:

  1. Make it work
  2. Make sure it works correctly - testability, robustness
  3. Make it work efficiently enough to be cost effective to run

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.

MarkR