SlideShare a Scribd company logo
Scaling with Memcached http://danga.com/memcached By Abhinav Singh
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 W hat 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.  W hen can we use it? Anywhere, if you have spare RAM Mostly used in wiki, social networking and book marking sites. W hy 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 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
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
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 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
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

More Related Content

What's hot (20)

PPTX
Supporting Apache HBase : Troubleshooting and Supportability Improvements
DataWorks Summit
 
PPTX
Apache Kudu: Technical Deep Dive


Cloudera, Inc.
 
PDF
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
Altinity Ltd
 
PPTX
Introduction to Redis
TO THE NEW | Technology
 
PDF
Hardening Kafka Replication
confluent
 
PPTX
Hadoop security
Shivaji Dutta
 
PDF
All about Zookeeper and ClickHouse Keeper.pdf
Altinity Ltd
 
PDF
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Altinity Ltd
 
PDF
Cassandra Introduction & Features
DataStax Academy
 
PPTX
Introduction to Redis
Arnab Mitra
 
PPT
Hadoop Security Architecture
Owen O'Malley
 
PDF
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
Databricks
 
PPTX
NGINX: Basics and Best Practices
NGINX, Inc.
 
PDF
Materialize: a platform for changing data
Altinity Ltd
 
PDF
Hive Bucketing in Apache Spark with Tejas Patil
Databricks
 
PDF
[EN] Building modern data pipeline with Snowflake + DBT + Airflow.pdf
Chris Hoyean Song
 
PDF
MySQL Administrator 2021 - 네오클로바
NeoClova
 
PDF
Neo4j Fundamentals
Max De Marzi
 
PDF
Introduction to Redis
Dvir Volk
 
PDF
Introduction to elasticsearch
pmanvi
 
Supporting Apache HBase : Troubleshooting and Supportability Improvements
DataWorks Summit
 
Apache Kudu: Technical Deep Dive


Cloudera, Inc.
 
OSA Con 2022 - Apache Iceberg_ An Architectural Look Under the Covers - Alex ...
Altinity Ltd
 
Introduction to Redis
TO THE NEW | Technology
 
Hardening Kafka Replication
confluent
 
Hadoop security
Shivaji Dutta
 
All about Zookeeper and ClickHouse Keeper.pdf
Altinity Ltd
 
Deep Dive on ClickHouse Sharding and Replication-2202-09-22.pdf
Altinity Ltd
 
Cassandra Introduction & Features
DataStax Academy
 
Introduction to Redis
Arnab Mitra
 
Hadoop Security Architecture
Owen O'Malley
 
Apache Spark Data Source V2 with Wenchen Fan and Gengliang Wang
Databricks
 
NGINX: Basics and Best Practices
NGINX, Inc.
 
Materialize: a platform for changing data
Altinity Ltd
 
Hive Bucketing in Apache Spark with Tejas Patil
Databricks
 
[EN] Building modern data pipeline with Snowflake + DBT + Airflow.pdf
Chris Hoyean Song
 
MySQL Administrator 2021 - 네오클로바
NeoClova
 
Neo4j Fundamentals
Max De Marzi
 
Introduction to Redis
Dvir Volk
 
Introduction to elasticsearch
pmanvi
 

Similar to Memcache (20)

PPTX
Scalling web applications using memcache
Sudar Muthu
 
PPTX
1
tristup
 
ODP
Caching and tuning fun for high scalability @ PHPTour
Wim Godden
 
PPT
Php MySql For Beginners
Priti Solanki
 
PPTX
Using memcache to improve php performance
Sudar Muthu
 
PDF
Zend Server Data Caching
El Taller Web
 
ODP
Caching and tuning fun for high scalability @ phpBenelux 2011
Wim Godden
 
ODP
phptek13 - Caching and tuning fun tutorial
Wim Godden
 
PPT
Caching Data For Performance
Dave Ross
 
ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
Wim Godden
 
PDF
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Michael Plöd
 
ODP
Caching and tuning fun for high scalability
Wim Godden
 
PDF
Give Your Site a Boost with Memcache
Ben Ramsey
 
PPTX
Memcached
Shrawan Kumar Nirala
 
ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
Wim Godden
 
PPT
Zend Con 2008 Slides
mkherlakian
 
PDF
Scaling Rails with memcached
elliando dias
 
KEY
Site Performance - From Pinto to Ferrari
Joseph Scott
 
PPTX
Memcached B box presentation
Nagesh Chinkeri
 
Scalling web applications using memcache
Sudar Muthu
 
Caching and tuning fun for high scalability @ PHPTour
Wim Godden
 
Php MySql For Beginners
Priti Solanki
 
Using memcache to improve php performance
Sudar Muthu
 
Zend Server Data Caching
El Taller Web
 
Caching and tuning fun for high scalability @ phpBenelux 2011
Wim Godden
 
phptek13 - Caching and tuning fun tutorial
Wim Godden
 
Caching Data For Performance
Dave Ross
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Wim Godden
 
Spring One 2 GX 2014 - CACHING WITH SPRING: ADVANCED TOPICS AND BEST PRACTICES
Michael Plöd
 
Caching and tuning fun for high scalability
Wim Godden
 
Give Your Site a Boost with Memcache
Ben Ramsey
 
Caching and tuning fun for high scalability @ FOSDEM 2012
Wim Godden
 
Zend Con 2008 Slides
mkherlakian
 
Scaling Rails with memcached
elliando dias
 
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Memcached B box presentation
Nagesh Chinkeri
 
Ad

Recently uploaded (20)

PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Ad

Memcache

  • 1. Scaling with Memcached http://danga.com/memcached By Abhinav Singh
  • 2. Who all are using it?
  • 3. 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.
  • 4. Three W’s W hat 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. W hen can we use it? Anywhere, if you have spare RAM Mostly used in wiki, social networking and book marking sites. W hy 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.
  • 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. 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. 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
  • 8. 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.
  • 9. 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?
  • 10. 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?
  • 11. 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 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
  • 12. 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.