780 likes | 946 Views
Adaptive Performance Optimization for Large Scale Web Applications. Xiaoliang “David” Wei Infrastructure, Facebook www.facebook.com/DavidWei. Agenda. About Facebook: the challenges. F acebook. The largest social network. F acebook: large scale.
E N D
Adaptive Performance Optimization for Large Scale Web Applications • Xiaoliang “David” Wei • Infrastructure, Facebook • www.facebook.com/DavidWei
Facebook The largest social network
Facebook: large scale • A web application with more than 400M users • A large web site • > 9 billion page views per days ( 1 msec / page => 2 human life of time a year ) • most traffiked site • A huge graph: • # of friends for a typical user: 130 • # of edges of the social graph: 130 * 400M ~ 50 Billions • A massive storage: • 25 billions of new contents each month • The large photo site
Facebook: viral growth • An open platform • 3rd party developers: > 1 millions • Active applications: > 550 K • Websites with Facebook Connect: > 250 K • Widgets all around the web
Facebook: fast evolution • A fast-moving product • Agile development: • One new revision each week • Patches pushed everyday • Urgent fixes 24/7 • Parallel product launches • > 100 experimental launches at the same time
Facebook: challenges • A unique large scale system • Large scale • No simple problem • Viral growth • No good assumption on growth and adoption • Fast evolution • Very small time window for product optimization
Facebook: under the hood • A relatively small team • ~400 engineers • Ratio: 1 engineer for 1 million users • Millions of lines of PHP codes • 2000 + Javascripts served from Facebook pages • 1000 + style sheets • Tens of thousands of static images (icons and etc)
Life of a web page request • User clicks on www.facebook.com Web Browser
Life of a web page request • User clicks • HTTP request sent to the server • DNS • TCP Handshake • HTTP Request (Path, parameter, cookies and etc) Web Browser NW Delay Servers
Life of a web page request • User clicks • HTTP request sent to the server • Web server accept the requests • Validation • Data retrieval • HTML generation • Post processing • Compression • Log Web Browser NW Delay Servers Server Delay
Life of a web page request • User clicks • HTTP request sent to the server • Web server accept the response • HTTP response sent to client • Basic HTML • External Images Link • External CSS: style control • External JS: user interaction Web Browser NW Delay Servers Server Delay
Life of a web page request • User clicks • HTTP request sent to the server • Web server accept the response • HTTP response sent to client • Browser renders: • Decompression • Analyze HTML, construct DOM • Load images, CSS, JS • Present the contents Web Browser Content Distribution Network (CDN) Client Delay NW Delay Servers Server Delay
Life of a web page request • The latency of a web page request: • Client Delay • Network Delay • Server Delay Web Browser Content Distribution Network (CDN) Client Delay NW Delay Servers Server Delay
Life of a web page request • The latency of a web page request: • Client Delay: 50% • Network Delay: 25% • Server Delay: 25% Web Browser Content Distribution Network (CDN) Client Delay NW Delay Servers Server Delay
Web service: a distributed system • Questions: • Scalability • Consistency • Performance • … Web Browser Content Distribution Network (CDN) Client Delay NW Delay Servers Server Delay
Servers: at the beginning • LAMP: • Platform: Linux • Web Server: Apache • Database: MySQL • Business Logic: PHP Web Browser Servers LAMP
Servers: more users… Web Server Web Browser MySQL Servers
Servers: even more users… Web Server Web Browser Memcache Server MySQL Servers
Servers: even more users… Web Server Web Server Web Server Web Browser Memcache Servers Memcache Servers MySQL MySQL MySQL Servers
Servers: even more users… Web Server Web Server Web Server Web Browser Memcache Servers Memcache Servers MySQL MySQL MySQL Servers
Static Content Distribution Apache+ Cache Apache+ Cache Apache+ Cache Web Browser Content Distribution Network (CDN) Distributed File Systems Servers
Static Content Distribution HTTP Server+ Cache HTTP Server+ Cache HTTP Server+ Cache Web Browser Content Distribution Network (CDN) Cache Cache Servers Distributed file System optimized for reads / costs
Client side: • 60%~90% of the end-to-end latency (while traditionally ignored) • Hostile environment: • IE6 / IE7 / IE8 / FF3/ Safari4 / Chrome … • Browser Add-ons • 3rd party scripts Web Browser Content Distribution Network (CDN) Servers
Performance impacts business • Google: +400ms => -0.6% daily search per user • Bing: +500ms => -1% click, +1.2 sec time to click (2x) • Facebook real time feed stories at summer 2009 (all 50-percentile): • +500ms (~ 25% of full latency) => -3% click • +1 sec ( ~50% of full latency) => -6% click
Status of web performance optimization • Decade-old protocols: TCP, HTTP, HTML • constraints & workaround • Fast-changing industry: browser war • Highly heterogeneous environments • Some rules & tools emerging (e.g. Steve Souder’s books, YSlow) • Large scale optimization is difficult
Web performance: some best-practice rules • Minimize HTTP requests (TCP initialization overhead) • Gzip resources (Bandwidth optimization) • Preload Components • Reduce DNS Lookup • Split Requests Across Domain (HTTP limits + TCP inefficiency)
Web performance: trade-offs • Minimize HTTP requests Increased unused bytes • Gzip resources Increased CPU utilization • Preload Components Increased bandwidth cost • Reduce DNS Lookup Split Requests Across Domain • Split Requests Across Domain Reduce DNS Lookup
Web performance optimization • Improve speed • Reduce cost (both latency penalty and $ cost) • Operate on the equilibrium • Easy for a single page, hard for large scale systems
Web performance: the gap • Large scale • Viral growth • Fast evolution “Reduce # of HTTP Requests” Package JS/CSS; Sprite Image Large Scale Operation
Web performance: fill the gap • Large scale • Viral growth • Fast evolution “Reduce # of HTTP Requests” Package JS/CSS; Sprite Image Interface, Model, Adaptive Infrastructure Large Scale Operation
Adaptive Performance optimization • Example: “Best Practices for Speeding Up You Web Site” – YSlow Rule 1: Minimize HTTP Request • Combined files: package Javscript and Stylesheets • Sprite Images • …
Performance optimization: Case Study • Day 1: Some smart engineers start a project! <Print CSS tag for feature A> <Print CSS tag for feature B> <Print CSS tag for feature C> <print HTML of feature A> <print HTML of feature B> <print HTML of feature C> … “Let’s write a new page with features A, B and C!”
Day 2: Some smart engineers read YUI blog and says… <Print CSS tag for feature A> <Print CSS tag for feature B> <Print CSS tag for feature C> <print HTML of feature A> <print HTML of feature B> <print HTML of feature C> … “A & B & C are always used; let’s package them together!”
Day 2: Awesome! <Print CSS tag for feature A&B&C in a single package> <print HTML of feature A> <print HTML of feature B> <print HTML of feature C> …
Day 3: feature C evolves… <Print CSS tag for feature A&B&C in a single package> <print HTML of feature A> <print HTML of feature B> If (users_signup_for_C()) { <print HTML of feature C>} …
Day 3: <Print CSS tag for feature A&B&C> <print HTML of feature A> <print HTML of feature B> If (users_signup_for_C()) { <print HTML of feature C>} … A&B are always used, while C is not. ..
Day 4: feature C is deprecated <Print CSS tag for feature A&B&C> <print HTML of feature A> <print HTML of feature B> // no one uses C { <print HTML of feature C>} …
Day 4: we start to send unused bits <Print CSS tag for feature A&B&C> <print HTML of feature A> <print HTML of feature B> // no one uses C { <print HTML of feature C>} … It is hard to remember we should remove C here.
One months later… <Print CSS tag for feature A&B&C&D&E&F&G…> if (F is used) <print HTML of feature F> <print HTML of feature G> if (F is not used) { <print HTML of feature E>} … Thousands of dead CSS bytes in the package.
Performance Optimization: the gap • One months later… <Print CSS tag for feature A&B&C&D&E&F&G…> if (F is used) <print HTML of feature F> <print HTML of feature G> if (F is not used) { <print HTML of feature E>} … “Reduce # of HTTP Requests” Package JS/CSS; Sprite Image Large Scale Operation
Adaptive Static Resource Optimization Fill the gap: • Build interface to separate requirement declaration and delivery of static resources • Model the trade-off of optimization • Infrastructure to analyze requirement logs and globally optimize the delivery performance “Reduce # of HTTP Requests” Package JS/CSS; Sprite Image Interface, Model, and Infrastructure Large Scale Operation
Interfaces • Back to Day 1: require_static(A_css); <render HTML of feature A> require_static(B_css); <render HTML of feature B> require_static(C_css);<render HTML of feature C> <deliver all required CSS> <print all rendered HTML> Separate Declaration from actual Delivery Requirement Declaration lives with HTML Global Optimization on Delivery
Packager: Global JS/CSS Optimization Offline analysis Online process require_static(A_css); <render HTML of feature A> require_static(B_css); <render HTML of feature B> require_static(C_css); <render HTML of feature C> <deliver all required CSS> <print all rendered HTML> Usage Pattern logs Packaging algorithm “Optimal” packages