Saturday, December 13, 2008

Caching & Performance: Lessons from Facebook

Facebook is 4th most trafficked site in the World. it has Over 90 million active users and Over 24,000 platform applications.

Caching Layers

  2. APC
  3. Memcached
  4. Database
  5. Browser Cache
  6. Third Party CDN
Globals Cache

  1. Avoids unnecessary APC and Memcached requests
  2. Automatic via abstraction
  3. But still has function call cost overhead


Opcode caching
‣ Hundreds of included libraries
‣ Thousands of functions
Variable caching
‣ Hundreds of MB’s of data

APC Cache

Non-user specific data
‣ Network/School information
‣ Database information
‣ Useragent strings
‣ Hot application data
‣ Site variables

APC Opcode Priming

APC+SVN Client Cache Busting

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


  • 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
  • Memcached PHP extension
‣ Reduced PHP function calling overhead
‣ Socket blocking in C instead of userspace PHP
‣ UDP support

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

No comments:

Post a Comment