Thursday, July 10, 2014

Memcached

Today start to look at Memcached.

Memcached [1][7] is a distributed in-memory key/value cache system. It is open source under the BSD license.  The first version was written in Perl by Brad Fitzpatrick in May, 2003 for his website Live Journal (as one can notice, this site runs very fast).  Then it was re-written in C by Anatoly Vorobey.  It uses server-client architecture. Multiple servers don't talk to each other, the client hashes the key and chooses a target server to store the data. It's designed for unix/linux, but was ported to windows too. That's all about it.

Memcached homepage is at [1]. Here you can understand what it's about, and download it. There is a small wiki [6] that contains basic information about it. The current version is 1.4.20 as of July 2014.

== Install ==

1) To install from package (recommended, especially if you are deploying to multiple servers):

Ubuntu & Debian: apt-get install memcached
Redhat/Fedora: yum install memcached
FreeBSD/(Mac ?): portmaster databases/memcached

2) Although less recommended, you can install from source too:

First you will need libevent as pre-requisite:
Ubuntu: apt-get install libevent-dev
Redhat/Fedora: yum install libevent-devel

Then install memcached:
wget http://memcached.org/latest
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure [--prefix=/usr/local/memcached]
make && make test
sudo make install


== Run from console ==

memcached [-m 64] -p 11211

Here -m specifies the memory allocated, unit is MB. -p is the port used, and 11211 is the default.

-- Test --

You can test memcached using the telnet interface: telnet localhost 11211
Then these commands can be used [2][3][4]:

get [key]
set [key flag timeout size]
add [key flag timeout size]
replace [key flag timeout size]
append [key flag timeout size]
prepend [key flag timeout size]
incr [key int_value]
decr [key int_value]
delete [key]
flush_all [ |timeout]
stats [ |slabs|malloc|items|detail|sizes|reset]
version
verbosity
quit

An example console session is:

telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
version
VERSION 1.4.20  
verbosity 10   # this will cause the server side echo telnet client input.
OK
stats
...                  
stats slabs
...
stats items
...
add mykey1 0 3600 5  # "mykey1" is the key, "0" is a 32-bit unsigne int  flag, "3600" is expiration (seconds), "5" is data size.
12345            # this is the data you store at key "mykey1".
STORED      
get mykey1
12345
END
quit
Connection closed by foreign host.

== Run as daemon ==

memcached -d

Or:
sudo service memcached stop
sudo service memcached start
sudo service memcached restart


Or:
sudo /etc/init.d/memcached start
sudo /etc/init.d/memcached stop
sudo /etc/init.d/memcached restart

Now if you run "top" command, you can see "memcached" in list of processes. You can run multiple versions of memcached using different ports. For more details, refer to [5] etc.

== Configuration of server, client and cluster ==

See [6].

== More things about using memcached, maintenance and development ==

See [6].

== Source code ==

The source code is available by instruction at [8] on its homepage [1]. A count of LOC on the current version is:

[./memcached/assoc.c] Lines: 293
[./memcached/assoc.h] Lines: 9
[./memcached/cache.c] Lines: 148
[./memcached/cache.h] Lines: 116
[./memcached/daemon.c] Lines: 89
[./memcached/globals.c] Lines: 25
[./memcached/hash.c] Lines: 21
[./memcached/hash.h] Lines: 14
[./memcached/items.c] Lines: 936
[./memcached/items.h] Lines: 37
[./memcached/jenkins_hash.c] Lines: 431
[./memcached/jenkins_hash.h] Lines: 15
[./memcached/memcached.c] Lines: 5646
[./memcached/memcached.h] Lines: 610
[./memcached/murmur3_hash.c] Lines: 124
[./memcached/murmur3_hash.h] Lines: 19
[./memcached/protocol_binary.h] Lines: 470
[./memcached/sasl_defs.c] Lines: 190
[./memcached/sasl_defs.h] Lines: 31
[./memcached/sizes.c] Lines: 29
[./memcached/slabs.c] Lines: 882
[./memcached/slabs.h] Lines: 49
[./memcached/solaris_priv.c] Lines: 44
[./memcached/stats.c] Lines: 375
[./memcached/stats.h] Lines: 8
[./memcached/testapp.c] Lines: 1967
[./memcached/thread.c] Lines: 854
[./memcached/timedrun.c] Lines: 102
[./memcached/trace.h] Lines: 71
[./memcached/util.c] Lines: 144
[./memcached/util.h] Lines: 33

[.] Total Lines: 13782


== Windows version ==

Memcached was designed for unix/linux. However, windows version is also available due to community support [9][10]. [11] is an example that seems no longer available.  One major vendor seems to be North Scale labs, they first provide memcached for windows as a stand-alone application, then combines it into their NoSQL product MemBase, and finally combines into CouchBase [12].


References:

[1] http://memcached.org
[2] Memcached telnet command summary
[3] Memcache Telnet Interface
[4] github: memcached / doc / protocol.txt 
[5] stackoverflow: stop and restart memcached server
[6] Memcached wiki 
[7] Wiki: memcached
[8] Obtain memcached source 
[9] memcached 1.4.4 Windows 32-bit binary now available!
[10] Memcached on Windows (x64)
[11] Installing Memcache on Windows
[12] http://www.couchbase.com/


No comments:

Blog Archive

Followers