Your SlideShare is downloading. ×
0
Scaling with Memcached http://danga.com/memcached By Abhinav Singh
Who all are using it?
History of Memcached <ul><li>Brad Fitzpatrick from  Danga Interactive  developed memcached to enhance speed of  livejourna...
Three W’s <ul><li>W hat is Memcached? </li></ul><ul><li>Memcached is a high-performance, distributed memory object caching...
Pseudo Implementation PHP DB API Database 1 st  time Query Checks cache and if not found extracts from database Cache the ...
Pseudo Implementation PHP DB API Database 2 nd  time Query Checks cache, resultset already found. Un-serialize and return ...
Pseudo Code <ul><li>$listofphotos = $mdb->getPhotos(); </li></ul><ul><li>$query = “SELECT * from photos where …..”; </li><...
Miscellaneous <ul><li>The story of memcache </li></ul><ul><li>MySQL Query Cache v/s APC v/s Memcache. Why memcache? </li><...
Facebook – How do they do? <ul><li>Me:  Hey how do you use memcache at facebook? </li></ul><ul><li>My friend  -  “A bit of...
Cross Datacenter Story <ul><li>My Friend from Facebook:  Consider this example: </li></ul><ul><li>1. I update my first nam...
Storing and Managing Collections <ul><li>keyArray = GET array of keys from cache IF keyArray NOT NULL   lstItems = MULTI G...
Why not use memcache? <ul><li>You have objects larger than 1MB.  </li></ul><ul><li>You have keys larger than 250 chars.  <...
<ul><li>Questions? </li></ul>
<ul><li>Thank You </li></ul>
Upcoming SlideShare
Loading in...5
×

Memcache

23,902

Published on

Memcache is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Published in: Education, Technology
2 Comments
10 Likes
Statistics
Notes
No Downloads
Views
Total Views
23,902
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
274
Comments
2
Likes
10
Embeds 0
No embeds

No notes for slide

Transcript of "Memcache"

  1. 1. Scaling with Memcached http://danga.com/memcached By Abhinav Singh
  2. 2. Who all are using it?
  3. 3. History of Memcached <ul><li>Brad Fitzpatrick from Danga Interactive developed memcached to enhance speed of livejournal.com , which was then doing 20 million+ dynamic PV’s per day. </li></ul><ul><li>Memcached reduced the database load to almost nothing, yielding faster page load time and better resource utilization. </li></ul><ul><li>Facebook is the biggest user of memcached after live journal. They have > 100 dedicated memcached servers. </li></ul>
  4. 4. Three W’s <ul><li>W hat is Memcached? </li></ul><ul><li>Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. </li></ul><ul><li>W hen can we use it? </li></ul><ul><li>Anywhere, if you have spare RAM </li></ul><ul><li>Mostly used in wiki, social networking and book marking sites. </li></ul><ul><li>W hy should we use it? </li></ul><ul><li>If you have a high-traffic site that is dynamically generated with a high database load that contains mostly read threads then memcached can help lighten the load on your database. </li></ul>
  5. 5. Pseudo Implementation PHP DB API Database 1 st time Query Checks cache and if not found extracts from database Cache the serialized resultset in memory 2Gb RAM reserved for caching the resultset http://localhost
  6. 6. Pseudo Implementation PHP DB API Database 2 nd time Query Checks cache, resultset already found. Un-serialize and return the resultset 2Gb RAM reserved for caching the resultset http://localhost
  7. 7. Pseudo Code <ul><li>$listofphotos = $mdb->getPhotos(); </li></ul><ul><li>$query = “SELECT * from photos where …..”; </li></ul><ul><li>$key = sha1($query) </li></ul><ul><li>If ($key FOUND IN memcache) { </li></ul><ul><li>$value = get($key); </li></ul><ul><li> $resultset = unserialize($value); </li></ul><ul><li>return $resultset; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>$resultset = mysql_query($query); </li></ul><ul><li>set($key,serialize($resultset),$TTL); </li></ul><ul><li>return $resultset; </li></ul><ul><li>} </li></ul><ul><li>PS: Do not use SQL_FOUND_ROWS in your $query while using memcache </li></ul>
  8. 8. Miscellaneous <ul><li>The story of memcache </li></ul><ul><li>MySQL Query Cache v/s APC v/s Memcache. Why memcache? </li></ul><ul><li>Deciding your key intelligently (User:md5($query)) or (Messages:sha1($query)). </li></ul><ul><li>A bit on how to apply versioning in keys. </li></ul><ul><li>Is memcache secure? No, then what precautions do I take? </li></ul><ul><li>Reduce load on your database by not updating it for trivial information like timestamp, last changed date etc. </li></ul>
  9. 9. Facebook – How do they do? <ul><li>Me: Hey how do you use memcache at facebook? </li></ul><ul><li>My friend - “A bit of background on our caching model: when a user modifies a data object our infrastructure will write the new value in to a database and delete the old value from memcache (if it was present). The next time a user requests that data object we pull the result from the database and write it to memcache. Subsequent requests will pull the data from memcache until it expires out of the cache or is deleted by another update. “ </li></ul><ul><li>Me: But does this work fine with datacenters spread around different countries? </li></ul>
  10. 10. Cross Datacenter Story <ul><li>My Friend from Facebook: Consider this example: </li></ul><ul><li>1. I update my first name from “ Abhinav &quot; to &quot; Monkey &quot; </li></ul><ul><li>2. We write &quot; Monkey &quot; in to the master database in California and delete my first name from memcache in California and Virginia </li></ul><ul><li>3. Someone goes to my profile in Virginia </li></ul><ul><li>4. We don't find my first name in memcache so we read from the Virginia slave database and get “ Abhinav &quot; because of replication lag </li></ul><ul><li>5. We update Virginia memcache with my first name as “ Abhinav &quot; </li></ul><ul><li>6. Replication catches up and we update the slave database with my first name as “ Monkey ” </li></ul><ul><li>7. Someone else goes to my profile in Virginia </li></ul><ul><li>8. We find my first name in memcache and returns “ Abhinav ” </li></ul><ul><li>9. Hence my first name will be shown as “Abhinav” in Virginia and “Monkey” in California. Confusing? </li></ul>
  11. 11. Storing and Managing Collections <ul><li>keyArray = GET array of keys from cache IF keyArray NOT NULL   lstItems = MULTI GET keyArray   FOR EACH item IN lstItems      IF item IS NULL         SET item in cache      END IF   END FOR ELSE   lstItems = list of items from persistent data store   FOR EACH item IN lstItems      ADD key for item INTO keyArray      STORE item in cache   END FOR   STORE keyArray in cache END IF RETURN lstItems </li></ul>
  12. 12. Why not use memcache? <ul><li>You have objects larger than 1MB. </li></ul><ul><li>You have keys larger than 250 chars. </li></ul><ul><li>You're running in an un-secure environment. </li></ul><ul><li>You want persistence. Or, a database. </li></ul>
  13. 13. <ul><li>Questions? </li></ul>
  14. 14. <ul><li>Thank You </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×