Upload
kaplumbaga
View
220
Download
0
Embed Size (px)
Citation preview
8/14/2019 MEMCACHED: What Is It And What Does It Do?
1/16
MEMCACHED: WHAT IS IT
AND WHAT DOES IT DO?Brian Moon
dealnews.com
http://brian.moonspot.net/
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
2/16
@BRIANLMOON
Senior Web Engineer for dealnews.com
Survived a 2006 Yahoo front page link
Founder and lead developer of Phorum
Memcached community member
Gearmand contributor
PHP internals contributor
I used PHP/FI
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
3/16
WHAT IS MEMCACHED?
Dumb daemon
It is a generic key/data storage system
Uses libevent and epoll/kqueue
Caches data in memory
Cache is distributed by the smart clients
memcached is a high-performance, distributedmemory object caching system, generic in nature, butintended for use in speeding up dynamic webapplications by alleviating database load.
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
4/16
PHP OPTIONS
PECL/memcache
Mature
Standalone
More hand holding
PECL/memcached
Built on libmemcached
More of a raw API
Has more features
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
5/16
SIMPLE PHP EXAMPLE
$MEMCACHE = new Memcache();$MEMCACHE->addServer(192.168.0.1);$MEMCACHE->addServer(192.168.0.2);
$mydata = $MEMCACHE->get(mydata);
if($mydata === false){$mydata = generate_mydata();$MEMCACHE->set(mydata, $mydata,
MEMCACHE_COMPRESSED,86400);
}echo $mydata;
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
6/16
WHERE IS MY DATA?
The client (not server) uses a hashing algorithm todetermine the storage server
Data is sent to only one server
Servers do not share data
Data is not replicated
Two hashing algorithms possible: Traditional
Consistent
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
7/16
WHERE IS MY DATA?
Both hash the key and use the result to choose a server.
Traditional hashing uses the forumla:
hash % num_servers
Resulting number determines the server used.
In Consistent hashing, each server is allocated LOTS ofslots and a key is hashed and to a number. The closestslot to that number is the server.
Adding/removing servers from the list results in lesskey reassignment.
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
8/16
WHAT CAN I STORE?
Server stores blobs of data up to 1MB
PHP extensions will serialize non-scalar data
Keys are limited to 250 bytes in length
Keys can not contain spaces or high characters. Stickwith letters, numbers, _ and you are pretty safe.
PECL/memcache will convert keys for you.
PECL/memcached will returns false and an additionalmethod must be used to find out why the set failed.
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
9/16
DATA SIZE MATTERS
Maximum size for one item is 1MB
Both clients support compression, neither by default
Data is stored in slabs based on size Lots of items of the same size is not optimal
Slab size can be customized
May not be able to store items when it appearsthere is free memory
Data can be evicted sooner than expected.
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
10/16
EVICTION AND EXPIRATION
Expiration time can be expressed as seconds from nowor as an absolute epoch time.
Values > 30 days are assumed to be an absolute time
Items are not removed from memory when theyexpire
Items are evicted when newer items need to be stored
Least Recenty Used (LRU) determines what isevicted
Eviction is done per slab
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
11/16
HOW WELL IS IT WORKING?
Graph stats from memcached using Cacti/Ganglia, etc.
Key stats:
Hits/Misses Gets/Sets
Evictions
Cacti Templates: http://dealnews.com/developers/
Wednesday, September 30, 2009
http://dealnews.com/developers/http://dealnews.com/developers/8/14/2019 MEMCACHED: What Is It And What Does It Do?
12/16
HOW DO I SEE THE CACHE?
You have no way to see the cached data.
You probably dont need to see it.
For memcached to tell you, it would freeze your entirecaching system
There are debug ways to see.
DO NOT COMPILE PRODUCTION WITH DEBUGBECAUSE YOU ARE A CONTROL FREAK!
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
13/16
HOW DO I BACK IT UP?
You dont! If you application requires that, you are using it wrong
It is a cache, not a data storage system
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
14/16
NAMESPACES & TAGGING
There is no concept of namespaces or tagging built into memcached
You can simulate them with an extra key storage
See the FAQ for an example of simulated namespaces
This of course means there is no mass delete inmemcached
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
15/16
ADVANCED TIPS
Use multi-gets to increase performance
PECL/memcache takes an array of keys to get()
PECL/memcached has a separate method
Use the binary protocol in PECL/memcached
Group keys with a master key
Use a cache hierarchy
GLOBALS + APC + memcached
Wednesday, September 30, 2009
8/14/2019 MEMCACHED: What Is It And What Does It Do?
16/16
REFERENCES
http://code.google.com/p/memcached/
http://pecl.php.net/package/memcache
http://pecl.php.net/package/memcached
http://brian.moonspot.net/
http://dealnews.com/developers/
S
http://dealnews.com/developers/http://brian.moonspot.net/http://pecl.php.net/package/memcachedhttp://pecl.php.net/package/memcachehttp://dealnews.com/developers/http://dealnews.com/developers/http://brian.moonspot.net/http://brian.moonspot.net/http://pecl.php.net/package/memcachedhttp://pecl.php.net/package/memcachedhttp://pecl.php.net/package/memcachehttp://pecl.php.net/package/memcachehttp://code.google.com/p/memcached/http://code.google.com/p/memcached/