AJAX for ScalabilityErik SchultinkPHP Barcelona – 30 Oct, 2009
AJAXWhat is AJAX?“Asynchronous JavaScript and XML”Paradigm for client-server interactionChange state on client, without loading a complete HTML page
Traditional HTML BrowsingUser clicks linkBrowser sends requestServer receives, parses request, generates responseBrowser receives response and begins renderingDependent objects (images, js, css) load and renderPage appears
AJAX BrowsingUser clicks linkBrowser sends requestServer receives, parses request, generates responseBrowser receives response and begins renderingDependent objects (images, js, css) load and renderPage appears
Why Asynchronous?Not always necessary to wait for server responseMost of response is predictable
Scalability?Through-put, not speedIdeally, capacity is linear function of machinesIn practice, linear below some boundary
What is Tuenti.com?
Tuenti.comStarted 2007More than 20 billion pageviews/monthMore than 20k/second at peakBased in Madrid~80 employees, 40 engineers
How does Tuenti use AJAX?Only pageloads are login and home pageLoader pulls in all JS/CSSAfterwards stay within one HTML page, rotating canvas area content
How does Tuenti use AJAX?PHOTO UPLOAD
How does Tuenti use AJAX?CHAT
How does Tuenti use AJAX?CHAT
How does Tuenti use AJAX?
How does Tuenti use AJAX?
Traditional Systems Architecturewww.tuenti.comLoad BalancerWeb server farmWeb server farmWeb server farm
Traditional Systems Architecturewww.tuenti.com12.45.34.17912.45.34.178Load BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farm
Client-side Routingwww.tuenti.comwwwb3.tuenti.comwwwb2.tuenti.comwwwb1.tuenti.comwwwb4.tuenti.comLoad BalancerLoad BalancerLoad BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farmLinearly scalable …
Client-side Routingwww.tuenti.comwwwb3.tuenti.comwwwb2.tuenti.comwwwb1.tuenti.comwwwb4.tuenti.comLoad BalancerLoad BalancerLoad BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farmLinearly scalable … except for top level
Client-side Routingwww.tuenti.comwwwb3.tuenti.comwwwb2.tuenti.comwwwb1.tuenti.comwwwb4.tuenti.comLoad BalancerLoad BalancerLoad BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farmlots of content creation = lots of dynamic data
Client-side Routingwww.tuenti.comwwwb3.tuenti.comwwwb2.tuenti.comwwwb1.tuenti.comwwwb4.tuenti.comLoad BalancerLoad BalancerLoad BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farmCache Farm lots of dynamic data = lots of cache = internal network traffic
Client-side Routingwww.tuenti.comwwwb3.tuenti.comwwwb2.tuenti.comwwwb1.tuenti.comwwwb4.tuenti.comLoad BalancerLoad BalancerLoad BalancerLoad BalancerWeb server farmWeb server farmWeb server farmWeb server farmCache FarmCache FarmCache FarmCache FarmPartition cacheRoute requests to a farm near cache needed to respond
Internal network savings
Balancing LoadTop-level requests to www.tuenti.comEach request tells client which farm it should be using, based on a mappingMapping can be changed to balance load, perform maintenance, etc
Robust BrowsingDon’t tightly couple layersClient and server are separate layers
Robust BrowsingClient-side code should be robust against server-side failuresWhat can go wrong?Client loses connectivityServer-side errorWeb server overloadedEntire server farm overloadedServer farm loses connectivity
Robust BrowsingOptions:Retry (carefully)Switch to alternate farm (carefully)Detect error, give user feedback
CachingThink like server-side scalabilityCache data that is likely to be requested in the futureDoesn’t have consistency issuesDoesn’t change
Cache Data in Client BrowserMore responsive for userFewer requests to your servers
Some detailsLots of dependencies to load initiallyLoading can be brittle and slowBrowser performance
Some detailsBeware of external librariesGoogle JS lib are not static, not indefinitely cacheable
Some detailsFriends do have consistency issuesAdd/remove friendsChange avatar (rare)
Image ServingTuenti serves ~2.5 billion images/dayAt peak, this is >6 Gbps and >70k hits/secWe use CDNs
What is a CDN?Content Delivery Network
What is a CDN?Examples: Akamai, Limelightalso dozens more, including AmazonBig distributed, object cachePay per use either per request, per TB transfer, or per peak Mbps
What is a CDN?Advantages:Outsource dev and infrastructureGeographically distributedEconomies of scaleDisadvantages:High costLess control and transparencyCommitments
What affects image load time?Client internet connectionResponse time of CDNCDN cache hit rateResponse time of Origin
What affects image load time?Client internet connectionResponse time of CDNCDN cache hit rateResponse time of Origin
Monitor Performance from ClientCloser to performance experienced by end-userOnly way to get view of network issues faced by users (ie last mile)
Good MonitoringPercentiles > averages Less noise, more stableComparable to SLA termsEstablish baselines performance relative to past more important than absoluteAvoid false positives
How to fix slow ISP?Choose better transit providerSet-up peering (or get CDN too)Traffic management
What affects image load time?Client internet connectionResponse time of CDNCDN cache hit rateResponse time of Origin
Quality of End-User Experiencevs.Cost
We use multiple CDNs, and shift content based on price/performance.
Know your content
Know your content
Know your content
Know your content3075200
Know your content600
Know your content120
Know your content
Pre-fetch ContentExploit predictable user behaviorEx: clicking to next photo in an albumSimple solution – load next image hiddenClient browser will cache it (next response < 100 ms)Increase tolerance for slow response time
Pre-fetch ContentMore complex solutionPre-fetch next canvas (full html), render in background – rotate in on NextEven more complexInstantiate HTML template w/ data  on clientPre-fetch data X photos in advance, render Y templates in advance with this data
Pre-fetch ContentProblems:Rendering still takes timeIncreases browser loadNeed to set cache headers correctly
Image deliverySmall images: High request, low volumeMost cost-effective to cache in memoryLarge images: High volume, low requests, greater tolerance for latency
What affects image load time?Client internet connectionResponse time of CDNCDN cache hit rateResponse time of Origin
Monitor Performance from Clientcold servers online
What affects image load time?Client internet connectionResponse time of CDNCDN cache hit rateResponse time of Origin
Deploy cache layers (squid, varnish)Optimize web servers for static content deliveryMore machinesAvoid RAID, file systemImprove Origin Response
SummaryCache and pre-fetch contentClient-side routingClient-side monitoring
General caveatsBe careful with browser performanceLook carefully at what’s in the worst 5%Don’t assume that high values are incorrect
Morejobs.tuenti.comPresentationSaturday 14:20Challenge (win a PS3)Talk: Continuous IntegrationDavideMendolia, Saturday 12:00
More? Ask us!
More? Ask us!
Q & A

AJAX for Scalability

Editor's Notes

  • #3 JavaScript and XML are just technologies; “Asynchronous” is what’s important – the shift in thinking from web browsing as serial page by page to more fluid navigation that’s wholly contained within the same HTML page. I’m not going to go much into implementation, etc – it’s a lot of detail, and talking about cross-browser compatibility isn’t so fun or interesting. Focus on approaches – what we’ve learned from scaling on the server side can be applied to client side.
  • #4 Using AJAX in application design, allows 1-6 to collapse a bit
  • #5 Using AJAX in application design, allows 1-6 to collapse a bit
  • #8 ComScore numbers show that we have more traffic than all Google properties combined. ComScore estimates 1 in 6 web pages viewed in Spain is from Tuenti. ComScore numbers are lower than our internal measurements.
  • #10 Consider breaking into slides that illustrate this process
  • #16 Not just well structure
  • #17 Not just well structure
  • #18 Not just well structure
  • #19 Not just well structure
  • #20 Not just well structure
  • #21 Not just well structure
  • #22 Not just well structure
  • #48 Competitive market – only 2 (Akamai and Limelight) are financially very healthy – and Limelight is losing money if you consider investments