Most webdevelopers are used to steady long term growth with occassional spikes, the kind of stuff you can just about get away with on shared hosting or a basic dedicated server. Facebook development can be a completely different kettle of fish, even relatively small apps can expect thousands of regular users hitting their app every day, and often growing very fast. You can see inexperienced facebook developers being caught with their pants down, how many apps out there are throwing up ‘not responding’ errors within a matter of days of being accepted into the application directory?
So you need to think ahead, particularly about your hosting requirements, you should plan an infrastucture where you can quickly plug in a few extra load balanced servers, and keep plugging more in as your app grows. This involves a set of skills beyond the scope of basic php/mysql app development and with a standard dedicated server setup, a significant increase in cost.
Amazons Webservices is one possible solution, in particular their “Elastic Compute Cloud” – EC2 service. EC2 essentially provides unlimited computing power through virtual servers – nodes, you can clone nodes and start up a new instance of a node within a couple of minutes and the beauty is you are billed by the hour not the month with no start up fees. Additionally you can easily update and save an image of your node so you can continue to tweak your virtual server, save it as an image and then use that image to create more nodes.
How To Set Up A Facebook Application On EC2
So I’ll give a rough outline to how you could host your application on EC2.
- Create An AWS account
- Go through the get started tutorial to get the idea of how it works.
This basically gets you familliar with the command line tools for setting up a node. Once a node is started you get an IP address and just log into it like you would any other unix server. - Plan your infrastructure.
- A typical set up would be Webservers serving the application, Database Master Server, Database Slave Servers.
To get you started, amazon has some good starting points, there is a nice apache webserver image and a separate mysql server image. - Additionally you’ll need to partition your application
- At the very least separate database reads and writes, so all reads are sent to the slaves and all writes to the master
- Consider how multiple webservers can distribute the load, they could all be clones or you could farm of different tasks to different servers
- Think about backup. Again the good thing about amazon EC2 is the fact you can save an snapshot of your node, this is especially nice if all your data is in a database as all you need to do is keep an up to date image of your webserver and do regular database backups, then you could in theory recover from anything in a few minutes (depending on database size). For live database backups you will need to be using replication and you can then safely perform backups of a slave. And you should back up the data to S3 (the snapshot images of nodes are pushed onto s3 automatically).
- A typical set up would be Webservers serving the application, Database Master Server, Database Slave Servers.
- Think about DNS, you’ll need a way of farming the requests to different servers and facebook (at time of writing) only gives one callback url. Fortunately this is fairly easy to get round with round robin DNS which is easy for transparently spreading requests accross your webservers. You will probably prefer to have your DNS separate from EC2 as IP addresses change if a node is shut down.
- Once you have thought through these points, you can now bear them in mind for planning/modifying your app, so next you should start up an EC2 instance (review the getting started tutorial) and start coding
Caveats
- This biggest caveat is ‘no persistant storage’ – if you terminate an instance all data is lost forever. However in the context of such a specific environment as a facebook app, this should not be a big problem, you just need to make sure you have planned a regular offsite data backup and take regular snapshots of your nodes.
- IP Address change, you IP address will change if you terminate an instance so you’ll need to have a low TTL on your DNS servers so IP addresses will never be cached for too long
leonard says
Interesting article, i was thinking about hosting my facebook app on the s3 and utilizing ec2 however thought that the process would be too complicated.
Is your app hosted was s3 using ec2 and was it more bother than it was worth?
Mr Kirkland says
I hosted the tracker application before it was deleted and that worked really well as I was able to quickly repsond to increase in users. If you are dealing with a popular or intesive app, then EC2 could well be worth it. You do have to think a little bit more about the infrastructure than on traditional hardware, but I think this is ultimately a good thing as it forces you plan well for failure and redundancy.