Memcached Server Technology2.pdf
-
Upload
flashmxpro -
Category
Documents
-
view
11 -
download
0
description
Transcript of Memcached Server Technology2.pdf
Memcached Technology
Karn SakulsakIT Manager, THiNKNET Co., Ltd.
Agenda
● What is memcached?● Lab: Interactive memcached session● Inside memcached● PHP interface to memcached● Case study: Memcached as PHP session storage
Case study is used for tur
What is memcached?
What is memcached?
● Multi-purpose network memory○ Can store anything○ Can be shared among group of servers
● Plain text TCP protocol● In 2004, Livejournal.com has 28 memcached instances on
10 servers, caching total 30GB of data with 92% hit rate● Benefits
○ Off-load database server○ Boost performance for often used look up tables
● Drawbacks○ No security in memcached protocol so we must secure
it with network policy
What is memcached?
● Installation (Linux)○ sudo yum install memcached
● Usage○ memcached -d -u memcached -m 64 -l 127.0.0.1 -p
11211
Interactive session
Memcached protocol
● Store command○ <command> <key> <flags> <exptime> <bytes>\r\n○ command = set/add/replace/append/prepend/cas
● Retrieval command○ <command> <key>*\r\n○ command = get/gets
● Deletion○ delete <key> [<time>]\r\n
● Increment / Decrement○ <command> <key> <value>\r\n○ command = incr/decr
● Statistics○ stats [items|slabs]
Memcached protocol
● Note: Store command○ <flags> = Per client used such as PHP use value 1 to
indicate that the stored value is Array, 0 for String○ <exptime> 0 = No expiration time (will be destroyed
by manual deletion or eviction)
Inside memcached
Memcached storage schemamemcached allocated memory
page page
slab
slab
Memcached storage schema
● Storage rule○ Each page allocation will not be over 1MB○ Every slabs in the same page have same size○ Each key will be stored in one slab
● So we can store at most "1MB" of data per key● Pages with same-size slab are called "Slab class"● Page size
○ slab size x number of slabs per page <= 1MB● Page will not be deallocated or change slab size after
created ● We can pre set minimum slab size and its growth to be
optimized with our application (via -n, -f)
Memcached retrieval mechanism
● Key indexing○ O(1) hashing algorithm
● No key list function● Multiple memcached server
○ Client module is responsible for server hashing so that retrieval time is O(1) + O(1)
PHP interface
PHP interface with memcached
● Memcached module○ Can be installed via pecl○ sudo yum install php-pecl-memcache
● Usage○ Memcache class
■ Memcache::connect■ Memcache::addServer ■ Memcache::set■ Memcache::get■ Memcache::delete
Sample code$memcache = new Memcache;$memcache->addServer('localhost', 11211) or die ("Could not connect");$memcache->addServer('localhost', 11212) or die ("Could not connect");if($value = $memcache->get($key)) { echo "get $key (".strlen(serialize($value)).")- OK\n";} else { echo "get $key - Not found\n";}if($memcache->set($key, str_pad('', $size, $pad), false, 0)) { echo "set $key - OK\n";} else { echo "set $key - FAIL\n";}
Case study: Memcached as PHP session storage
Memcached as PHP session storage
● What do we have?○ session_set_save_handler○ session_set_cookie_params
● session_set_save_handler○ Customize how session work
■ How to store / retrieve / destroy session data ● session_set_cookie_params
○ Customize parameter of session cookie■ Expiration time■ Path■ Domain
Walk through code snippet
function mcs_start($param) { global $__mcs_memcached; global $__mcs_timeout; ... $__mcs_memcached->connect($host['host'], $host['port']); ... session_set_save_handler("__mcs_open", "__mcs_close", "__mcs_read", "__mcs_write", "__mcs_destroy", "__mcs_gc"); session_set_cookie_params(0, '/', $domain); session_start();}
Walk through code snippet
function __mcs_openfunction __mcs_closefunction __mcs_gc { return true;}
Walk through code snippet
function __mcs_read($id) { global $__mcs_memcached; if(isset($__mcs_memcached)) { if($sess_data = $__mcs_memcached->get(MCS_KEYPREFIX.$id)) { } else { $sess_data = ''; } return $sess_data; } else { return ''; }}
Walk through code snippet
function __mcs_write($id, $sess_data) { global $__mcs_memcached; global $__mcs_timeout; if(isset($__mcs_memcached) && isset($__mcs_timeout)) { return $__mcs_memcached->set(MCS_KEYPREFIX.$id, $sess_data, false, $__mcs_timeout); } else { return false; }}
Walk through code snippet
function __mcs_destroy($id) { global $__mcs_memcached; if(isset($__mcs_memcached)) { return $__mcs_memcached->delete("mcs_$id"); } else { return false; }}
Usage test: set.php
set.php-----------require('lib.mcsession.php'); mcs_start(array( 'server' => 'localhost', 'port' => '11211', 'domain' => '.thinknet.co.th'));$_SESSION['username'] = 'thinknet';echo "USERNAME[".$_SESSION['username']."]";
Usage test: get.php
get.php-----------require('lib.mcsession.php'); mcs_start(array( 'server' => 'localhost', 'port' => '11211', 'domain' => '.thinknet.co.th'));echo "USERNAME[".$_SESSION['username']."]";
Usage test: Run
Output from set.php:USERNAME[thinknet]Output from get.php:USERNAME[]
Why can't we get the username session variable in get.php?!!!
Debug
We found this in our error_log:PHP Fatal error: Call to a member function set() on a non-object in ...
Why?Because __mcs_write (session write) is called after memcached connection object is destroyed!
Debug
Solution
function mcs_end() { session_write_close(); }
Usage:This function, mcs_end, must be called once follow last session-related statement or before script end.
We can modify our mcs_start to register mcs_end as shutdown function for PHP script via "register_shutdown_function". (This mod is implemented in mcsession 0.1.2)
Usage test #2: set.php
set.php-----------require('lib.mcsession.php'); mcs_start(array( 'server' => 'localhost', 'port' => '11211', 'domain' => '.thinknet.co.th'));$_SESSION['username'] = 'thinknet';echo "USERNAME[".$_SESSION['username']."]";mcs_end();
Usage test #2: get.php
get.php-----------require('lib.mcsession.php'); mcs_start(array( 'server' => 'localhost', 'port' => '11211', 'domain' => '.thinknet.co.th'));echo "USERNAME[".$_SESSION['username']."]";mcs_end();
Usage test #2: Run
Output from set.php:USERNAME[thinknet]Output from get.php:USERNAME[thinknet]
CHAIYO!!!
Conclusion
● Benefits○ We can use memcached as shared-session among
group of servers so that our web users can move along server farm without loss session related data.
● Drawbacks○ Session security is compromised by being network-
accessible, we must keep it hidden in our own private network and using firewall protection
○ At current version of mcsession, 0.1.1, we cannot browse session data
Q & A
References
● Fitzpatrick, Brad. memcached: a distributed memory object caching system [homepage on the Internet] Available from: http://www.danga.com/memcached/
● Memcached protocol document [document on the Internet]Available from: http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
● Fitzpatrick, Brad. Distributed Caching with Memcached [document on the Internet] Available from: http://www.linuxjournal.com/article/7451
● PHP Manual [document on the Internet]Available from: http://php.net/manual/en/index.php