Cache That!
Gopal Vijayaraghavan
Yahoo Inc.
gopalv@php.net

                       July 26th, 2007
Cache ? Why ?
- Easiest optimisation you could do
  - Maximum return on work invested
- Hopefully transparent
- Probably d...
Sandwich Theory




http://flickr.com/photos/ozute/87465787/
Sandwich Theory
- Pages are dynamic
- Like a sandwich made to order.
- The whole page is built to order, but bits of
  con...
“fast”: latency & throughput




http://flickr.com/photos/brtsergio/184026033/
Cache: strategies and levels
- Browser land
- Content proxying
- Pre-generating content
- Active data caching
- Backend ca...
So, what's this talk about ?




http://flickr.com/photos/frogmuseum2/238601344
Common Sense: Repeated
- Browser caches ?
  - DNS, Expires:, Cache-Control:
- Squid reverse proxies ?
  - Ad-hoc semi-stat...
Active Data Caching
- We're actually in PHP land now
- Two main cache tools
  - APC
  - Memcache
- APC is php specific
- M...
Choices




http://flickr.com/photos/elsie/347928333/
Red or Blue ?
- APC                    - Memcached
 - Single server          - Distributed
 - Stores arrays as-is    - nee...
APC: Opcode Cache
- Drop-in & use
- Prevents disk access & syscalls
- Is really old Hat, now
- Tuning & tweaking
  - nosta...
Tuning APC – check list
- You need to tweak APC if you've got
  - a large number of files (> 1024)
  - a large cache footp...
APC Tuneables
- apc.ini
  - apc.shm_size (64 M)
  - apc.num_files_hint (512)
  - apc.stat (On)
  - apc.stat_ctime (Off)
  ...
APC monitoring
- Monitor APC using the apc.php in the pkg
- Provides basic information about
  - memory usage
  - fragment...
