scaling facebook apps - karl bunyan november 28th

33
Scaling an application Facebook D eveloper’s G arage November 28 th 2007

Upload: antb2000

Post on 08-Jul-2015

523 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: SCALING FACEBOOK APPS - Karl Bunyan November 28th

S c a lin g a n a p p lic a t io n

F a c e b o o k D e v e lo p e r ’s G a r a g eN o v e m b e r 2 8 t h 2 0 0 7

Page 2: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Scaling an application

What I’m covering

• Facebook specific issues

• A couple of problems solved• A couple of hacks

• Other issues around scaling

Page 3: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Scaling an application

What I’m not covering:

• Writing good code

• Hosting and hardware• Server optimising

Page 4: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Scaling an application

About Exponetic

• Web application developers

• Based in London• 12 members of staff

• Founded in 2002

• 9 Facebook applications live

• 5 currently in development

Page 5: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

Two approaches

• A useful app that’s tricky to scale

• A trivial app that uses no resources

Page 6: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

Two approaches

• A trivial app that uses no resources– Which Vegetable Are You?

– 155k users

• A useful app that’s tricky to scale– Six Degrees of Separation– 205k users

Page 7: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

Six degrees of separation

• Started as an R&D project: 1.5 days work

• Designed to test the end-to-end process of releasing an application

• We didn’t expect it to go anywhere

• Built in PHP on Windows, with SQL Server (to make use of views and triggers)

Page 8: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

Six degrees of separation

Original functionality consisted of:

• Calculate how many degrees of separation from the user to me

• Give them some other numbers

Page 9: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

Page 10: SCALING FACEBOOK APPS - Karl Bunyan November 28th

What kind of scale?

Active users:

• Small: < 200

• Smallish: < 1,000• Bigish: < 5,000

• Big: < 25,000 We are here

• Huge: …

Page 11: SCALING FACEBOOK APPS - Karl Bunyan November 28th

What kind of scale?

• Currently 205k users

• 87 users after 2 days

• 2000 users after 1 week• 9700 users after 2 weeks

• 128k users after 2 months

Page 12: SCALING FACEBOOK APPS - Karl Bunyan November 28th

What kind of scale?

0

20000

40000

60000

80000

100000

120000

140000

160000

29/08

/200

7

05/09

/200

7

12/09

/200

7

19/09

/200

7

26/09

/200

7

03/10

/200

7

10/10

/200

7

17/10

/200

7

24/10

/200

7

Installs over time

Page 13: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to get scale

Two different approaches to application design...

Page 14: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to get scale

Six Degrees:

• Nothing spammy

• No feeds or notifications used• The sign-up process is:

– User sign-up– Shown results– User is encouraged to invite friends “in order

to connect better”

Page 15: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to get scale

Which Vegetable:

• Give the user a reward for sending invites:– “Invite your friends to find out which

vegetables they are”

• App promoted on “Six Degrees” for 2 days

• Viral spread thereafter

Page 16: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Overview of the code

How “Six Degrees” is put together

Page 17: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Overview of the code

Version 1:

• A handful of FBML pages

• A lot of database triggers• User profiles updated in real time

Page 18: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 1 – 2,000 users

• A few bugs to fix

• Scaling is a minimal issue

• Time to add a new feature:– Show connections back

• Moaning users: “I guess you all are in a "repair stage", and the application is unusable at this point? Please notify us when it's been fixed. Thank you!”

Page 19: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Beginning week 2

• Starting to slow down

• “A few kinks”

• Time to streamline the API calls• … and the database

• Use fb:ref to minimise profile updates

• Use an iframe to perform background tasks

Page 20: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Hack #1: Iframe tasks

• FBML is useful, but Facebook doesn’t wait around...

• ...whereas the API may make you wait• Put tasks that may take time into an iframe

• Pass parameters through the fb:iframe src attribute e.g. <fb:iframe src=“find.php?user1=12345&user2=23456" />

Page 21: SCALING FACEBOOK APPS - Karl Bunyan November 28th

The application

[Iframe]

Page 22: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 2 – 10,000 users

• It’s getting busy

• New features have new demands

• Almost every calculation is in an iframe• Queuing of profile updates

• “select id from users with (nolock)”

• More RAM for the server

• The “Calculating…” script

Page 23: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Hack #2: Calculating…

Page 24: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Hack #2: Calculating…

• One iframe:– contains a dynamic ‘calculating’ message– and another iframe

• which does the calculation• and tells its parent when it’s finished

• All done through DHTML

Page 25: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 4 – 100,000 users

• Separate server for the database– Dual core– 4 gig ram

• Remove the triggers and run “cleanup” scripts

Page 26: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 12 – 200,000 users

• Outgrowing the database server

• Queries are often taking minutes

• 20,000 page views/day, but performance is poor

Page 27: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 12 – 200,000 users

More user comments:

“It used to work and it was brilliant. The other day, I was showing it to my friends and it didn't work. I tried the same names that gave results before and even they didn't work. Sometimes it does not find the person, sometimes it times out and etc...Make it work, plz!”

Page 28: SCALING FACEBOOK APPS - Karl Bunyan November 28th

Week 12 – 200,000 users

Next steps:

• The concept was never intended to scale

• Needs revision and rethinking

I’d rather have to change the application once I have the users.

Page 29: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to scale?

Page 30: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to scale?

• Design for scale?– Don’t know what’s going to be popular– It’s not a bad problem to solve

• Give the user something on the screen

• Limit API calls in FBML

• Use queuing for non-critical updates• Use the “about” wall to communicate

Page 31: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to scale?

All condensed into one simple PHP function:

Page 32: SCALING FACEBOOK APPS - Karl Bunyan November 28th

How to scale?

if($app->isScalable())

{

$server++;

}else{

break;

}

Page 33: SCALING FACEBOOK APPS - Karl Bunyan November 28th

S c a lin g a n a p p lic a t io nT h a n k s

F a c e b o o k D e v e lo p e r ’s G a r a g eN o v e m b e r 2 8 t h 2 0 0 7