SlideShare a Scribd company logo
1 of 34
Download to read offline
Caching & Performance:
Lessons from Facebook


             Lucas Nealan

          OSCon, Portland OR
      July 23, 2008, 14:35 – 15:20
Facebook

Social Utility
Sharing and communicating efficiently
4th most trafficked site in the World *




                  * ComScore 2008
Facebook Stats


Over 90 million active users
~ 50 pages per user daily
Over 24,000 platform applications
Facebook Architecture

                                       !5678



         -**. /012",
                       (") *+",




34%#%+
                                  !"#$%$&"'
Complexity
Connecting to all Database is impossible
Large codebase
Scaling affects resources in many ways
  ‣ Memory consumption
  ‣ Socket connection limits
Cache retrieval is ~ 10% cpu-user of most pages
What are the Benefits of Caching?
Caching Layers

    $GLOBALS
       APC
    Memcached
     Database
   Browser Cache
  Third Party CDN
Globals Cache
function cache_get($id, $key, $apc=false) {
  if (isset($GLOBALS['CACHE']["$key:$id"])) {
    $cache = $GLOBALS['CACHE']["$key:$id"]));
    $hit = 1;
  } elseif ($apc && (($cache = apc_fetch("$key:$id")) !==
            false) {
    $hit = 1;
  } else {
    ... // fetch from memcached
    if ($apc) apc_store("$key:$id", $cache);
  }
  if ($hit) $GLOBALS['CACHE']["$key:$id"] = $cache;
  return $hit ? $cache : NULL;
}
Globals Cache
         Avoids unnecessary APC and Memcached requests
         Automatic via abstraction
         But still has function call cost overhead

