gdd japan 2009 - designing opensocial apps for speed and scale

68

Upload: patrick-chanezon

Post on 15-Jun-2015

2.185 views

Category:

Technology


0 download

DESCRIPTION

Google Developer Days Japan 2009 - Designing OpenSocial Apps For Speed and ScaleOriginal slides from Arne Roomann-Kurrik & Chris Chabot with a few Zen quotes and references added by me:-)

TRANSCRIPT

Page 1: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale
Page 2: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Designing OpenSocial Apps For Speed and ScalePatrick Chanezonwith slides fromArne Roomann-Kurrik & Chris Chabot June 9 2009

Page 3: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Social application eCPM is lower than average

• Direct access to millions of signed in userso Growth functionality built into social platforms

• Small margins x Lots of users = Small Tweaks Matter

Success In A Social Market

Chart from Pubmatic AdPrice Index 2008: http://pubmatic.com/adpriceindex/index.html

Page 4: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

"Improving our latency is really, really important" +0.5 seconds costs Google 20% search traffic

Marissa Mayer, Google http://bit.ly/idItc ~12:50

Page 5: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

"Even very small delays would result in substantial and costly drops in revenue" +0.1 seconds costs Amazon 1% of sales

Greg Linden, Amazon http://glinden.blogspot.com/2006/11/marissa-mayer-at-web-20.htmlhttp://home.blarg.net/~glinden/StanfordDataMining.2006-11-29.ppt

Page 6: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

"If you make a product faster, you get that back in terms of increased usage" -30KB gave Google Maps 30% growth in 3 weeks

Marissa Mayer, Googlehttp://www.youtube.com/watch?v=6x0cAzQ7PVs ~18:10

Page 7: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

A monk asked Joshu:“Will my web application have Buddha-nature if I optimize for speed or number of features?”

Joshu answered:“Mu”

Liberally adapted from “Zen Flesh, Zen Bones”Jōshū Jūshin (778–897)

Image Source http://en.wikipedia.org/wiki/Image:BodhidharmaYoshitoshi1887.jpg public domain

Page 8: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Measure impact of changes on large scale apps • Sample OpenSocial application

o Different strategieso Real performance numbers

• Goals

o Deliver a fast user experienceo Minimize costs of running a large social appo Highlight new OpenSocial features

This Presentation

Page 9: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Introducing Quartermile

Photo by Phil McElhinney http://www.flickr.com/photos/philmcelhinney/1000986005/

Page 10: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile UI

Page 11: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Backend:o Built on Google App Engineo Remote Procedure Calls o JSON

• Frontend:

o Gadgets on many containerso JavaScript library for RPC calls

• View the source: http://bit.ly/quartermile-src• XML spec: http://bit.ly/quartermile-app

Building Quartermile

Page 12: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Request typeso Quartermile API o OpenSocial APIo Assets (images, JS, CSS)

• Metrics

