memcached Distributed Cache. memcached is the most popular cache solution for low latency high throughput websites. improves the read timings drastically.
Next-generation AAM aircraft unveiled by Supernal, S-A2
memcached Distributed Cache
1. Aniruddha Chakrabarti
AVP and Solution Lead, Digital Practice, Mphasis
ani.c@outlook.com | Linkedin.com/in/aniruddhac | slideshare.net/aniruddha.chakrabarti/ | Twitter - anchakra
Overview of memcached Distributed Cache
2. Agenda
• What is memcached (and what it’s not)
• Popularity of memcached, who uses memcached, history of memcached
• memcached vs. MemcacheDB
• Memcached operations
• Store data (set)
• Update data (replace, append, prepend)
• Retrieve data (get)
• Remove date (delete)
• Clear data
• Increment & Necrement numeric value
• Clear all data
• Server statistics
• Resources
3. What is memcached
• High-performance, distributed in-memory object caching system
• Free & open source (licensed under BSD license)
• Memcached in completely in memory, it cannot persist any data, and so it’s mostly
suitable for caching.
• For persistent storage memcachedb could be used, which is a different product.
• 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.
• Generic in nature, but mostly intended for use in speeding up dynamic web applications
by alleviating database load.
• Its simple design promotes quick deployment, ease of development, and solves many
problems facing large data caches.
• Memcahed runs on Linux, OSX and Windows. memcached API is available for most
popular languages.
4. Caching strategies
• Read-through caching
• Client tier requests Application tier for some data
• Application tier checks if the data already exist in Cache tier (memcached)
• If data exists, then it’s returned directly from Cache tier (memcached) saving
database round trip and load on database
• If data does not exist, then it’s retrieved from database and returned to app tier. The
data is added to cache tier so that subsequent requested could be served from cache.
• Write-through caching
• Data is written to memcached, data is not written directly to database (as done in
read-through approach). A separate process reads the stored data from memcached
and writes to the database
6. Popularity of memcached
• memcahed is ranked 22rd among all
Databases products including
RDBMS and NoSQL
• memcahed is the 2nd popular in
memory Key Value store after Redis
• It seems recently redis has become
more popular than memcached
7. memcached history
• Memcached was first developed by Brad Fitzpatrick for the website
LiveJournal, on May 22, 2003 at Danga Interactive. It reduced the database
load for the extremely busy web site LiveJournal.com, which was at the
time handling over 20 million dynamic page views per day for 1 million
users.
• Solved for LiveJournal.com the problem that many other sites also have —
how to reduce read access to the database.
• It was originally written in Perl, then later rewritten in C by Anatoly
Vorobey, then employed by LiveJournal.
8. memcached vs MemcacheDB
• memcached is a distributed in memory key value cache.
• MemcacheDB is a distributed key-value storage system designed for
persistent. It is NOT a cache solution, but a persistent storage engine for
fast and reliable key-value based object storage and retrieval.
9. Installing and running memcached
Download memcached from http://memcached.org/downloads.
Unzip the .gz file to any folder
memcached Server:
• Run memcached.exe
memcached Client:
• Connect to memcached via telnet
C:>telnet 127.0.0.1 11211
11211 is the default port
to which memcached server listens
10. Set Data
• set command is used to set a value to key; if the key does not exist, a new key is created
and value is assigned to that key.
set key flags exptime bytes [noreply]
value
• key - It is the name of the unique key by which data is accessed.
• flags - It is the 32-bit unsigned integer that the server stores with the data provided by the user,
and returns along with the data when the item is retrieved.
• exptime - It is the expiration time (seconds) of data stored in cache. A 0 value means "never
expire", i.e. it should not be removed from the cache unless required. If the exptime is more than
30 days then Memcached interprets it as UNIX timestamp for expiration.
• bytes - This is the length of the data in bytes that needs to be stored in Memcached.
• noreply (optional) - This parameter informs the server not to send any reply.
• value - It is the data that needs to be stored. The data needs to be given on the new line after
executing the command with the above options.
11. Set Data - example
set key flags exptime bytes [noreply]
Value
set tutorial 0 0 18
memcached tutorial
STORED
set city 0 900 9
bangalore
STORED
Flags Expiry time (in seconds)
Length of data in bytes to be stored
Value to be stored
Key
Flags
Expiry time (in seconds)
0 indicates no expiry
Length of data in bytes to be stored
Value to be stored
Key
• key - It is the name of the unique key by
which data is accessed.
• flags - It is the 32-bit unsigned integer
that the server stores with the data
provided by the user, and returns along
with the data when the item is retrieved.
• exptime - It is the expiration time
(seconds) of data stored in cache. A 0
value means "never expire", i.e. it should
not be removed from the cache unless
required. If the exptime is more than 30
days then Memcached interprets it as
UNIX timestamp for expiration.
• bytes - This is the length of the data in
bytes that needs to be stored in
Memcached.
• noreply (optional) - This parameter
informs the server not to send any reply.
• value - It is the data that needs to be
stored. The data needs to be given on the
new line after executing the command
with the above options.
12. Get Data - example
get key [noreply]
set city 0 900 9
bangalore
STORED
set tutorial 0 0 18
memcached tutorial
STORED
get city
VALUE city 0 9
bangalore
END
get tutorial
VALUE tutorial 0 9
memcached tutorial
END
get key1 key2 [noreply]
get city tutorial
VALUE city 0 9
bangalore
VALUE tutorial 0 11
memcached t
END
13. Append Data
• append command is used to add data in an existing key. This data is added at the
end of the previous value.
append key flags exptime bytes [noreply]
value
append city 0 0 8
,india
STORED
get city
VALUE city 0 17
bangalore,india
14. Prepend Data
• prepend command is used to add data in an existing key. This data is added before the
existing data of the key.
prepend key flags exptime bytes [noreply]
value
get city
VALUE city 0 17
bangalore,india
prepend city 0 0 13
whitefield,
STORED
get city
VALUE city 0 30
whitefield,bangalore,india
END
15. Replace Data
• replace command is used to replace the value of an existing key. If the key does not
exist, then it gives the output NOT_STORED.
prepend key flags exptime bytes [noreply]
value
get city
VALUE city 0 30
whitefield,bangalore,india
replace city 0 0 7
kolkata
STORED
get city
VALUE city 0 7
kolkata
16. Delete Data
delete key [noreply]
Output:
• DELETED indicates successful deletion.
• ERROR indicates error while deleting data or wrong syntax.
• NOT_FOUND indicates that the key does not exist in the Memcached server.
delete city
DELETED
17. Incr and Decr
• incr and decr commands are used to increment or decrement the numeric value of an existing key.
If the key is not found, then it returns NOT_FOUND. If the key is not numeric, then it
returns CLIENT_ERROR cannot increment or decrement non-numeric value. Otherwise, ERROR is
returned.
incr key increment_value
set ctr 0 0 1
1
STORED
incr ctr 1
2
incr ctr 5
7
decr key decrement_value
decr ctr 1
6
incr ctr 3
3
18. Server Stats
• stats command is used to return server statistics such as PID, version, connections, etc.
stats
STAT pid 7484
STAT uptime 17901
STAT time 1442771796
STAT version 1.4.5_4_gaa7839e
STAT pointer_size 64
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 24
STAT cmd_set 13
STAT cmd_flush 0
STAT get_hits 18
...
19. Clear Data
• flush_all command is used to delete all data (key-value pairs) from the Memcached server. It
accepts an optional parameter called time, that sets a time after which the Memcached data is to be
cleared.
flush_all
OK
get city tutorial
END
20. Using memcached from node
https://github.com/3rd-Eden/memcached
Node driver Installation – npm install memcached
var Memcached = require('memcached');
// first parameter is server location which is mentioned as host:port
// Here host is 127.0.0.1 or localhost & port is 11211 which is the default port for memcached
var memcached = new Memcached("127.0.0.1:11211");
// Other options could be passed as an object as second parameter
//var options = {retries:10,retry:10000,remove:true,failOverServers:['192.168.0.103:11211']};
//var memcached = new Memcached("127.0.0.1:11211", options);
21. Using get and set from node
memcached.get - Get the value for the given key in Memcached.
memcached.set - Stores a new value in Memcached.
var key = 'city', value = 'Bangalore';
var lifetime = 300; // lifetime in seconds
memcached.set(key, value, lifetime, function(error){
if (!error){
console.log('data added to cache');
memcached.get(key, function(error, data){
console.log(data);
});
}
});
22. Using add from node (error)
memcached.add - Add the value, only if it's not in memcached already.
var key = 'city', value = 'Bangalore', lifetime = 0; // lifetime in seconds, 0 means no expiry
memcached.set(key, value, lifetime, function(error){
if (!error){
console.log('data added to cache');
memcached.get(key, function(error, data){
console.log(data);
});
memcached.add(key, "Den Haag", lifetime, function (error){
if (error){
console.log(error);
}
});
}
});
23. Using add from node (success)
memcached.add - Add the value, only if it's not in memcached already.
memcached.add('new_city', 'Den Haag', 300, function (error){
if (error){
console.log(error);
}
else{
memcached.get('new_city', function(error, data){
console.log(data);
});
}
});
24. Using prepend and append from node
memcached.append - Add the given value to the start of an existing item
memcached.prepend - Add the given value to the end of an existing item
memcached.prepend(key, "Whitefield, ", function(error){
if (!error){
memcached.append(key, ", Karnataka, IN", function(error){
memcached.get(key, function (error, data){
if(!error){
console.log(data);
// displays Whitefield, Bangalore, Karnataka, IN
}
});
});
}
})
25. Using replace and del from node
memcached.replace - Replaces the existing value of a key in memcached
memcached.del - Removes the key from memcached
memcached.replace('city','Minneapolis, MN', 300, function(err){});
memcached.get('city', function(err, data){
console.log(data); // displays Minneapolis, MN
});
memcached.del('city',function(err){
if(!err){
console.log('del success');
}
});
26. Using getMulti from node
memcached.getMulti - Retrieves a bunch of values from multiple keys
memcached.set('city1','New Delhi',0, function(error){});
memcached.set('city2','Kolkata',0, function(error){});
memcached.set('city3','Amsterdam',0, function(error){});
memcached.getMulti(['city1','city2','city3'], function (error, data){
if (!error){
console.log(data.city1);
console.log(data.city2);
console.log(data.city3);
}
})
27. Using incr and decr from node
memcached.incr - Increment a given key by the specified value
memcached.incr - Decrement a given key by the specified value
memcached.set('ctr', 10, 0, function(error){});
memcached.incr('ctr', 5, function(error){});
memcached.get('ctr', function(error, data){
console.log(data); // 15
});
memcached.decr('ctr', 10, function(error){});
memcached.get('ctr', function(error, data){
console.log(data); // 5
});