4. "Scalability" is not equivalent to "Raw Performance"
Understand environmental workload conditions that the system is design for
Understand who is your priority customers
Scale out and Not scale up
Keep your code modular and simple
Don't guess the bottleneck, Measure it
Plan for growth
What Should We Focus On?
4
http://horicky.blogspot.com/2008/02/scalable-system-design.html
5. Techniques
Server Farm/Cluster (real time access)
Data Partitioning
Map / Reduce (Batch Parallel Processing)
Content Delivery Network (Static Cache)
Cache Engine (Dynamic Cache)
Resources Pool
Calculate an approximate result
Filtering at the source
Asynchronous Processing
Common Techniques
5
7. Load Balancer
•A dispatcher determines which worker instance will handle a request based on different policies.
Scatter and Gather
•A dispatcher multicasts requests to all workers in a pool. Each worker will compute a local result and send it back to the dispatcher, who will consolidate them into a single response and then send back to the client.
Result Cache
•A dispatcher will first lookup if the request has been made before and try to find the previous result to return, in order tosave the actual execution.
Shared Space
•All workers monitors information from the shared space and contributes partial knowledge back to the blackboard. The informationis continuously enriched until a solution is reached.
Pipe and Filter
•All workers connected by pipes across which data flows.
MapReduce
•Targets batch jobs where disk I/O is the major bottleneck. It use a distributed file system so that disk I/O can be done in parallel.
Bulk Synchronous Parallel
•A lock-step execution across all workers, coordinated by a master.
Execution Orchestrator
•An intelligent scheduler / orchestrator schedules ready-to-run tasks (based on a dependency graph) across a clusters of dumb workers.
8 Commonly Used Scalable System Design Patterns
7
8. Load Balancer (1)
8
•There is a dispatcher that determines which worker instance will handle the request based on different policies.
•The application should best be "stateless" so any worker instance can handle the request.
11. Scatter and Gather
11
•This pattern is used in Search engines like Yahoo, Google to handle user's keyword search request ... etc.
12. Result Cache
12
The dispatcher will first lookup if the request has been made before and try to find the previous result to return, in order to save the actual execution.
15. Map Reduce
15
The model is targeting batch jobs where disk I/O is the major bottleneck. It use a distributed file system so that disk I/O can be done in parallel.
20. •Users
–More than 400 million active users.
–50% of active users login each day.
–Average user has 130 friends on the site.
•Activity
–User spends an average of 55 minsper day.
–More than 60 million status updates each day.
–More than 100 million photos uploaded each day.
•Platform
–Currently 500K active applications.
–About 250 apps have more than 1 million users.
–About 60 million users use FB Connect from external web sites each month.
Facebook Web SiteStatistic
20
21. Challenges
•High Concurrency
•High Data Volumes
•Multilevel Hierarchical Data
Ok to Live with
•Not Mission Critical
•Cached data is fine
•Write Failures are tolerable
Facebook Web SiteTechnical Challenges
21
22. •General Design Principles
–Use open source where possible
–Unix philosophy
•Keep individual components simple yet performant
•Combine as necessary
•Concentrate on clean interface points
–Build everything for scale
–Try to minimize failure points
–Simplicity, Simplicity, Simplicity.
Facebook Web SiteArchitecture
22
23. Facebook Web SiteArchitecture
23
Services
Cache
Database
File System
Presentation Layer
Load Balancer
Web Servers runingPHP, basically
assembles data from lower layer
and presents it on the page.
Backend services are mainly implemented in C++(other language can be used). Very fast.
Memcachedis used to cache almost everything that is needed to produce a page
An array of MySQLservers are used in an interesting way to store the data
Internally developed file system, Haystackused to store photos.
26. •Presentation Layer: PHP
•Issues:
–High CPU and memory consumption.
–An Interoperability with C++ Challenging.
–Language doesn’t encourage good programming in the large.
–Initialization cost of each page scales with size of code base
Facebook Web SiteWeb Tier at Facebook
26
70
40
38
21
6
3
2
1
0
10
20
30
40
50
60
70
80
Ruby
PHP
Perl
Python
Erlang
C#
Java
C++
Relative Performance of Language Runtime (lower is better)
Programming Language performance ranking
28. •HipHop
–Source Code Transformer
•Transform PHP in to highly optimized C++ and then compile it using g++
–50% reduction in CPU usage than Apache + HTTP
–Facebook’s API tier can serve twice the traffic using 30% less CPU
–It has embedded simple webserver on top of libevent.
Facebook Web SiteWeb Tier at Facebook
28
https://github.com/facebook/hhvm
29. •Tornado: Facebook's Real-Time Web Framework for Python
–Tornado is a relatively simple, non-blocking Web server framework written in Python
–It is designed to handle thousands of simultaneous connections, making it ideal for real-time Web services.
Facebook Web SiteWeb Tier at Facebook
29
http://www.tornadoweb.org/
30. •BigPipe: first breaks web pages into multiple chunk called pagelets
Facebook Web SiteWeb Tier at Facebook
30
31. •Memcachedis an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
•Alleviate database load
•Over 25TB in-memory caching on more than 800 servers
•Multi-gets used to make the system more efficient
•Facebook contributes UDP support and performance enhanced to Memcached.
Facebook Web SiteMemcached
31
http://memcached.org/
32. •MySQL
–Fast and reliable
–Thousands of MySQL servers
•Users randomly distributed across these servers
–Relational aspect of DB is not used
•No joins. Logically difficult(Data is distributed randomly)
•Primarily key-value store
–Customizations
•Custom partitioning scheme –Global id assigned to all data
•Custom archiving scheme –Base on frequency and recencyof data on a per user basis
Facebook Web SiteMySQL Database
32
34. •Many services written in C++, Python, Java
–AdServer
–Search
–Network Selector
–News Feed
–Blogfeeds
–CSSParser
–Mobile
–ShareScraper
Facebook Web SiteServices
34
35. •Services Philosophy
–Create a service if required
•Real overhead for deployment, maintenance, separate code-base
•Another failure point
–Create a common framework and toolset that allow for easier creation of services
•Thrift
•Scribe
•ODS, Alerting, Monitoring service
–Use the right language
Facebook Web SiteServices
35
36. •How to community between these?
•Thrift
–http://incubator.apache.org/thrift/
–Lightweight software framework for cross-language development
–Provide IDL, statically generate code
–Supported bindings: C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and Ocaml
Facebook Web SiteServices
36
38. •Statistic
–Facebook has 200M active users
–800+ million user messages / day
–7+ million active channels at peak
–1GB+ in / sec at peak
–100+ channel machines
•System challenges
–How does synchronous messaging work on the Web?
•"Presence" is hard to scale
–Need a system to queue and deliver messages
–Millions of connections, mostly idle
–Need logging, at least between page loads
–Make it work in Facebook’s environment
Chat Service @ Facebook Statistic & Challenges
38
40. Chat Service @ Facebook System Overview
•User Interface
–Mix of client-side Javascriptand server-side PHP
–Works around transport errors, browser differences
–Regular AJAX for sending messages, fetching conversation history
–Periodic AJAX polling for list of online friends
–AJAX long-polling for messages (Comet)
•Back End
–Discrete responsibilities for each service
•Communicate via Thrift
–Channel (Erlang): message queuing and delivery
•Queue messages in each user’s “channel”
•Deliver messages as responses to long-polling HTTP requests
–Presence (C++): aggregates online info in memory (pull-based presence)
–Chatlogger(C++): stores conversations between page loads
–Web tier (PHP): serves our vanilla web requests
40
44. Chat Service @ Facebook Channel Server
44
•One channel per user
•Web tier delivers messages for that user
•Channel State: short queue of sequenced messages
•Long poll for streaming (Comet)
–Clients make an HTTP request
–Server replies when a message is ready
–One active request per browser tab
47. Big Data @ FacebookData Infrastructure Overview
47
Data Infrastructure @ FB built on open source technologies:
•Many committers across all the projects
•Plans to open source other parts of the data stack
•Figure out a model to stay in sync and still work at FB speed
48. Big Data @ FacebookLife of a tag for data infrastructure
48
49. Big Data @ FacebookLife of a tag for data infrastructure
49
•Technoloy:
–Log collection -Scribe
–Realtimeanalyitcs-Puma
–Batch analytics –Hive
–Adhocanalytics -Peregrine
–Periodic analytics -Nocron
50. Big Data @ FacebookWarehouse Architecture
50
4TB of compressed new data added per day.
135TB of compressed data scanned per day.
7500+ Hive Job on production cluster per day.
80K computer hours per day.
51. •Scribe
–http://developers.facebook.com/scribe/
–Scalable distributed logging framework
–Useful for logging a wide array of data
–Simple data model
–Built on top of Thrift
•Hive
–A system for managing and querying structure data built on top of Hadoop.
•Map Reduce for execution
•HDFS for storage
•Metadata in an RDBMS
–Key Building Principles:
•SQL as a familiar data warehousing tool.
•Extensibility –Type, Functions, Formats, Scripts.
•Scalability and Performance
•Interoperability.
Big Data @ FacebookWarehouse Architecture
51
52. Big Data @ FacebookWarehouse Architecture
52
•Hive Architecture
53. Big Data @ FacebookWarehouse Architecture
53
•Data Flow Architecture