foreach($ids as $id) {
  if (isset($GLOBALS['CACHE']["profile:$id")) {
    $profile = $GLOBALS['CACHE']["profile:$id"];
  } else {
    $profile = cache_get($id, 'profile');
  }
}
APC
Opcode caching
  ‣ Hundreds of included libraries
  ‣ Thousands of functions
Variable caching
  ‣ Hundreds of MB’s of data
APC User Cache
Non-user specific data
  ‣ Network/School information
  ‣ Database information
  ‣ Useragent strings
  ‣ Hot application data
  ‣ Site variables
  ‣ Languange Strings
Friends

           Normal      4050ms


            APC        135ms


          apc.stat=0   128ms
APC Opcode Priming
$path      = '/path/to/source';
$exclude   = $path.'/www/admin/';
$expr      = '.*.php';
$files =   split(' ', exec("find -L $path -regex '$expr'
                           | grep -v '$exclude' | xargs"));

// prime php files
foreach($files as $file) {
  apc_compile_file($file);
}
APC+SVN Client Cache Busting
function get_static_suffix($file) {
  global $ROOT;
  if ($version = cache_get($file, ‘sv’, 1) === null) {
    $version = trim(shell_exec(“svn info $ROOT/$file | grep
    ‘Changed Rev’ | cut -c 19-”));

        apc_store(“sv:$file”, $version);
    }

    return '?'.$version;
}
APC + Useragent Strings
Useragent string parsing is inefficient in PHP
Cache parsed useragents in APC for the first 10 minutes
Hit rate of over 50%


Pear implementation available:
  ‣ PEAR::Net_Useragent_Detect_APC
Site Variables

Enable/Disable site features across all servers
Configure memcached cluster IP’s
Configure product features
Version memcached keys for invalidation
Site Variables
                   9+;$6<-87-8    !"#                 -.(&91<67686
                   6'"5$'7686
                                  -./01$23
                   -.9&98$7#:#    -./01$23
                                  -./.$'-"+,
                                  -./.$'-"+,4#"5$                                -./.$'-"+,
                   -.<6;&#$7686


                                                                     =$59&98$;




*+;$ B<-8 !$'.$'
                                                                           =2!>?
  -.(&91<67686                                                           !"#$.&' @A
                                        !"#$ %&'"&()$ *+,-+)$
Memcached
Distributed object cache
Facebook currently utilizes > 400 memcached hosts
With > 5TB in memory cache
Facebook contributions:
  ‣ UDP Support
  ‣ Performance Enhancements
Many choices in opensource clients
What to cache?
 User Specific Data
   ‣ Long profile
   ‣ Short profile
   ‣ Friends
   ‣ Applications
 Key versioning
   ‣ sp:6:10030226
Cache Retreival
Create Wrapper functions:
  ‣ cache_get($id, <key>, <miss>, $apc, $timeout);
  ‣ cache_get_multi($ids, <key>, <miss>, $apc);


Cache key callback function:


   function profile_key($id) {
     global $VERSION_SP; // primed site variable
     return “sp:$VERSION_SP:$id”;
   }
Cache Multiget
Sort keys into buckets of servers   foreach($keys as $key => $kdata) {
                                      $host = get_host($key); // hash
For each server                       $keys_hosts[$host][$key] = $kdata;
  ‣ obtain connection               }

  ‣ send requests                   function get_host($key) {
                                      global $servers;
For each server
  ‣ read responses                      $prefix = $key[0].$key[1].$key[2];
                                        $prefix = isset($servers[$prefix) ?
  ‣ deserialize non-scalar types                  $prefix : ‘wildcard’;

                                        $hash = crc32($key);
                                        $host = $servers[$hash %
                                                count($servers[$prefix])];
                                    }
Cache Multiget
Sort keys into buckets of servers   if (isset($cache_sock[$host])) {
                                      return $cache_sock[$host];
For each server                     }
  ‣ obtain connection
                                    list($ip, $port) = explode(‘:’, $host);
  ‣ send requests
                                    while($try < 5 && !$sock) {
For each server                       $sock = pfsockopen($ip, $port ...);
  ‣ read responses                    $try ++;
                                    }
  ‣ deserialize non-scalar types
                                    stream_set_write_buffer($sock, 0);

                                    $cmd = “get $keysrn”;
                                    fwrite($sock, $cmd);
Profile Multigets
Profile info
Profile installed platform applications
Viewer installed applications
Platform application data
List of friends
Privacy data
Cache Dispatching
cache_get_multi($ids, <key>, <miss>, $apc,
                $pending=false, &$pending_arr);

cache_dispatch();



Combine requests for data from the same memcache server
  ‣ Up to 10% performance improvement
Execute code while the kernel buffers the memcache response
Profile Multigets
                              <?php
<?php                         include_once(...);
include_once(...);
                              •get_profile(true);
parse_arguments();            •get_photos(true);
check_permissions();          •get_applications(true)
check_friends();
                              •get_friends(true)
check_friend_status();        •get_networks(true)
• get_profile();              cache_dispatch();
render_basic_information();
get_friend_details();         parse_arguments();
render_minifeed();            check_permissions();
render_wall();                check_friends();
• get_photos();               check_friend_status();
count_photos();               • get_profile();
• get_applications();         render_basic_information();
render_menu_actions();        get_friend_details();
• get_friends();              render_minifeed();
render_friends();             render_wall();
• get_networks();             get_photos();
render_networks();            count_photos();
render_applications();        get_applications();
                              render_menu_actions();
                              get_friends();
                              render_friends();
                              get_networks();
                              render_networks();
                              render_applications();
Lets make it even faster
Memcached PHP extension
  ‣ Reduced PHP function calling overhead
  ‣ Socket blocking in C instead of userspace PHP
  ‣ UDP support
Friends Again
Memcached extension runs ~ 10% faster realtime than in PHP userspace


                                             Userspace          131ms

                                             Extension          115ms
                                            Extension w/
                                                                122ms
                                                UDP
Serialization
Facebook internal serialization
  ‣ Profiles store 38% less memory in memcache
  ‣ Improves network throughput and realtime I/O




Compression
  ‣ Investigating LZO compression for even more space savings
UDP Memcached
TCP Limitations
  ‣ Maximum simultaneous connect count of ~ 250k
  ‣ Impedes scalability of memcached clusters


Requires a very stable network environment
Occasional misses are acceptible
Reduces kernel buffer memory usage on clients and servers
Supported in Facebook Memcache Extension coming soon
A Dirty Problem
Wrapper functions
   ‣ cache_dirty($id, $key);


Actively dirty cache entries as users change data
Dirty entries between multiple hosting facilities via proxy
Memcached Proxy
  0"1 2"34"3

56%$&"

                                       !.2=:



                       !"#$%$&"' ()
         7389.




                                          :8; ,%-"<$.




                         *%$+,+-. (/
Latency
Proxy deletes only work with low latency between facilities
When facilities are further apart deletes need to be smarter

    5"6 /"78"7

     92%$&"
                           !./0+



                                            !"#$%$&"' ()
                          +%,"-$.




                         !./0+
                         1"234$%


                                             !"#$%$&"' (*
Presention online
http://sizzo.org/talks/



lucas@facebook.com
4069180 Caching Performance Lessons From Facebook

More Related Content

What's hot

Bootstrapping multidc observability stack
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stackBram Vogelaar
 
Api Design
Api DesignApi Design
Api Designsartak
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalabilityWim Godden
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappersPositive Hack Days
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and DesktopElizabeth Smith
 
Observability with Consul Connect
Observability with Consul ConnectObservability with Consul Connect
Observability with Consul ConnectBram Vogelaar
 
Creating Reusable Puppet Profiles
Creating Reusable Puppet ProfilesCreating Reusable Puppet Profiles
Creating Reusable Puppet ProfilesBram Vogelaar
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonMasahiro Nagano
 
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...Puppet
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scriptingTony Fabeen
 
Puppet and the HashiStack
Puppet and the HashiStackPuppet and the HashiStack
Puppet and the HashiStackBram Vogelaar
 
Static Typing in Vault
Static Typing in VaultStatic Typing in Vault
Static Typing in VaultGlynnForrest
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareAlona Mekhovova
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteBram Vogelaar
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015Fernando Hamasaki de Amorim
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterZendCon
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010isnull
 

What's hot (20)

Bootstrapping multidc observability stack
Bootstrapping multidc observability stackBootstrapping multidc observability stack
Bootstrapping multidc observability stack
 
Api Design
Api DesignApi Design
Api Design
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 
Observability with Consul Connect
Observability with Consul ConnectObservability with Consul Connect
Observability with Consul Connect
 
Creating Reusable Puppet Profiles
Creating Reusable Puppet ProfilesCreating Reusable Puppet Profiles
Creating Reusable Puppet Profiles
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
 
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...
Puppet Camp Phoenix 2015: Managing Files via Puppet: Let Me Count The Ways (B...
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Puppet and the HashiStack
Puppet and the HashiStackPuppet and the HashiStack
Puppet and the HashiStack
 
Static Typing in Vault
Static Typing in VaultStatic Typing in Vault
Static Typing in Vault
 
Top Node.js Metrics to Watch
Top Node.js Metrics to WatchTop Node.js Metrics to Watch
Top Node.js Metrics to Watch
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
Integrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suiteIntegrating icinga2 and the HashiCorp suite
Integrating icinga2 and the HashiCorp suite
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 
PECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
 
Php on Windows
Php on WindowsPhp on Windows
Php on Windows
 

Similar to 4069180 Caching Performance Lessons From Facebook

Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
Burn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websitesBurn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websitesLindsay Holmwood
 
Service discovery and configuration provisioning
Service discovery and configuration provisioningService discovery and configuration provisioning
Service discovery and configuration provisioningSource Ministry
 
Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetAchieve Internet
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & ToolsIan Barber
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebMikel Torres Ugarte
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppSmartLogic
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: BackendVõ Duy Tuấn
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Tips
TipsTips
Tipsmclee
 
ELK: a log management framework
ELK: a log management frameworkELK: a log management framework
ELK: a log management frameworkGiovanni Bechis
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207patter
 

Similar to 4069180 Caching Performance Lessons From Facebook (20)

Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Burn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websitesBurn down the silos! Helping dev and ops gel on high availability websites
Burn down the silos! Helping dev and ops gel on high availability websites
 
Service discovery and configuration provisioning
Service discovery and configuration provisioningService discovery and configuration provisioning
Service discovery and configuration provisioning
 
Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and Puppet
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Memcached Study
Memcached StudyMemcached Study
Memcached Study
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo Web
 
EC2
EC2EC2
EC2
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Fatc
FatcFatc
Fatc
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
Heavy Web Optimization: Backend
Heavy Web Optimization: BackendHeavy Web Optimization: Backend
Heavy Web Optimization: Backend
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Tips
TipsTips
Tips
 
ELK: a log management framework
ELK: a log management frameworkELK: a log management framework
ELK: a log management framework
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207
 
Frontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and HowFrontend Servers and NGINX: What, Where and How
Frontend Servers and NGINX: What, Where and How
 

Recently uploaded

Plan d'orientations stratégiques rugby féminin
Plan d'orientations stratégiques rugby fémininPlan d'orientations stratégiques rugby féminin
Plan d'orientations stratégiques rugby fémininThibaut TATRY
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyApk Toly
 
Interpreting the Secrets of Milan Night Chart
Interpreting the Secrets of Milan Night ChartInterpreting the Secrets of Milan Night Chart
Interpreting the Secrets of Milan Night ChartChart Kalyan
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxEuro Cup 2024 Tickets
 
Presentation: The symbols of the Olympic Games
Presentation: The symbols of the Olympic  GamesPresentation: The symbols of the Olympic  Games
Presentation: The symbols of the Olympic Gamesluciavilafernandez
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeOptics-Trade
 
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebonyhf8803863
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.SJU Quizzers
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my InterestNagaissenValaydum
 
VIP Kolkata Call Girl Liluah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Liluah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Liluah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Liluah 👉 8250192130 Available With Roomdivyansh0kumar0
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeOptics-Trade
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeOptics-Trade
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Judith Chuquipul
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样7pn7zv3i
 
Technical Data | ThermTec Wild 325 | Optics Trade
Technical Data | ThermTec Wild 325 | Optics TradeTechnical Data | ThermTec Wild 325 | Optics Trade
Technical Data | ThermTec Wild 325 | Optics TradeOptics-Trade
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLAll American Billiards
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝soniya singh
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7dollysharma2066
 
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts servicevipmodelshub1
 

Recently uploaded (20)

Plan d'orientations stratégiques rugby féminin
Plan d'orientations stratégiques rugby fémininPlan d'orientations stratégiques rugby féminin
Plan d'orientations stratégiques rugby féminin
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
 
Interpreting the Secrets of Milan Night Chart
Interpreting the Secrets of Milan Night ChartInterpreting the Secrets of Milan Night Chart
Interpreting the Secrets of Milan Night Chart
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
 
Presentation: The symbols of the Olympic Games
Presentation: The symbols of the Olympic  GamesPresentation: The symbols of the Olympic  Games
Presentation: The symbols of the Olympic Games
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics Trade
 
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interest
 
VIP Kolkata Call Girl Liluah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Liluah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Liluah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Liluah 👉 8250192130 Available With Room
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics Trade
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
 
Technical Data | ThermTec Wild 325 | Optics Trade
Technical Data | ThermTec Wild 325 | Optics TradeTechnical Data | ThermTec Wild 325 | Optics Trade
Technical Data | ThermTec Wild 325 | Optics Trade
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FL
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
 
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts serviceChennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
Chennai Call Girls Anna Nagar Phone 🍆 8250192130 👅 celebrity escorts service
 
Stunning ➥8448380779▻ Call Girls In Delhi Cantt Delhi NCR
Stunning ➥8448380779▻ Call Girls In Delhi Cantt Delhi NCRStunning ➥8448380779▻ Call Girls In Delhi Cantt Delhi NCR
Stunning ➥8448380779▻ Call Girls In Delhi Cantt Delhi NCR
 

4069180 Caching Performance Lessons From Facebook

  • 1. Caching & Performance: Lessons from Facebook Lucas Nealan OSCon, Portland OR July 23, 2008, 14:35 – 15:20
  • 2. Facebook Social Utility Sharing and communicating efficiently 4th most trafficked site in the World * * ComScore 2008
  • 3. Facebook Stats Over 90 million active users ~ 50 pages per user daily Over 24,000 platform applications
  • 4. Facebook Architecture !5678 -**. /012", (") *+", 34%#%+ !"#$%$&"'
  • 5. Complexity Connecting to all Database is impossible Large codebase Scaling affects resources in many ways ‣ Memory consumption ‣ Socket connection limits Cache retrieval is ~ 10% cpu-user of most pages
  • 6. What are the Benefits of Caching?
  • 7. Caching Layers $GLOBALS APC Memcached Database Browser Cache Third Party CDN
  • 8. Globals Cache function cache_get($id, $key, $apc=false) { if (isset($GLOBALS['CACHE']["$key:$id"])) { $cache = $GLOBALS['CACHE']["$key:$id"])); $hit = 1; } elseif ($apc && (($cache = apc_fetch("$key:$id")) !== false) { $hit = 1; } else { ... // fetch from memcached if ($apc) apc_store("$key:$id", $cache); } if ($hit) $GLOBALS['CACHE']["$key:$id"] = $cache; return $hit ? $cache : NULL; }
  • 9. Globals Cache Avoids unnecessary APC and Memcached requests Automatic via abstraction But still has function call cost overhead foreach($ids as $id) { if (isset($GLOBALS['CACHE']["profile:$id")) { $profile = $GLOBALS['CACHE']["profile:$id"]; } else { $profile = cache_get($id, 'profile'); } }
  • 10. APC Opcode caching ‣ Hundreds of included libraries ‣ Thousands of functions Variable caching ‣ Hundreds of MB’s of data
  • 11. APC User Cache Non-user specific data ‣ Network/School information ‣ Database information ‣ Useragent strings ‣ Hot application data ‣ Site variables ‣ Languange Strings
  • 12. Friends Normal 4050ms APC 135ms apc.stat=0 128ms
  • 13. APC Opcode Priming $path = '/path/to/source'; $exclude = $path.'/www/admin/'; $expr = '.*.php'; $files = split(' ', exec("find -L $path -regex '$expr' | grep -v '$exclude' | xargs")); // prime php files foreach($files as $file) { apc_compile_file($file); }
  • 14. APC+SVN Client Cache Busting function get_static_suffix($file) { global $ROOT; if ($version = cache_get($file, ‘sv’, 1) === null) { $version = trim(shell_exec(“svn info $ROOT/$file | grep ‘Changed Rev’ | cut -c 19-”)); apc_store(“sv:$file”, $version); } return '?'.$version; }
  • 15. APC + Useragent Strings Useragent string parsing is inefficient in PHP Cache parsed useragents in APC for the first 10 minutes Hit rate of over 50% Pear implementation available: ‣ PEAR::Net_Useragent_Detect_APC
  • 16. Site Variables Enable/Disable site features across all servers Configure memcached cluster IP’s Configure product features Version memcached keys for invalidation
  • 17. Site Variables 9+;$6<-87-8 !"# -.(&91<67686 6'"5$'7686 -./01$23 -.9&98$7#:# -./01$23 -./.$'-"+, -./.$'-"+,4#"5$ -./.$'-"+, -.<6;&#$7686 =$59&98$; *+;$ B<-8 !$'.$' =2!>? -.(&91<67686 !"#$.&' @A !"#$ %&'"&()$ *+,-+)$
  • 18. Memcached Distributed object cache Facebook currently utilizes > 400 memcached hosts With > 5TB in memory cache Facebook contributions: ‣ UDP Support ‣ Performance Enhancements Many choices in opensource clients
  • 19. What to cache? User Specific Data ‣ Long profile ‣ Short profile ‣ Friends ‣ Applications Key versioning ‣ sp:6:10030226
  • 20. Cache Retreival Create Wrapper functions: ‣ cache_get($id, <key>, <miss>, $apc, $timeout); ‣ cache_get_multi($ids, <key>, <miss>, $apc); Cache key callback function: function profile_key($id) { global $VERSION_SP; // primed site variable return “sp:$VERSION_SP:$id”; }
  • 21. Cache Multiget Sort keys into buckets of servers foreach($keys as $key => $kdata) { $host = get_host($key); // hash For each server $keys_hosts[$host][$key] = $kdata; ‣ obtain connection } ‣ send requests function get_host($key) { global $servers; For each server ‣ read responses $prefix = $key[0].$key[1].$key[2]; $prefix = isset($servers[$prefix) ? ‣ deserialize non-scalar types $prefix : ‘wildcard’; $hash = crc32($key); $host = $servers[$hash % count($servers[$prefix])]; }
  • 22. Cache Multiget Sort keys into buckets of servers if (isset($cache_sock[$host])) { return $cache_sock[$host]; For each server } ‣ obtain connection list($ip, $port) = explode(‘:’, $host); ‣ send requests while($try < 5 && !$sock) { For each server $sock = pfsockopen($ip, $port ...); ‣ read responses $try ++; } ‣ deserialize non-scalar types stream_set_write_buffer($sock, 0); $cmd = “get $keysrn”; fwrite($sock, $cmd);
  • 23. Profile Multigets Profile info Profile installed platform applications Viewer installed applications Platform application data List of friends Privacy data
  • 24. Cache Dispatching cache_get_multi($ids, <key>, <miss>, $apc, $pending=false, &$pending_arr); cache_dispatch(); Combine requests for data from the same memcache server ‣ Up to 10% performance improvement Execute code while the kernel buffers the memcache response
  • 25. Profile Multigets <?php <?php include_once(...); include_once(...); •get_profile(true); parse_arguments(); •get_photos(true); check_permissions(); •get_applications(true) check_friends(); •get_friends(true) check_friend_status(); •get_networks(true) • get_profile(); cache_dispatch(); render_basic_information(); get_friend_details(); parse_arguments(); render_minifeed(); check_permissions(); render_wall(); check_friends(); • get_photos(); check_friend_status(); count_photos(); • get_profile(); • get_applications(); render_basic_information(); render_menu_actions(); get_friend_details(); • get_friends(); render_minifeed(); render_friends(); render_wall(); • get_networks(); get_photos(); render_networks(); count_photos(); render_applications(); get_applications(); render_menu_actions(); get_friends(); render_friends(); get_networks(); render_networks(); render_applications();
  • 26. Lets make it even faster Memcached PHP extension ‣ Reduced PHP function calling overhead ‣ Socket blocking in C instead of userspace PHP ‣ UDP support
  • 27. Friends Again Memcached extension runs ~ 10% faster realtime than in PHP userspace Userspace 131ms Extension 115ms Extension w/ 122ms UDP
  • 28. Serialization Facebook internal serialization ‣ Profiles store 38% less memory in memcache ‣ Improves network throughput and realtime I/O Compression ‣ Investigating LZO compression for even more space savings
  • 29. UDP Memcached TCP Limitations ‣ Maximum simultaneous connect count of ~ 250k ‣ Impedes scalability of memcached clusters Requires a very stable network environment Occasional misses are acceptible Reduces kernel buffer memory usage on clients and servers Supported in Facebook Memcache Extension coming soon
  • 30. A Dirty Problem Wrapper functions ‣ cache_dirty($id, $key); Actively dirty cache entries as users change data Dirty entries between multiple hosting facilities via proxy
  • 31. Memcached Proxy 0"1 2"34"3 56%$&" !.2=: !"#$%$&"' () 7389. :8; ,%-"<$. *%$+,+-. (/
  • 32. Latency Proxy deletes only work with low latency between facilities When facilities are further apart deletes need to be smarter 5"6 /"78"7 92%$&" !./0+ !"#$%$&"' () +%,"-$. !./0+ 1"234$% !"#$%$&"' (*