In this article, we will learn about Memcache. We will learn the Memcache with question and answer approach.
What is “Memcached”?
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached uses an LRU caching algorithm(Least Recently Used (LRU) – discards the least recently used items first).
Why “Memcached”?
-
Free & Open source
-
High performance
-
Simple to set up
-
Ease of development
-
APIs are available for most popular languages
Who are all using “Memcached”?
-
ProliphiqViews iPad application
-
Youtube.com
-
Digg.com
-
Twitter.com
-
Wikipedia.com
-
Flickr
-
Craiglist.com
- ….. So many users
What are the Use cases to use “Memcached”?
-
To achieve high performance
- To achieve the scalability
The above question and answers will give us some insights about the Memcached. Now, we will see “how the traditional systems will use the cache” from the below diagram.
In the above architecture, the cache is part of the application server. In this case, some of the memory is allocated for cache from the heap size allotted to the JVM. If the cache size increases, then the heap size need to increase. There is no better control over the cache, because, the cache is bundled with the app server. To have better control, we need to separate the cache from the app server. The architecture given below depicts the same.
In the above architecture, we have separated the cache from the application. So, either we can maintain the cache in the same server where the application is deployed or we can assign separate machine for the cache. If the cache size increases, we can increase the RAM size without affecting the application. So, we have better control over the cache.
How to install and set up “Memcached”?
In this article, I have used the Ubuntu operating system to install and set up the Memcached. Follow the below steps to install and run the Memcached.
- Download the source code from https://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
- Extract the downloaded file and move to the extracted folder.
- Run the below commands
- ./configure
- Make & make install
- After the successful installation start the Memcached using memcached -u <user name>
- The Memcache server will run by default on 11211 port
- Open a new command prompt and try to connect to the Memcached through telnet. For example telnet <host> 11211. If you are able to connect successfully to the Memcached server then the installation and the setup is done.
What operations we can perform with “Memcached”?
After establishing the connection to the Memcached through telnet, we can perform the below operations.
- Set(Store key/value pair in Memcached)
Format:
- set <key> <flags> <exptime> <bytes>
Parameters:
- <key>: the key of the data stored
- <flags>: 32-bit unsigned integer that the server store with the data (provided by the user), and return along with the data when the item is retrieved
- <exptime>: expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX timestamp for expiration
- <bytes>: number of bytes in the data block
These commands can return :
- STORED to indicate success
- NOT_STORED indicate that the data was not stored because the condition for “add” or “replace” command wasn’t met, or the item is in a delete queue
- Get(Get value from the key)
Format:
- get <key>
Parameters:
- <key>: the key of the data stored to retrieve
- <key>* mean you can request the value of multiple keys separated by white space
These commands return :
- VALUE <flag> <bytes>\r\n<data>
- END indicates the end of the response
- Add(Store key/value pair in Memcached, but only if the server doesn’t already hold data for this key)
Format:
- add <key> <flags> <exptime> <bytes>
Parameters:
- <key>: the key of the data stored
- <flags> : 32-bit unsigned integer that the server
- store with the data (provided by the user), and return along with the data when the item is retrieved
- <exptime>: expiration time in seconds, 0 mean no delay, if exptime is superior to 30 days, Memcached will use it as a UNIX time stamps for expiration
- <bytes>: number of bytes in the data block
These commands can return :
- STORED to indicate success
- NOT_STORED indicate that the data was not stored because the condition for “add” or “replace” command wasn’t met, or the item is in a delete queue
- Replace(Store key/value pair in Memcached, but only if the server already hold data for this key)
Format:
- replace <key> <flags> <exptime>
<bytes> - Delete(Deletes key/value pair in Memcached)
Format:
- delete <key>
- Flush_all(Flush the server key/value pair (invalidating them) )
Format:
- flush_all
- stats(Return general-purpose statistics like uptime, version, memory occupation, …)
Format:
- stats
- Item stats(Return items statistics, will display items statistics (count, age, eviction, …))
Format:
- stats items
Do we have any client API’s?
We have client APIs for all the major languages. You can find the list of client API’s here.
How to use JAVA client API?
Follow the below steps to use the JAVA client API.
- To contact Memcached from Java, download the java client from http://code.google.com/p/spymemcached/
- Get Memcached client
- MemcachedClient c=new MemcachedClient(new InetSocketAddress(“localhost”, 11211));
- After the completion of operations, call shutdown() on memcached client.
Now, you can try out “Memcached” as part of your applications.
[…] Redis看起来和Memcached很相像,但它们之间还是有些不同。 […]
Very good article. Thanks for your effort. Keep posting new articles.
I couldn’t refrain from commenting. Exceptionally well written!
The technology usage with use cases are good. Are there any other prominent cache frameworks available?
I am using spring framework for our applications. How can we use memcached with spring framework. Any references?
Thanks.
Nice explanation!!!
Thanks!
The article is good, thats why i have read it
completely
You made it looks very simple. Thanks for you effort!
Thank you, Thiyagu.