Memcache
-
Upload
abhinav-singh -
Category
Education
-
view
23.839 -
download
0
description
Transcript of Memcache
Who all are using it?
History of Memcached
• Brad Fitzpatrick from Danga Interactive developed memcached to enhance speed of livejournal.com, which was then doing 20 million+ dynamic PV’s per day.
• Memcached reduced the database load to almost nothing, yielding faster page load time and better resource utilization.
• Facebook is the biggest user of memcached after live journal. They have > 100 dedicated memcached servers.
Three W’s
• What is Memcached?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.
• When can we use it?Anywhere, if you have spare RAMMostly used in wiki, social networking and book marking sites.
• Why should we use it?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.
Pseudo Implementation
PHP DB API
Database
1st 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
Pseudo Implementation
PHP DB API
Database
2nd time Query
Checks cache, resultset already found. Un-serialize
and return the resultset
2Gb RAM reserved for caching the resultset
http://localhost
Pseudo Code• $listofphotos = $mdb->getPhotos();
• $query = “SELECT * from photos where …..”;
• $key = sha1($query)
• If ($key FOUND IN memcache) { $value = get($key);
$resultset = unserialize($value); return $resultset; } else { $resultset = mysql_query($query); set($key,serialize($resultset),$TTL); return $resultset; }
• PS: Do not use SQL_FOUND_ROWS in your $query while using memcache
Miscellaneous
• The story of memcache• MySQL Query Cache v/s APC v/s Memcache. Why
memcache?• Deciding your key intelligently (User:md5($query)) or
(Messages:sha1($query)). • A bit on how to apply versioning in keys.• Is memcache secure? No, then what precautions do I
take?• Reduce load on your database by not updating it for
trivial information like timestamp, last changed date etc.
Facebook – How do they do?
• Me: Hey how do you use memcache at facebook?
• 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. “
• Me: But does this work fine with datacenters spread around different countries?
Cross Datacenter Story• My Friend from Facebook: Consider this example:
1. I update my first name from “Abhinav" to "Monkey" 2. We write "Monkey" in to the master database in California and delete
my first name from memcache in California and Virginia 3. Someone goes to my profile in Virginia 4. We don't find my first name in memcache so we read from the Virginia
slave database and get “Abhinav" because of replication lag 5. We update Virginia memcache with my first name as “Abhinav" 6. Replication catches up and we update the slave database with my first
name as “Monkey” 7. Someone else goes to my profile in Virginia 8. We find my first name in memcache and returns “Abhinav” 9. Hence my first name will be shown as “Abhinav” in Virginia and “Monkey”
in California. Confusing?
Storing and Managing Collections
• 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 FORELSE 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 cacheEND IF
RETURN lstItems
Why not use memcache?
• You have objects larger than 1MB. • You have keys larger than 250 chars. • You're running in an un-secure environment. • You want persistence. Or, a database.
Questions?
Thank You