SlideShare a Scribd company logo
Memcached
Free & open source, high-
performance, distributed memory
object caching system
B I
G H A
S H T A
B L E
Memcached
Memcached
What to Store/Cache
So what to cache :
• Query Results
• Object with much Calculation
• Any thing that takes time to generate
Database calls , API calls,Page rendering etc..
General Steps while using memcached
Stroring/Updating DB calls result
• 1) Right before you query the database to retrieve data, check to see if
that information is stored in the cache. If the data is found in memcached,
then use the cached data as opposed to querying the database for the
information.
• 2) If the information is not found in memcached, then go ahead, call the
database. Once you load the data you queried for, don’t forget to put in
the cache. Then proceed normally. Now, in subsequent calls to fetch this
information you don’t need to call the database at all.
• 3)Now, if the information changes for some reason, a user updates the
data for example, then delete the data from the cache. That way if
someone tries to load it again, they will be forced to go back to the
database to get it again. This keeps the cache fresh and accurate.
Restiction on Key-Value
• Key : 250 Chars
• Object size: 1 MB
Memcached Client for Java
• http://www.couchbase.org/code/couchbase/java
(spymemcached)
• http://code.google.com/p/xmemcached
• http://www.whalin.com/memcached
• http://code.google.com/p/javamemcachedclient
• http://code.google.com/p/memcache-client-
forjava
Used in CMS (spymemcached and xmemcached)
Used in CMS
• Stroing SSOUserDetails and UserDetails
• Tomcat User Sessions Object data
(On the fly add memcached servers to clients
using JMX.)
• Storing Flat Hierarchy …
• Graph…
Next Dam…
Cache algorithm
• Cache algorithm is LRU(Least Recently Used)
Memcached In Clusters
Memcached In Clusters
• Memcached servers won't know about each other
• Memcached client kwon all the memcached servers
• Every API request takes a "key" parameter. There is a 2-
step process at the client side...
• For given key, locate the server
server = serverlist[hash(key)%serverlist.length];
This works great – if the server set never changes.
• Forward the request to that server and get results
What if one or more servers down ?
If one or more servers down in pool of servers
the client starts using other servers from the
pool.
Price of failure is the cost of extra lookup from
DB, API calls etc..
How to Run/Start
• memcached –d –m 256 –c 1024 –l 192.168.33.239 –p 11211 –u root
d = daemon, m = memory, u = user, p = port
What if defined limit for storing data reached?
start/stop memcahced:
sudo service memcached stop
sudo service memcached start
sudo service memcached restart
When Memory Is Reclaimed ?
• Memory for an item is not actively reclaimed. If
you store an item and it expires, it sits in the LRU
cache at its position until it falls to the end.
• However, if you fetch an expired item,
memcached will find the item, notice that it's
expired, and free its memory. This gives you the
common case of normal cache churn reusing its
own memory.
• Items can also be evicted to make way for new
items that need to be stored, or expired items are
discovered and their memory reused.
Most Commonly Used Commands
Storage Commands
• set
Most common command. Store this data, possibly overwriting any
existing data. New items are at the top of the LRU.
• add
Store this data, only if it does not already exist. New items are at
the top of the LRU. If an item already exists and an add fails, it
promotes the item to the front of the LRU anyway.
("Touching" keys with add)
Create an item that you want to expire in a week? Don't always fetch
the item but want it to remain near the top of the LRU for some
reason? add will actually bump a value to the front of memcached's
LRU if it already exists. If the add call succeeds, it means it's time to
recache the value anyway.
Retrieval Commands
• get
Command for retrieving data. Takes one or more keys
and returns all found items.
Delete Command
• delete
Removes an item from the cache, if it exists.
• incr/decr
Increment and Decrement.
Statistics
• There're a handful of commands that return counters and settings of the
memcached server.
• stats
basic stats command.
• stats items
Returns some information, broken down by slab, about items stored in
memcached.
• stats slabs
Returns more information, broken down by slab, about items stored in
memcached. More centered to performance of a slab rather than counts
of particular items.
• stats sizes
A special command that shows you how items would be distributed.
• flush_all
Invalidate all existing cache items
This command does not pause the server, as it returns immediately. It
does not free up or flush memory at all, it just causes all items to expire.
Security in memcached?
• Prevent external access - Deploy memcached behind your firewall and
only allow machines from within a specific network to access to the cache.
• Encrypt the data you store in the cache – I personally feel like this is
overkill because for most applications it adds an extra hoop to jump
through every single time you visit the cache. But for the hyper-paranoid
that work in shared environments, I suppose this is something worth
considering.
• Choose obscure keys – there is no way for a user to query memcached
for a list of keys, therefore the only way for someone to retrieve
information stored there is if they know the key for the corresponding
information. Therefore, if your keys have predictable names then it would
be relatively easy for someone to guess them. So make your keys
somewhat obscure. Consider creating a simple key like “object:10032” and
then generate a sha1 hash of it. This will create a very obscure key name
while using a very standard, easy to remember key naming scheme of your
choosing.
Reference
. http://memcached.org/
.https://code.google.com/p/memcached/wiki/NewStart?tm=6
• https://code.google.com/p/memcached/wiki/NewConfiguringClient
• http://www.slideshare.net/gear6memcached/implementing-high-
availability-services-for-memcached-1911077?related=1
• https://ihong5.wordpress.com/2014/08/19/consistent-hashing-
algorithm/
• https://www.adayinthelifeof.nl/2011/02/06/memcache-internals/
• http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_-
_a_consistent_hashing_algo_for_memcache_clients
Thank You 

