Memcached is a free and open-source distributed memory caching system that can be used to speed up dynamic web applications by reducing database load. It stores objects in memory to return frequently or recently used results very quickly. Common things to cache include query results, objects with heavy calculations, and anything that takes time to generate like database calls, API calls, or page rendering. The memcached client knows all memcached servers and hashes keys to determine which server to store or retrieve each object from. Objects are stored using keys and have a maximum size of 1MB. Commands like get, set, add, delete are used to interact with the cache.
Report
Share
Report
Share
1 of 21
More Related Content
Memcached
1. Memcached
Free & open source, high-
performance, distributed memory
object caching system
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.
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…
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.