Effectice Caching w/ PHP + Caching

12,403 views

Published on

Scaling your Database or other resources your website needs can be
complicated. Why not reduce their load instead? That is where memcached can
help. Developed by the creators of LiveJournal, memcached is a light weight
application that does one thing extremely well: caching data in memory and
retrieving it very fast.

This presentation will cover the basics of memcached and then explain some
caching theory and best practices for using memcache. Topics to be covered
at:

- What is Memcached?
- How can it help my website?
- Installing
- Configuring & Running
- Basic Workflow
- Best Practices w/ Keys
- What to Cache
- When to Cache
- Expiring Data
- Wrapping Caches with a Class
- Security
- Question & Answer Session

Justin Carmony has worked in web development professionally for the last
five years. With emphasis on PHP, .NET, and Web Services, he has worked on
projects ranging from simple websites to complex communications between
thousands of remote systems. He currently is working as an independent
contractor & private consultant. If you have any questions, comments, or
curiosities you can contact him (justin AT justincarmony DOT com) or read
about his latest endeavors at his blog (www.justincarmony.com/blog).

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

No Downloads
Views
Total views
12,403
On SlideShare
0
From Embeds
0
Number of Embeds
2,147
Actions
Shares
0
Downloads
215
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide
  • Effectice Caching w/ PHP + Caching

    1. 1. EFFECTIVE CACHING WITH PHP & MEMCACHED by Justin Carmony
    2. 2. ABOUT ME • Full Time Web Developer & Consultant • Worked with LAMP & .NET Solutions • Blog - http://www.justincarmony.com/blog/ • Current Projects:
    3. 3. ABOUT PRESENTATION On-Topic Questions During Presentation Q & A Session After Presentation Feel Free to Email Me Questions: justin@justincarmony.com
    4. 4. WHAT IS MEMCACHED?
    5. 5. ABOUT MEMCACHED
    6. 6. ABOUT MEMCACHED Developed by LiveJournal’s Developers
    7. 7. ABOUT MEMCACHED Developed by LiveJournal’s Developers Primary Function: Relieve Database Load (Mainly Reads)
    8. 8. ABOUT MEMCACHED Developed by LiveJournal’s Developers Primary Function: Relieve Database Load (Mainly Reads) Can Scale Across Multiple Servers
    9. 9. ABOUT MEMCACHED Developed by LiveJournal’s Developers Primary Function: Relieve Database Load (Mainly Reads) Can Scale Across Multiple Servers Giant “Hash Table” or “Array”
    10. 10. ABOUT MEMCACHED Developed by LiveJournal’s Developers Primary Function: Relieve Database Load (Mainly Reads) Can Scale Across Multiple Servers Giant “Hash Table” or “Array” Stores Data in Memory - Extremely Fast
    11. 11. WHY WE CACHE
    12. 12. CONCEPT OF CACHING
    13. 13. CONCEPT OF CACHING • Resource that is Struggling Website Resource Application
    14. 14. CONCEPT OF CACHING • Resource that is Struggling Website • Introduce Cache to Help Resource Application Cache
    15. 15. CONCEPT OF CACHING • Resource that is Struggling Website • Introduce Cache to Help Resource Application • Store “Reliable” Data in Cache Cache
    16. 16. CONCEPT OF CACHING • Resource that is Struggling Website • Introduce Cache to Help Resource Application • Store “Reliable” Data in Cache • Check Cache Before Hitting Resource Cache
    17. 17. CONCEPT OF CACHING • Resource that is Struggling Website • Introduce Cache to Help Resource Application • Store “Reliable” Data in Cache • Check Cache Before Hitting Resource Cache • Resource Types: DB, Web Service, XML, Files, Etc.
    18. 18. THE DATABASE
    19. 19. THE DATABASE • Scaling is Complicated
    20. 20. THE DATABASE • Scaling is Complicated • Its because what makes databases great: ACID.
    21. 21. THE DATABASE • Scaling is Complicated • Its because what makes databases great: ACID. • What about SQL’s Internal Caching?
    22. 22. THE DATABASE • Scaling is Complicated • Its because what makes databases great: ACID. • What about SQL’s Internal Caching? • Less Load on DB is Good
    23. 23. HOW DOES IT IMPROVE PERFORMANCE?
    24. 24. DATING DNA PERFORMANCE BOOST Before After Daily New Registrations 100 500 HTTP Requests Per Second ~ 36 r/s ~150 r/s AVG Database CPU ~ 73% ~32% # Queries Per Second ~ 2,200 ~1,700 AVG Page Load Time 2 - 5 seconds 0.01 - 0.4 seconds * Note - Several Performance Changes Made, Not 100% Due to Memcached
    25. 25. LIVE MEMCACHED STATS
    26. 26. INSTALLING & RUNNING MEMCACHED
    27. 27. INSTALLING • Linux - Aptitude, Yum, RPM, Compile from Source, etc... • Mac OS X - Compile Only • Windows - Downloadable Binaries • Its pretty simple!
    28. 28. RUNNING • No Configuration Files • Just a few command line options: memcached -d -m 2048 -l 127.0.0.1 -p 11211
    29. 29. BASIC CACHE FLOW
    30. 30. CONNECT <?php // Create New Memcache Instance $memcache = new Memcache(); // Add Memcache Server to Connection Pool $memcache->addServer('127.0.0.1', 11211); // ... continue code ...
    31. 31. GET & SET DATA // Try to Get the $my_name = $memcache->get('my_name'); if($my_name === false) { $my_name = 'Justin Carmony'; $memcache->set('my_name', $my_name, false, 60); } echo $my_name; // output: Justin Carmony
    32. 32. “DELETING” DATA // Lets say we don't like Justin anymore... $memcache->delete('my_name'); $value = $memcache->get('my_name'); // returns false
    33. 33. CACHING THEORY ( WHERE, WHAT, WHEN, & HOW)
    34. 34. WHERE TO CACHE
    35. 35. IDENTIFYING WHAT TO CACHE
    36. 36. IDENTIFYING WHAT TO CACHE ‣ High Demand
    37. 37. IDENTIFYING WHAT TO CACHE ‣ High Demand ‣ Expensive
    38. 38. IDENTIFYING WHAT TO CACHE ‣ High Demand ‣ Expensive ‣ Large in Size
    39. 39. IDENTIFYING WHAT TO CACHE ‣ High Demand ‣ Expensive ‣ Large in Size ‣ Common
    40. 40. IDENTIFYING WHAT TO CACHE ‣ High Demand ‣ Expensive ‣ Large in Size ‣ Common The Best Cacheable Data with Highest Performance ROI Fits Multiple Characteristics
    41. 41. TIPS FOR FINDING CACHEABLE DATA
    42. 42. TIPS FOR FINDING CACHEABLE DATA ‣ Monitor Queries
    43. 43. TIPS FOR FINDING CACHEABLE DATA ‣ Monitor Queries ‣ Output Queries
    44. 44. TIPS FOR FINDING CACHEABLE DATA ‣ Monitor Queries ‣ Output Queries ‣ Monitor Page Loads
    45. 45. TIPS FOR FINDING CACHEABLE DATA ‣ Monitor Queries ‣ Output Queries ‣ Monitor Page Loads ‣ Monitor Web Analytics
    46. 46. TIPS FOR FINDING CACHEABLE DATA ‣ Monitor Queries ‣ Output Queries ‣ Monitor Page Loads ‣ Monitor Web Analytics ‣ Brainstorming
    47. 47. WHAT TO CACHE
    48. 48. OUTPUT CACHING Capture Output Buffer When to Use Output Caching: - Page Has High Load - Expensive to Generate - Easier to Cache over Data
    49. 49. OUTPUT CACHING Capture Output Buffer When to Use Output Caching: - Page Has High Load - Expensive to Generate - Easier to Cache over Data
    50. 50. OUTPUT BUFFER EXAMPLE // Output Buffer Cache $output = $memcache->get('my_html'); if(!$output) { ob_start(); // Start Capture ?> <div id="main">Hello World!</div> <!-- Insert Some Crazy HTML Code --> <?php $output = ob_get_contents(); ob_end_clean(); $memcache->set('my_html', $output, false, 15 * 60); } echo $output;
    51. 51. DATA CACHING Create a Class to Cache Use Factory Methods to Retrieve Use When Use: - Data Used Frequently - Displayed Different - Easier from Cache than other Sources
    52. 52. WHEN TO EXPIRE
    53. 53. TIMEOUT EXPIRATION Settings a Lower Timeout Great for Summaries Okay to be Kinda Out of Date “Quick & Dirty” Optimizations Output + Timeout Common
    54. 54. EVENT EXPIRATION Data is Infrequently Updated Data is Time Critical - Cannot Be Out of Date Data + Event Common
    55. 55. HOW TO CACHE
    56. 56. IT’S ALL ABOUT YOUR KEYS Keys MUST Be Unique Prefix Cache Types (i.e. prflinfo_ for ProfileInfo) SHA256 / MD5 Identifiers Example: ProfileInfo for User ID 1337: prflinfo_e48e13207341b6bffb7fb1622282247b
    57. 57. WRAP YOUR CACHE W/ CLASS Pick a Cacheable Piece of Information Create a Class For That Cache Create Methods For: ‣ Generate Key Name ‣ Get & Set ‣ Destroy / Delete
    58. 58. class ProfileInformation { public $user_id = null; public $first_name = null; public $email_address = null; public $profile_img = null; public $city = null; public $state = null; public $age = null; private function __construct($user_id) { $this->user_id = $user_id; $sql = "SELECT * FROM users WHERE user_id = '$user_id'"; $p_data = DBI::fetchObject($sql); $this->first_name = $p_data->first_name; $this->email_address = $p_data->email_address; $this->age = xDate::GetAge($p_data->birthdate); // .. Set More Information .. } // .. more .. }
    59. 59. class ProfileInformation { // .. more .. static public function GetProfile($user_id) { // Try to get it from the cache $profile = null; $profile = self::GetCache($user_id); if(!$profile) { $profile = new ProfileInformation($user_id); self::SetCache($user_id, $profile); } return $profile; } // ... more ... }
    60. 60. class ProfileInformation { // .. more .. static protected function GetCacheName($user_id) { return 'profile_'.hash('sha256',"user_".$user_id."_".$user_id); } // ... more ... }
    61. 61. class ProfileInformation { // .. more .. static protected function GetCache($user_id) { $name = self::GetCacheName($user_id); $profile = null; $memcache = MemcacheCluster::GetInstance(); $profile = $memcache->get($name); if(!$profile) { return null; } return $profile; } // ... more ... }
    62. 62. class ProfileInformation { // .. more .. static protected function SetCache($user_id, $data) { $name = self::GetCacheName($user_id); $memcache = MemcacheCluster::GetInstance(); $profile = $memcache->set($name, $data, false, 24 * 60 * 60); } public static function DestroyCache($user_id) { $name = self::GetCacheName($user_id); $memcache = MemcacheCluster::GetInstance(); $profile = $memcache->delete($name); } // End Of Class }
    63. 63. require_once 'ProfileInformation.class.php'; $user_id = 1337; // Incorrect Method $profile = new ProfileInformation($user_id); // Throws Error // Correct Method $profile = ProfileInformation::GetProfile($user_id); echo $profile->first_name;
    64. 64. FINAL THOUGHTS
    65. 65. COMMON PITFALLS • Caching Only Optimization - Ignoring Other Inefficiencies • Cache Offline == Catastrophic Failure • Poor Key Choices • Inability to easily invalidate specific Cache Entries • Caching SQL Queries
    66. 66. PITFALL: QUERY CACHE <?php $sql = "SELECT * FROM users WHERE id = 5"; $result = $mem->get(md5($sql)); if(!$result) { // Execute and get the result of the query in an array $result = Database::FetchArray($sql); // set the timeout to 4 hours $mem->set(md5($sql), $result, 0, 4 * 60 * 60); } ?>
    67. 67. SECURITY No Real Security for Memcached --listen Not Super Secure / Configurable Limit Access at Network Level IPTables / Firewalls / Internal Network
    68. 68. MISCELLANEOUS THINGS Memcache Library vs Memcached Library Where can I find out More? - My Blog: Search Memcached - Google Memcached - PHP Documentation
    69. 69. QUESTIONS?
    70. 70. SHAMELESS PLUG: October 8-10, 2009 | www.utosc.com
    71. 71. THANK YOU Website www.justincarmony.com Email justin@justincarmony.com Twitter JustinCarmony Skype JustinCarmony IRC irc.freenode.net #uphpu

    ×