o Bandwidth / PV (KB)o Requests / PV (#)o Latency / PV (ms)o CPU time (megacycles)

• Measuring each individually is important• Have more control over some than others

Measuring Quartermile

Page 13: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Assets

Social requests

Quartermile API calls 44.21

Latency(ms)

Requests / PV Bandwidth / PV(kb)

Quartermile 'Naive' Implementation Metrics

6490

21.45 804

22135.6 3152

CPU time (megacycles)

2078

0

0

Page 14: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile 'Naive' Implementation Costs

Page 15: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web DevelopmentBest Practices*

Photo by rudolf_schuba http://www.flickr.com/photos/rudolf_schuba/153225000/

* Gadgets are web pages too!

Page 16: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• We're interested in the following metrics:o Application sizeo Request counto Latency of initial load

• Measuringo Safari: Web Inspectoro Firefox: Firebug & YSlowo IE: HttpWatch

Web Development Best PracticesGadgets are web pages too!

Page 17: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Concatenate JavaScript & CSS files o Reduces latencyo Reduces HTTP Requests

• Compress JavaScript and CSSo Reduces download size

Web Development Best PracticesMinify JavaScript & CSS

var foo = ['1','2','3','4'];for (var i = 0 ; i < 4 ; i++) { alert(foo[i]);}

var foo=["1","2","3","4"];for(var i=0;i<4;i++){alert(foo[i])}

Page 18: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web Development Best PracticesDetermining application size

Page 19: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile Improvement After JavaScript + CSS Minification (YUI Compressor)

Page 20: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web Development Best PracticesMeasuring Latency

Page 21: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web Development Best PracticesMeasuring latency

Page 22: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web Development Best PracticesLatency is not a static number

Page 23: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Measure from different locations• Measure often

Web Development Best PracticesDecreasing latency increases user happiness and engagement

Page 24: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• JavaScript code for recording gadget latency:

var startTime = new Date();var imgElement = new Image()imgElement.onload = function() { var endTime = new Date(); var latency = endTime. getTime() - startTime.getTime(); // report latency back to your server}imgElement.src = "http://your.server.com/ping.gif";

Web Development Best PracticesAutomatically collecting latency measurements

Page 25: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Concatenate images into a single file (sprite) and use CSS to selectively display portions of the spriteo Reduce # of requestso Reduce latency

Web Development Best PracticesSpriting Images

Page 26: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Web Development Best PracticesDetermining Request Count With YSlow!

Page 27: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile Improvement After Image Spriting

Page 28: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Use the browser's cache for static datao Reduces total bandwidth & requestso Reduces latency

• Apache configuration example:

Web Development Best PracticesAdjusting Cache Headers

<FilesMatch "\.(css|js|gif|jpe?g|png)$"> Header set Cache-Control "max-age=290304000, public"</FilesMatch>

http://example.org/css/style.css?v=3

• Use Cache-busting to force refresh

Page 29: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Server AssistedOptimizations

Photo by zappowbang http://www.flickr.com/photos/zappowbang/3202362752/

Page 30: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Social gadget:o Small tweak == big gain

• Social networko Many small tweaks == very big gain

• Social network advantages:

o Control over the HTML they outputo Better network infrastructure

Server Assisted Optimizations

Page 31: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Social network willing to absorb some traffic for you o Could use content delivery networks (CDNs) o Distributed network, great for serving static content

all over the world• Important for clients further away from your servers!

Static Content Proxies

var div = $("#flashcontainer"); var url = gadgets.io.getProxyUrl( "http://me.com/flash.swf");gadgets.flash.embedFlash(url, div, 10);

Page 32: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Social network has control over its own outputo CSS first, JS lasto Concatenate, Minify o Rewrite URLs for static proxy

• Caching controls in your gadget spec:

Content Rewriting

<Module> <ModulePrefs> <Optional feature="content-rewrite"> <Param name="include-urls"></Param> <Param name="exclude-urls">.*</Param> <Param name="include-tags"></Param> </Optional> ...

Page 33: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Image by Paul Downey http://www.flickr.com/photos/psd/2841928867/in/datetaken

OpenSocial Best

Practices

Page 34: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Some optimizations only make sense in a social application context

• OpenSocial offers conveniences to social app developers

• Learn from the OpenSocial API when designing your

own application interfaces

OpenSocial: Designed For Social Apps

Page 35: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

One API call == 1 HTTP request:

Do as much as you can in a single trip:

2 -> 1 OpenSocial requests4 -> 1 Quartermile API requests

Batchingosapi.people.getViewer().execute(onVwr);osapi.people.getOwner().execute(onOwnr);osapi.people.getViewerFriends().execute(onFrnd);osapi.people.getOwnerFriends().execute(onOFrnd); var batch = osapi.newBatch() .add("vwr", osapi.people.getViewer()) .add("vfd", osapi.people.getViewerFriends()) .add("owr", osapi.people.getOwner()) .add("ofd", osapi.people.getOwnerFriends()) .execute(onData);

Page 36: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile Improvement After Request Batching

Page 37: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• The Naive implementation makes a lot of requests• How can we improve on that?

OpenSocial Best PracticesData Pipelining + Proxied Content

Page 38: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Using OpenSocial 0.9's Data-Pipelining, we can declare which social data to POST to your server

• Your server operates on the data and returns the HTML to display

• Available in iGoogle & orkut sandboxes, coming to a container near you soon(tm)

OpenSocial Best PracticesData Pipelining + Proxied Content

Page 39: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

OpenSocial Best PracticesData Pipelining + Proxied Content

<Module> <ModulePrefs ... etc .../> <Content type="html" view="profile" href="http://yoursite.com/proxied.php" authz="signed"> <os:ViewerRequest key="vwrData" fields="id,displayName"/> <os:OwnerRequest key="ownData"/> <os:PeopleRequest key="ownFriends" userId="@owner" groupId="@self"/> </Content> </Module>

Page 40: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

OpenSocial Best PracticesData Pipelining + Proxied Content

<?php $postData = json_decode(file_get_contents("php://input")); echo "<h1>Hello {$postData['vwrData']['name']}</h1>";echo "These are {$postData['ownData']['name']}'s friends:"; echo "<br/>";

foreach ($postData['ownFriends'] as $friend) { echo "{$friend['name']}<br/>";}

Page 41: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile Improvement After Data Pipelining

Page 42: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• New application design pattern available with the features introduced in 0.9

• When your internal state changes on the application server, use REST/RPC calls to invalidate data:o Per user or urlo Application ID is determined by 2 Legged OAuth

call

OpenSocial Best PracticesInvalidation Pattern

Page 43: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Calling the invalidation API:

OpenSocial Best PracticesInvalidation Pattern

POST /api/rest/cache/invalidate HOST opensocial.example.org Content-Type: application/json { invalidationKeys : [ "http://www.myapp.com/gadgetspec.xml", "http://yoursite.com/proxied.php" "user:123"] }

Page 44: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Image by euthman http://www.flickr.com/photos/euthman/1846038389/

Optimizing Your Data Store

Page 45: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Database joins against friend lists are generally very expensive

• Plan ahead if you're not using App Engineo Master / Slave architectureo Database partitioning

• Use Memcache to cache data (in App Engine too!)o Filter results in software, make the most of cache

• Consider storing frequently used data in JSON Blobs instead traditional relational storage

OpenSocial Best PracticesData Store Structuring

Page 46: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Consider using background processingo Updates are slightly delayedo Doesn't block user interactiono Great for "What are your friends doing" result sets

• Use a off-the-shelf / open source Queue system or• App Engine, use cron.yaml:

OpenSocial Best PracticesData Store Structuring

cron:- description: process activities entries url: /tasks/processActivities schedule: every 1 minutes

Page 47: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• How to plan a scalable social application?

• Prefer to enforce hard limits up front, than deliver a poor user experience

• Decided friend queries were too expensive:

o orkut lets you have 1000 friendso MySpace lets you have 100,000s+ of friends

• Do all 100,000 friends need to see your exercises?

o Created artificial idea of "teams"o Choose a subset of friends to invite to your teamo Side effect: Drive adoption!

Designing Quartermile's Data Model

Page 48: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Goal: Fetch all of a team's data for any given week in one database query

• How many users can we put on a team?

• App Engine returns 1000 entries max for any query • 1 entry / workout • 3 / day ~ 20 / week

• 1000 / 20 = 50 users

Dreaming Up Reasonable Limits

Page 49: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Goal: Implement "updates from your friends" inside of the Quartermile app

• Slow!

o Fetch all friendso See which updated recentlyo Sort

• Friend updates are lower priority than team updateso Process in the backgroundo Fetch friends using 2-legged OAuth o Do "updates from friends" calculation o Store result of calculation (can be stale)

Limits: Made To Be BrokenNot every app will be able to enforce such restrictions

Page 50: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Database • Memcache• OpenSocial App Data

Where To Put It?

Page 51: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

"App Data is one of the most misunderstood and misused portions of the OpenSocial specification" App Data is often used incorrectly

Arne Roomann-Kurrik, Google May 2009 at Google I/O

Page 52: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Data store of keys and valueso Essentially public, so you can't put secrets hereo User writable via JS, so you can't trust ito But it's fast!

• Perfect place to cache slow data • Background process does slow calculation• Pushes result to AppData• When rendered, injected directly into the gadget

o No server hit!

App Data

Page 53: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Photo by redjar http://www.flickr.com/photos/redjar/704710355/

Container-MandatedOptimizations

Page 54: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• 'Naive' implementation:o Easy to make mistakes

• Container:

o Keep gadgets fast == keep container fasto Userbase affects acceptable latency valueso Constraints to keep gadget developers honest

Container-Mandated Optimizations

Page 55: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Directory will soon be taking latency into account • Implicit latency penalty, too:

iGoogle's Latency

Page 56: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

• Profiles:o The most traffic on orkuto Users love gadgets!

• OpenSocial 0.9 templates

o Can display social data o Can display App Datao No external fetches or dynamic data

• Produces an extremely fast profile render• Great use case for AppData cache

orkut's Template-Only Profiles

Page 57: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

orkut's Template-Only Profiles

<Module> <ModulePrefs title="Template"> <Require feature="opensocial-data" /> <Require feature="opensocial-templates"> <Param name="process-on-server">true</Param> </Require> </ModulePrefs> <Content type="html" view="profile"><![CDATA[ <script type="text/os-data"> <os:PeopleRequest key="friends" userId="@viewer" groupId="@friends"/> </script> <script type="text/os-template"> <div repeat="${friends}">${Cur.name.givenName}</div> </script> ]]></Content></Module>

Page 58: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Photo by peagreengirl http://www.flickr.com/photos/peagreenchick/384744358/in/photostream/

Summary

Page 59: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Comparison of each techniqueSummary

Latency:• JS Minification• Spriting• Content Rewriting• Content Proxy• Batching• Data Store Optimization• Background• App Data Cache• Pipelining• Invalidation• Limited Profiles• Cache Headers

App Size:• JS Minification• Content Rewriting• Content Proxy• Pipelining• Invalidation• Cache Headers

Requests:• JS Minification• Spriting • Content Rewriting• Cache Headers

Page 60: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Quartermile 'Most Optmized' Implementation

Page 61: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

All you ever wanted to know about web apps performance tuning

http://stevesouders.com/

61

Page 62: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

A monk asked Zhaozhou to teach him.Zhaozhou asked, "Have you eaten your meal?"The monk replied, "Yes, I have.""Then go wash your bowl", said Zhaozhou.At that moment, the monk was enlightened.

無門関, Mumonkan

Performance 悟り?)

Image Source http://commons.wikimedia.org/wiki/File:Satori.svg public domain

Page 63: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Page Speed Firefox Extension

• Released June 4 2009• http://code.google.com/speed/page-speed/

63

Page 64: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

64

Google Developer Relations Japan

• Google の デベロッパーコミュニティサイト– Google Developer Day セッションのスライドやビデオ– イベント情報

http://sites.google.com/site/devreljp/

Page 65: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

65

code.google.com

• Google の デベロッパー向けサイトをご利用ください。– 日本語のドキュメント– 日本語のデベロッパーコミュニティ

http://code.google.com/intl/ja/

Page 66: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

66

アンケートにご協力ください

• Google Developer Day をより充実したものとするためにアンケートにご協力ください。

• アンケートは2種類あります。– 全体アンケート– セッションアンケート

• セッションアンケートはお手元の用紙でも回答できます。– アンケートは Google Developer Relations Japan で回答できます。

http://sites.google.com/site/devreljp/

Page 67: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale

Q & A

Page 68: GDD Japan 2009 - Designing OpenSocial Apps For Speed and Scale