Scalability

Rajat Agarwalla (see his bio in Contributors)

Problem

How do I scale my application so that it is not a victim of its own success?

Solution

Speed is critical. If your application does not load within milliseconds, it will tend to drive away users. Given the viral nature of Facebook, such a scenario can occur within weeks or even days of launching your application. Scalability needs to be ingrained within all aspects of the application: source code, database, hardware/operating system platform, user interface, and support systems. As there is no fixed formula for scalability, you need to monitor these aspects closely and tune the ones that you think will cause a bottleneck. Build your application to sustain at least three times more load than you initially expect.

Discussion

When we launched Scrabulous on Facebook in June 2007, we did not expect more than a few thousand users. Accordingly, Scrabulous was launched on a shared-hosting service and utilized a common database server with possibly 50 other websites. Two weeks into our launch and with less than 10,000 users on Scrabulous, we were introduced to the word “scalability.” Our hosting service politely asked us to remove the application from their servers as it was causing a large amount of load! Even today, after having rewritten the entire source code four times and moving onto private dedicated servers, we still need to consider the scalability factors before launching a new feature on Scrabulous. For your application, you may want to consider looking into the following areas.

Monitoring software

This is the number one requirement. A number of free utilities can help you analyze your hardware resources (Cacti, MRTG, etc.). Use them as efficiently as possible. They are a tremendous help in letting you decide whether to add hardware or focus on the source code.

Application source code

The source code of your application may end up being a resource hog. In a hurry to launch applications, developers sometimes overlook the benefits of object-oriented programming (OOP). Even if you are not much into OOP, try to write your code in functions that can be loaded as required. For example, if you are writing your application in PHP, you can easily split functions into files and load the appropriate files based on the user’s action. This will ensure minimal memory usage on page loads.

It is highly recommended that a versioning system, such as CVS or SVN, is utilized. Coding is more efficient and you can keep a history of changes, which helps when trying to tune the code for scalability.

Database

If your budget allows, start off with your database on a separate server within the same network as your primary server. This will allow you to tune the OS specifically for the database, removing all other applications and making more memory and CPU resources available to the database. Having a separate server will also let you cluster/replicate without affecting other aspects of the system.

Hardware/OS/software utilities

With the right hardware and OS platform, you can easily scale up. For example, if you are on Linux and using Apache as your HTTPD, it is very easy to convert your box into a load-balancing unit!

Simple utility software, such as memcached and various PHP accelerators, go a long way in easing out hardware resources. When we implemented memcached for Scrabulous, it reduced our database loads by 40%. You may also want to consider using Java or C++ servers for backend tasks (such as updating Facebook Profiles). Accordingly, you can schedule the backend tasks to run during off-peak hours.

User interface/support systems

Build your user interface so that it can grow with your application. Drastic interface changes drive users away. Try to imagine the largest possible feature set for your application and then see whether your interface can easily scale up to that level or not. This is an issue we faced with Scrabulous. There were too many useful features and too little space on the user interface to make them easily accessible.

Utilize a support system from day one so that all your user requests are handled in an efficient manner. This helps in bug solving and allows for easy handover to a support team when your application becomes successful.

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.