More Related Content

PDF
Memcached Study
KEY
Introduction to memcached
PPTX
캐시 분산처리 인프라
PDF
Memcached Presentation
PPTX
Using memcache to improve php performance
PDF
Memcached Code Camp 2009
PDF
Caching with Memcached and APC
PDF
Moxi - Memcached Proxy
Memcached Study
Introduction to memcached
캐시 분산처리 인프라
Memcached Presentation
Using memcache to improve php performance
Memcached Code Camp 2009
Caching with Memcached and APC
Moxi - Memcached Proxy

What's hot (19)

PPT
Memcache
ODP
Cassandra as Memcache
PDF
Varnish Configuration Step by Step
PDF
Introduction to Redis
PDF
The Google Chubby lock service for loosely-coupled distributed systems
PPTX
Caching
PDF
Redis memcached pdf
KEY
Memcached: What is it and what does it do?
PPTX
cache concepts and varnish-cache
PDF
Caching with Varnish
KEY
DjangoCon 2010 Scaling Disqus
PDF
Practicing Continuous Deployment
PDF
Implementing High Availability Caching with Memcached
PPTX
NGINX High-performance Caching
PDF
Kafka Summit SF 2017 - One Day, One Data Hub, 100 Billion Messages: Kafka at ...
PDF
Top ten-list
PDF
Ehcache 3: JSR-107 on steroids at Devoxx Morocco
PDF
How Prometheus Store the Data
ODP
Caching Strategies
Memcache
Cassandra as Memcache
Varnish Configuration Step by Step
Introduction to Redis
The Google Chubby lock service for loosely-coupled distributed systems
Caching
Redis memcached pdf
Memcached: What is it and what does it do?
cache concepts and varnish-cache
Caching with Varnish
DjangoCon 2010 Scaling Disqus
Practicing Continuous Deployment
Implementing High Availability Caching with Memcached
NGINX High-performance Caching
Kafka Summit SF 2017 - One Day, One Data Hub, 100 Billion Messages: Kafka at ...
Top ten-list
Ehcache 3: JSR-107 on steroids at Devoxx Morocco
How Prometheus Store the Data
Caching Strategies
Ad

Similar to Memcached (20)

PDF
Where Django Caching Bust at the Seams
PPTX
Memcached B box presentation
PPTX
Real time analytics using Hadoop and Elasticsearch
PPTX
memcached Distributed Cache
PPT
7. Key-Value Databases: In Depth
PDF
Overview of the ehcache
PPTX
#GeodeSummit - Off-Heap Storage Current and Future Design
PPTX
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
PDF
Architecting for the cloud elasticity security
PPTX
Cassandra
PDF
Building Big Data Streaming Architectures
PPTX
Mini-Training: To cache or not to cache
PPTX
Entity framework advanced
PDF
Avoiding big data antipatterns
PPTX
My Database Skills Killed the Server
PDF
Membase Intro from Membase Meetup San Francisco
ODP
Caching and tuning fun for high scalability
PDF
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
PPTX
L6.sp17.pptx
Where Django Caching Bust at the Seams
Memcached B box presentation
Real time analytics using Hadoop and Elasticsearch
memcached Distributed Cache
7. Key-Value Databases: In Depth
Overview of the ehcache
#GeodeSummit - Off-Heap Storage Current and Future Design
Search on the fly: how to lighten your Big Data - Simona Russo, Auro Rolle - ...
Caching and tuning fun for high scalability @ FrOSCon 2011
Architecting for the cloud elasticity security
Cassandra
Building Big Data Streaming Architectures
Mini-Training: To cache or not to cache
Entity framework advanced
Avoiding big data antipatterns
My Database Skills Killed the Server
Membase Intro from Membase Meetup San Francisco
Caching and tuning fun for high scalability
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
L6.sp17.pptx
Ad