Tune your Code - #I
- Reduce include/include_once calls
  - Template & stitch
  - R3 (http://rthree.sf.net/)
- Use full in...
Inclued
Inclued: Smarty
APC: var_export gotchas
- PHP opcodes are cached in APC
- So, putting a constant array on a file seems OK
- But here's the...
APC: Dynamic Style
- apc_fetch()/apc_store()
- The interface is simple enough
   if(!($data = apc_fetch('data'))) {
     $...
Lies, Damned Lies & Statistics




http://flickr.com/photos/matthijs/82616861/
APC vs Files
                                                           Writes     Reads
     file + includes             ...
APC vs Files
- Serialized arrays are the fastest to write to
- APC is the slowest to write to, but the fastest to
  read f...
Tune your Code - II
- Use APC user cache
  - WPcache2, smarty_cache_apc
- Re-order your nested lookups
  - NO: $a = apc_fe...
Anatomy of a Cache Slam
What works (or has worked)
- Give it more (virtual) memory to burn
- Use APC user cache for infrequently updated
  data
- ...
Thank you for listening
- Resources
  - http://pecl.php.net/package/APC
  - http://pecl.php.net/package/memcache
- Slides
...
Upcoming SlideShare
Loading in …5
×

Os Gopal

2,551 views
2,478 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,551
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Os Gopal

  1. 1. Cache That! Gopal Vijayaraghavan Yahoo Inc. gopalv@php.net July 26th, 2007
  2. 2. Cache ? Why ? - Easiest optimisation you could do - Maximum return on work invested - Hopefully transparent - Probably doesn't need a complete redesign - especially of business logic - Tradeoff between speed and accuracy - 80+% of Web content is not mission critical - scale is more important
  3. 3. Sandwich Theory http://flickr.com/photos/ozute/87465787/
  4. 4. Sandwich Theory - Pages are dynamic - Like a sandwich made to order. - The whole page is built to order, but bits of content require varying “freshness” - A jar of pre-cut olives are the next best thing after Sliced Bread
  5. 5. “fast”: latency & throughput http://flickr.com/photos/brtsergio/184026033/
  6. 6. Cache: strategies and levels - Browser land - Content proxying - Pre-generating content - Active data caching - Backend caches
  7. 7. So, what's this talk about ? http://flickr.com/photos/frogmuseum2/238601344
  8. 8. Common Sense: Repeated - Browser caches ? - DNS, Expires:, Cache-Control: - Squid reverse proxies ? - Ad-hoc semi-static content - Content pre-gen ? - RSS feeds & other periodically updated content - Backend caches ? - DB query caches, disk/OS caches
  9. 9. Active Data Caching - We're actually in PHP land now - Two main cache tools - APC - Memcache - APC is php specific - Memcached is more generic and distributed
  10. 10. Choices http://flickr.com/photos/elsie/347928333/
  11. 11. Red or Blue ? - APC - Memcached - Single server - Distributed - Stores arrays as-is - needs serialization - small data - large data safe - data churn causes - handles data churn issues better - Does opcodes too - Only data
  12. 12. APC: Opcode Cache - Drop-in & use - Prevents disk access & syscalls - Is really old Hat, now - Tuning & tweaking - nostat mode - include_once_override - locking modes
  13. 13. Tuning APC – check list - You need to tweak APC if you've got - a large number of files (> 1024) - a large cache footprint (> 64 M) - a mess of include_onces - a highly OOP design - very static php files - rsync and inode re-use
  14. 14. APC Tuneables - apc.ini - apc.shm_size (64 M) - apc.num_files_hint (512) - apc.stat (On) - apc.stat_ctime (Off) - apc.include_once_override (Off) - apc.filters () - apc.localcache (Off)
  15. 15. APC monitoring - Monitor APC using the apc.php in the pkg - Provides basic information about - memory usage - fragmentation - cache request/insert rates - user and file - expunge/cache-full count
  16. 16. Tune your Code - #I - Reduce include/include_once calls - Template & stitch - R3 (http://rthree.sf.net/) - Use full include paths - include(“./z.php); instead of include(“z.php”); - Use constant includes - don't wrap include with your functions - Avoid eval() at all costs
  17. 17. Inclued
  18. 18. Inclued: Smarty
  19. 19. APC: var_export gotchas - PHP opcodes are cached in APC - So, putting a constant array on a file seems OK - But here's the “constant” array in PHP line # op ext operands ------------------------------------------------------- 2 0 INIT_ARRAY ~0, 'z', 'x' 3 1 ADD_ARRAY_ELEMENT ~0, 'b', 'a' 4 2 ADD_ARRAY_ELEMENT ~0, 'c', 'b' 5 3 ADD_ARRAY_ELEMENT ~0, 'd', 'c' 4 ASSIGN !0, ~0 7 5 RETURN 1 6 ZEND_HANDLE_EXCEPTION
  20. 20. APC: Dynamic Style - apc_fetch()/apc_store() - The interface is simple enough if(!($data = apc_fetch('data'))) { $data = array( .... ); apc_store('data', $data); } - But with a Time To Live (TTL) setting, data expires
  21. 21. Lies, Damned Lies & Statistics http://flickr.com/photos/matthijs/82616861/
  22. 22. APC vs Files Writes Reads file + includes 17809 1856348.5 file + serialized arrays 13804 517380.2 apc 98586 359790.3 Read/Write performance * Less time means better Includes Read Write Serialized APC 0.00 500000.00 1000000.00 1500000.00 2000000.00 http://pooteeweet.org/blog/721
  23. 23. APC vs Files - Serialized arrays are the fastest to write to - APC is the slowest to write to, but the fastest to read from - Disk files with arrays lose out on both fronts - But then why would you ever use files ? - Because they expire gracefully, that's why - So, what happens when a cache entry expires ?
  24. 24. Tune your Code - II - Use APC user cache - WPcache2, smarty_cache_apc - Re-order your nested lookups - NO: $a = apc_fetch(“foo”); echo $a[“baz”]; - YES: $a = apc_fetch(“foo.baz”); echo $a; - Avoid data race conditions - shouldn't I fix the race conditions ?
  25. 25. Anatomy of a Cache Slam
  26. 26. What works (or has worked) - Give it more (virtual) memory to burn - Use APC user cache for infrequently updated data - Update your APC cache out of band - Cron & Curl are APC's friends - Always check your APC info for - cache full count - fragmentation
  27. 27. Thank you for listening - Resources - http://pecl.php.net/package/APC - http://pecl.php.net/package/memcache - Slides - http://t3.dotgnu.info/slides/oscon07.pdf - My Blog - http://t3.dotgnu.info/blog/php/

×