Académique Documents
Professionnel Documents
Culture Documents
Jimmy Guerrero
Sr Product Marketing Manager - Sun Microsystems, Database Group
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 1
• Sun – MySQL Overview
• Introduction to memcached
• Use Cases
• Solutions
• Next Steps plus Q & A
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 2
About MySQL
• 12 years old
• 400+ employees
• 750 partners
• 60K downloads/day
• Customers across every major
operating system, hardware
vendor, geography, industry,
and application type
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 3
Established & Emerging Companies
Web 2.0
Enterprise 2.0
craigslist
SaaS
Telecom
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 4
Introduction to memcached
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 5
“Cache is King”
Browser Cache
ms Memcached Cache
Disk Storage
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 6
What is Memcached?
“A high-performance, distributed memory object caching
system, generic in nature, but intended for use in speeding up
dynamic web applications by alleviating database load” *
* http://www.socialtext.net/memcached/index.cgi?faq
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 7
What is Memcached?
• Runs wherever RAM is available
– Application, Web, Database or dedicated memcached servers
– Low CPU utilization
– Multiple operating systems and architectures supported
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 8
Why Use Memcached With MySQL?
• Enables massive scale-out of dynamic web-sites
• Faster page loads
• Allows for more efficient use of existing database resources
• Can easily utilize idle computing resources
• Dozens to hundreds of nodes can be supported in a
memcached cluster
• No interconnect or proprietary networking required
• Extensible and customizable
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 9
How Does Memcached Work?
Hash Function
• A hash is a procedure for turning data into a small integer
that serves as an index into an array
• Speeds up table lookup or data comparison tasks
Memcached
• Two-stage hash, like a giant hash table looking up key =
value pairs
• Client hashes the key against a list of servers
• When the server is identified, the client sends its request
• Server performs a hash key lookup for the actual data
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 10
Basic Memcached Example
X Y Z
Client X
mc mc mc
1) set key “foo” with value “seattle”
2) hashes the key against server list
hash server list get key
3) Server B is selected
select server connect 4) connects to Server B and sets key
connect get value
set key value
Client Z
5) get key “foo”
6) connects to Server B
ms ms ms
7) requests “foo” and gets value “seattle”
A B C
key = value
foo = seattle
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 11
Memcached & MySQL
read write
Web Servers
Application
Servers
Memcached
Clients mc mc mc
Memcached
Servers ms ms ms
cache update
MySQL Server
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 12
Use Cases
http://mashraqi.com
http://mysqldatabaseadministration.blogspot.com
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 13
Fotolog Introduction
• World’s largest photo blogging community
– > 18 million members
– > 155 million page views
– > 550 million photos
• 4th largest social network (based on traffic)
• Ranked top 57 site
– 13th before Alexa changed ranking algorithm
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 14
Memcached @ Fotolog
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 15
Memcached Getting Started
• libevent:
– Install
• wget http://monkey.org/~provos/libevent-1.4.5-stable.tar.gz
• tar -xzvf libevent-1.4.5-stable.tar.gz
• cd libevent-1.4.5-stable
• ./configure
• make
• make install
• memcached
– Install
• wget http://www.danga.com/memcached/dist/memcached-
1.2.5.tar.gz
• tar -zxvf memcached-1.2.5.tar.gz
• ./configure; make; make install
– Installs in /usr/local/bin/memcached
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 16
Memcached Getting Started
• Starting memcached
– /usr/local/bin/memcached -p 112111 -d -m 50 -c 10
– See all options:
• /usr/local/bin/memcached -h
– In newer versions use –L to “preallocate memory and
request as large memory pages as possible”
• Thanks Trond and Matt!
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 17
Checking Stats
• Escape character is '^]'.
• stats
• STAT pid 20386 // process id
• STAT uptime 1731531 // # of seconds the server has been up
• STAT time 1215055326 // current UNIX time
• STAT version 1.2.2 // version
• STAT pointer_size 32 // pointer size
• STAT rusage_user 94433.301890 // accumulated user time for this process
• STAT rusage_system 96915.777948 // accumulated system time for this process
• STAT curr_items 13870559 // current number of items stored by the server
• STAT total_items 74108243 // total number of items stored by the server
• STAT bytes 1622142816 // # of bytes used to store items
• STAT curr_connections 433 // # of open connections
• STAT total_connections 1465167 // # of total connections
• STAT connection_structures 1614 // # of connection structures allocated by the server
• STAT cmd_get 20273309141 // # of retrieval requests (cumulative)
• STAT cmd_set 335118914 // # of storage requests (cumulative)
• STAT get_hits 17566792339 // # of keys requested that were present
• STAT get_misses 2706516802 // # of keys requested that were not found
• STAT evictions 0 // # of items removed from cache to store new items
• STAT bytes_read 488383819042 // # of bytes read by server (from network)
• STAT bytes_written 1804307490483 // # of bytes written by server (to network)
• STAT limit_maxbytes 2147483648 // # of bytes that the server can use for storage
• STAT threads 1 // # of worker threads
• END
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 18
Stats Items
• stats items
– Info about item storage per slab class
– STAT items:<slabclass>:<stat> <value>
• Output
– STAT items:1:number 2799134 // # of items stored class
including expired items
– STAT items:1:age 1703686 // age of oldest item in
// LRU (LRU is on a per
// slab basis)
– STAT items:3:number 6410968
– STAT items:3:age 1711349
– STAT items:4:number 4660462
– STAT items:4:age 1705032
– END
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 19
Stats Slabs
• stats slabs
– Info about size and memory usage
– STAT <slabclass>:<stat> <value>
– slabclass
• Output
– STAT 1:chunk_size 80 // amount of space used by each chunk
– STAT 1:chunks_per_page 13107 // # of chunks within each page (default page: 1MB, slabs allocated
// per page then chunks are allocated)
– STAT 1:total_pages 214 // # of pages allocated to the slab class
– STAT 1:total_chunks 2804898 // total # of chunks allocated to the slab class
– STAT 1:used_chunks 2804897 // # of chunks allocated to items
– STAT 1:free_chunks 1 // # of chunks not yet allocated or freed after a delete call
– STAT 1:free_chunks_end 5737 // # of chunks that are free at end of last allocated page
– STAT 3:chunk_size 128
– STAT 3:chunks_per_page 8192
– STAT 3:total_pages 822
– STAT 3:total_chunks 6733824
– STAT 3:used_chunks 6414485
– STAT 3:free_chunks 319339
– STAT 3:free_chunks_end 3517
– STAT 4:chunk_size 160
– STAT 4:chunks_per_page 6553
– STAT 4:total_pages 712
– STAT 4:total_chunks 4665736
– STAT 4:used_chunks 4665734
– STAT 4:free_chunks 2
– STAT 4:free_chunks_end 5271
– STAT active_slabs 3
– STAT total_malloced 1832839072
– END
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 20
Stats Sizes
• stats sizes
– Information about general size and count of all items in
cache
– Locks up the cache
– <size> <count>
• Output
– 64 2136
– 96 2797013
– 128 6410943
– 160 4660601
– END
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 21
Memcached Client
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 22
Starting Memcached
• frank$ /usr/local/bin/memcached -p 11211 -d -m 50 -c 10 -vv
• slab class 1: chunk size 88 perslab 11915
• slab class 2: chunk size 112 perslab 9362
• slab class 3: chunk size 144 perslab 7281
• slab class 4: chunk size 184 perslab 5698
• slab class 5: chunk size 232 perslab 4519
• slab class 6: chunk size 296 perslab 3542
• slab class 7: chunk size 376 perslab 2788
• slab class 8: chunk size 472 perslab 2221
• …
slab class 17: chunk size 3600 perslab 291
• slab class 18: chunk size 4504 perslab 232
• …
slab class 38: chunk size 391224 perslab 2
• slab class 39: chunk size 489032 perslab 2
• <4 server listening
• <5 server listening
• <6 send buffer was 9216, now 233016
• <6 server listening (udp)
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 23
Telnet Example
• No expiration
– set mc_webinar1 1 0 6
farhan
STORED
get mc_webinar1
VALUE mc_webinar1 1 6
farhan
END
• Expiration
– set mc_webinar2 1 5 3
– usa
– STORED
– get mc_webinar2
– VALUE mc_webinar2 1 3
– usa
– END
– get mc_webinar2
– END
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 24
Basic Example
• <?php
• require_once 'memcached-client.php';
• $mc01 = new memcached(array('servers' =>
array('localhost:11211'), 'debug' => false,
'compress_threshold' => 10240, 'persistent' => true));
• $mc01->add('key',array(‘1',‘2‘,’3’));
• $val=$mc01->get('key');
• ?>
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 25
Caching an Object
• $user->user_name=“frank";
• $user->url="http://mashraqi.com";
• $user->friend[]="Matt";
• $user->friend[]="Jimmy";
• $user->friend[]="Trond";
• $mc01->add(“user:".$user->user_name,$user);
• $value = $mc01->get(“user:".$user->user_name);
• var_dump($value);
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 26
Caching an Object (contd)
• Produces:
– object(stdClass)(3) {
– ["user_name"]=>
– string(5) "Frank"
– ["url"]=>
– string(19) "http://mashraqi.com"
– ["friend"]=>
– array(3) {
– [0]=>
– string(4) "Matt"
– [1]=>
– string(5) "Jimmy"
– [2]=>
– string(5) "Trond"
– }
– }
• Log
– <7 new client connection
– <7 add key_Frank 1 0 150
– >7 STORED
– <7 get user:frank
– >7 sending key user:frank
– >7 END
– <7 connection closed.
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 27
Memcached Use Cases
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 28
Non-Deterministic Cache:
Classic Memcached Model
• Check memcached, check database, then populate
memcached
• Previously using classic implementation:
– App servers running memcached on “spare” memory
• 90% CPU usage
• Memory garbage collected nearly once a minute
• Experienced blocking on memcached on app servers
• 45 memcached instances dedicated to non-
deterministic cache
– Each instance (on average):
• ~440 gets per second
• ~40 sets per second
• ~11 gets/set
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 29
Non-Deterministic Cache:
Classic Memcached Model
• Ideal for:
– Caching objects that are to be queried several times
– Replacement for MySQL’s query cache
– Caching relationships and other lists
– Slow data that’s used across many pages
– Don’t cache if its more taxing to cache than you’ll save
– Tag clouds and auto-suggest lists
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 30
State Caching
• “Who’s online?”
• ~9G dedicated
• Multiple instances
• Ideal for caching:
– Sessions
– Current state of users
• Especially for IM applications
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 31
Example of classic caching model
• <?php
// initialize memcached
// …
// check memcached
$row = $mc01->get(“user:".$user->user_name);
// if no row found, get object from database
if ($row == NULL) {
$sql = “SELECT * FROM user WHERE
user_name=‘frank’ ”;
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
// Now populate memcached
$mc01->add("key_".$row->user_name,$row);
}
• ?>
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 32
Deterministic Cache
• Memcached has what database has…read scalability
• Multiple dedicated cache pools
• Cache has to be populated when the site “starts”
– Maintained by application from that point onwards
– ~ 90,000 gets / second across cache cluster
– ~ 300 sets / second
– get/set ratio of ~ 300
• Ideal for caching:
– Heavily accessed data/objects/lists
– User credentials
– User profiles
– User preferences
– Active media belonging to users
– Outsourcing logins to memcached
• Have enough memory:
– exptime of 0 will cause items to never expire
• But they still may be deleted
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 33
Pre-loading deterministic cache
• <?php
// initialize memcached
// …
// while rows are found, populate memcached
$sql = “SELECT … FROM user u, preferences p
WHERE u.username = p.username ”;
$result = mysql_query($sql);
while ($row = mysql_fetch_object) {
// Now populate memcached
$mc01->add(“user:".$row->id,$row);
}
• ?>
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 34
Proactive Caching (binlog)
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 37
Proactive Caching
• memc_servers_set
– Takes a comma separated list of host/ip:port
– select memc_servers_set(‘localhost:11211’);
– select memc_servers_set(‘localhost:11211’,
‘localhost:11212’,’localhost:11213’);
• memc_set
– select memc_set(‘user:frank', ‘http://mashraqi.com');
• memc_get
– select memc_get(‘user:frank’);
• Returns ‘http://mashraqi.com’
• If no value exists in memcached, NULL is returned
• memc_delete
– select memc_delete(‘user:frank’);
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 40
UDF Usage Examples
• memc_increment
– select memc_set('seq', 1);
– select memc_get('seq');
• Returns 1
– select memc_increment('seq');
• Returns 2
– select memc_increment('seq', 2);
• Returns 4
– select memc_decrement('seq');
• Returns 3
• Consult the README file for more examples
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 41
Add-on/Replacement for NFS File System Cache
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 42
Add-on/Replacement for NFS File System Cache
• Example:
– // initialize memcached
// read contents
$contents = @file_get_contents(“/path/to/xml/on/nfs/file”);
// store the contents in memcached
$mc01->add(“file:xml",$contents);
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 43
Partial Page Caching
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 44
Example Partial Page Caching
• <?php
require_once 'memcached-client.php';
$mc01 = new memcached(array('servers' =>
array('localhost:11211'), 'debug' => false,
'compress_threshold' => 10240, 'persistent' =>
true));
$html_content=“<div>… Sidebar HTML Content
Really Taxing to Generate…</div>”;
$mc01->add(‘partial:sidebar’,$html_content);
$row01=$mc01->get(‘partial:sidebar’);
?>
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 45
Application-Based Memcached Replication
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 46
Memcached replication using client
mc
mc
mc
mc
mc
Application server
mc
• <?php
require_once 'memcached-client.php';
$mc01 = new memcached(array('servers' =>
array('localhost:11211'), 'debug' => false,
'compress_threshold' => 10240, 'persistent' => true));
$mc02 = new memcached(array('servers' =>
array('localhost:11212'), 'debug' => false,
'compress_threshold' => 10240, 'persistent' => true));
$user->user_name=“frank”;
$user->url=“http://mashraqi.com”
$mc01->add(‘user:‘.$user->user_name,$user);
$mc02->add(‘user:’.$user->user_name,$user);
$row01=$mc01->get(‘user:‘.$user->user_name);
$row02=$mc02->get(‘user:‘.$user->user_name);
?>
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 49
Ending thoughts
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 50
Thanks!
• Brian Aker
• Patrick Galbraith
• Brad Fitzpatrick
• Matt Ingenthron
• Trond Norbye
• Jimmy Guerrero
• Rich Taylor
• Alex Roedling
• Andrey Kartashov
• Adam Lee
• And all others who contribute to memcached and
MySQL
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 51
Resources
• Memcached
– Home Page:
• http://danga.com/memcached/
– Memcached functions for MySQL
• http://tangent.org/586/Memcached_Functions_for_MySQL.html
– Memcached Client APIs
• http://danga.com/memcached/apis.bml
– Presentation/Notes
• http://download.tangent.org/talks/Memcached%20Study.pdf
• http://www.igvita.com/2008/04/22/mysql-conf-memcached-internals/
• http://www.slideshare.net/vishnu/livejournals-backend-a-history-of-
scaling
• libmemcached
– http://tangent.org/552/libmemcached.html
• Libevent
– http://monkey.org/~provos/libevent/
• Protocol
– http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.
txt
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 52
Solutions
Jimmy Guerrero
Sr Product Marketing Manager
Sun Microsystems - Database Group
jimmy@mysql.com
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 53
Memcached for MySQL
• Support is built into your MySQL Enterprise subscription
http://www.mysql.com/products/enterprise/memcached.html
• MySQL Enterprise
– 24x7 Production Support
– Enterprise Monitor
– MySQL Enterprise Server
– Additional Add-ons Available
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 54
Why Memcached with MySQL?
• Enables massive scale-out of dynamic web-sites
• Faster page loads
• Allows for more efficient use of existing database resources
• Can easily utilize idle computing resources
• Dozens to hundreds of nodes can be supported in a
memcached cluster
• No interconnect or proprietary networking required
• Extensible and customizable
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 55
Next Steps
Memcached for MySQL -
http://www.mysql.com/products/enterprise/memcached.html
Whitepapers - http://www.mysql.com/why-mysql/white-papers/
Documentation -
http://dev.mysql.com/doc/refman/6.0/en/ha-memcached.html
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 56
Questions?
Jimmy Guerrero
Sr Product Marketing Manager - Sun Microsystems, Database Group
jimmy@mysql.com
Copyright 2008 MySQL AB The World’s Most Popular Open Source Database 57