Memcached

  • 1. Memcached Free & open source, high- performance, distributed memory object caching system
  • 2. B I G H A S H T A B L E
  • 5. What to Store/Cache So what to cache : • Query Results • Object with much Calculation • Any thing that takes time to generate Database calls , API calls,Page rendering etc..
  • 6. General Steps while using memcached Stroring/Updating DB calls result • 1) Right before you query the database to retrieve data, check to see if that information is stored in the cache. If the data is found in memcached, then use the cached data as opposed to querying the database for the information. • 2) If the information is not found in memcached, then go ahead, call the database. Once you load the data you queried for, don’t forget to put in the cache. Then proceed normally. Now, in subsequent calls to fetch this information you don’t need to call the database at all. • 3)Now, if the information changes for some reason, a user updates the data for example, then delete the data from the cache. That way if someone tries to load it again, they will be forced to go back to the database to get it again. This keeps the cache fresh and accurate.
  • 7. Restiction on Key-Value • Key : 250 Chars • Object size: 1 MB
  • 8. Memcached Client for Java • http://www.couchbase.org/code/couchbase/java (spymemcached) • http://code.google.com/p/xmemcached • http://www.whalin.com/memcached • http://code.google.com/p/javamemcachedclient • http://code.google.com/p/memcache-client- forjava Used in CMS (spymemcached and xmemcached)
  • 9. Used in CMS • Stroing SSOUserDetails and UserDetails • Tomcat User Sessions Object data (On the fly add memcached servers to clients using JMX.) • Storing Flat Hierarchy … • Graph… Next Dam…
  • 10. Cache algorithm • Cache algorithm is LRU(Least Recently Used)
  • 12. Memcached In Clusters • Memcached servers won't know about each other • Memcached client kwon all the memcached servers • Every API request takes a "key" parameter. There is a 2- step process at the client side... • For given key, locate the server server = serverlist[hash(key)%serverlist.length]; This works great – if the server set never changes. • Forward the request to that server and get results
  • 13. What if one or more servers down ? If one or more servers down in pool of servers the client starts using other servers from the pool. Price of failure is the cost of extra lookup from DB, API calls etc..
  • 14. How to Run/Start • memcached –d –m 256 –c 1024 –l 192.168.33.239 –p 11211 –u root d = daemon, m = memory, u = user, p = port What if defined limit for storing data reached? start/stop memcahced: sudo service memcached stop sudo service memcached start sudo service memcached restart
  • 15. When Memory Is Reclaimed ? • Memory for an item is not actively reclaimed. If you store an item and it expires, it sits in the LRU cache at its position until it falls to the end. • However, if you fetch an expired item, memcached will find the item, notice that it's expired, and free its memory. This gives you the common case of normal cache churn reusing its own memory. • Items can also be evicted to make way for new items that need to be stored, or expired items are discovered and their memory reused.
  • 16. Most Commonly Used Commands Storage Commands • set Most common command. Store this data, possibly overwriting any existing data. New items are at the top of the LRU. • add Store this data, only if it does not already exist. New items are at the top of the LRU. If an item already exists and an add fails, it promotes the item to the front of the LRU anyway. ("Touching" keys with add) Create an item that you want to expire in a week? Don't always fetch the item but want it to remain near the top of the LRU for some reason? add will actually bump a value to the front of memcached's LRU if it already exists. If the add call succeeds, it means it's time to recache the value anyway.
  • 17. Retrieval Commands • get Command for retrieving data. Takes one or more keys and returns all found items. Delete Command • delete Removes an item from the cache, if it exists. • incr/decr Increment and Decrement.
  • 18. Statistics • There're a handful of commands that return counters and settings of the memcached server. • stats basic stats command. • stats items Returns some information, broken down by slab, about items stored in memcached. • stats slabs Returns more information, broken down by slab, about items stored in memcached. More centered to performance of a slab rather than counts of particular items. • stats sizes A special command that shows you how items would be distributed. • flush_all Invalidate all existing cache items This command does not pause the server, as it returns immediately. It does not free up or flush memory at all, it just causes all items to expire.
  • 19. Security in memcached? • Prevent external access - Deploy memcached behind your firewall and only allow machines from within a specific network to access to the cache. • Encrypt the data you store in the cache – I personally feel like this is overkill because for most applications it adds an extra hoop to jump through every single time you visit the cache. But for the hyper-paranoid that work in shared environments, I suppose this is something worth considering. • Choose obscure keys – there is no way for a user to query memcached for a list of keys, therefore the only way for someone to retrieve information stored there is if they know the key for the corresponding information. Therefore, if your keys have predictable names then it would be relatively easy for someone to guess them. So make your keys somewhat obscure. Consider creating a simple key like “object:10032” and then generate a sha1 hash of it. This will create a very obscure key name while using a very standard, easy to remember key naming scheme of your choosing.
  • 20. Reference . http://memcached.org/ .https://code.google.com/p/memcached/wiki/NewStart?tm=6 • https://code.google.com/p/memcached/wiki/NewConfiguringClient • http://www.slideshare.net/gear6memcached/implementing-high- availability-services-for-memcached-1911077?related=1 • https://ihong5.wordpress.com/2014/08/19/consistent-hashing- algorithm/ • https://www.adayinthelifeof.nl/2011/02/06/memcache-internals/ • http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_- _a_consistent_hashing_algo_for_memcache_clients