Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalability 101: How can I design a scalable web application using PHP?

I am building a web-application and have a couple of quick questions. From what I learnt, one should not worry about scalability when initially building the app and should only start worrying when the traffic increases. However, this being my first web-application, I am not quite sure if I should take an approach where I design things in an ad-hoc manner and later "fix" them. I have been reading stories about how people start off with an app that gets millions of users in a week or two. Not that I will face the same situation but I can't help but wonder, how do these people do it?

Currently, I bought a shared hosting account on Lunarpages and that got me started in building and testing the application. However, I am interested in learning how to build the same application in a scalable-manner using the cloud, for instance, Amazon's EC2. From my understanding, I can see a couple of components:

  • There is a load balancer that first receives requests and then decides where to route each request
  • This request is then handled by a server replica that then processes the request and updates (if required) the database and sends back the response to the client
  • If a similar request comes in, then a caching mechanism like memcached kicks into picture and returns objects from the cache
  • A blackbox that handles database replication

Specifically, I am trying to do the following:

  • Setting up a load balancer (my homework revealed that HAProxy is one such load balancer)
  • Setting up replication so that databases can be synchronized
  • Using memcached
  • Configuring Apache to work with multiple web servers
  • Partitioning application to use Amazon EC2 and Amazon S3 (my application is something that will need great deal of storage)
  • Finally, how can I avoid burning myself when using Amazon services? Because this is just a learning phase, I can probably do with 2-3 servers with a simple load balancer and replication but until I want to avoid paying loads of money accidentally.

I am able to find resources on individual topics but am unable to find something that starts off from the big picture. Can someone please help me get started?

like image 283
Legend Avatar asked Feb 20 '11 07:02

Legend


People also ask

How do you scale a PHP application?

Vertical Scaling By far the easiest way to scale up your application is to buy a better server. Adding more CPUs and RAM will speed up your website, but it comes at a cost as buying a high-end server is expensive.

What is scalable PHP architecture?

High Scalability Architecture is the base of modern PHP applications. The increasing demand on web and mobile applications has made necessary for development and operation teams to implement new approaches to ensure the best performance and therefore attend user needs 24/7.


1 Answers

Personally, I think you should be considering how your app will scale initially - as otherwise you'll run into problems down the line.

I'm not saying you need to build it initially as a multi-server system, but if you think you'll need to do it later, be mindful of the concerns now.

In my experience, this includes things like:

  • Sessions. Unless you use 'sticky' load balancing, you will have to have some way of sharing session state between servers. This probably means storing session data on either shared storage, or in a DB.

  • File uploads and replication. If you allow users to upload files, or you have a CMS that allows you to upload images/documents, it needs to cater for the fact that these files will also need to find their way onto other nodes in your cluster. However, if you've gone down the shared storage route mentioned above, this should cover it.

  • DB scalability. If you're using traditional DB servers, you might want to think about how you'll implement scalability at that level. This may mean coding your app so you use one connection string for reads, and another for writes. Then, you are free to implement replication with one master node handling the inserts/updates cascading the changes to read only nodes that handle the bulk of the work.

  • Middleware. You might even want to go down the route of implementing some kind of message oriented middleware solution to completely hand off business logic functions - this will give you a great level of flexibility in how you wish to scale this business logic layer in the future. Although initially this will be a lot of complication and work for not a great deal of payoff.

like image 156
rvxnet Avatar answered Sep 21 '22 05:09

rvxnet