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

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