give your site a boost with memcache
DESCRIPTION
Today's high-traffic websites must implement performance-boosting measures that reduce data processing and reduce load on the database, while increasing the speed of content delivery. One such method is the use of a cache to temporarily store whole pages, database recordsets, large objects, and sessions. While many caching mechanisms exist, memcached provides one of the fastest and easiest-to-use caching servers. This talk will cover memcached and the memcache extension for PHP from setting up a memcached server to using it to provide a variety of caching solutions, including the use of memcached as a session data store.TRANSCRIPT
![Page 1: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/1.jpg)
Ben RamseySeptember 25, 2009
Give Your Site A Boost With Memcache
![Page 2: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/2.jpg)
Why cache?2
![Page 3: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/3.jpg)
To make it faster.3
![Page 4: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/4.jpg)
4
“A cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache. In other words, a cache is a temporary storage area where frequently accessed data can be stored for rapid access.”— Wikipedia
![Page 5: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/5.jpg)
Why cache?
5
You want to reduce the number of retrieval queries made to the databaseYou want to reduce the number of external requests (retrieving data from other web services)You want to cut down on filesystem access
![Page 6: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/6.jpg)
Caching options
6
Flat file cachingCaching data in the databaseMySQL 4.x query cachingShared memory (APC)RAM diskmemcached
![Page 7: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/7.jpg)
What is memcached?
7
Distributed Memory Object Caching SystemCaching daemonDeveloped by Danga Interactive for LiveJournal.comUses RAM for storageActs as a dictionary of stored data with key/value pairs
![Page 8: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/8.jpg)
Is memcached fast?
8
Stored in memory (RAM), not on diskUses non-blocking network I/O (TCP/IP)Uses libevent to scale to any number of open connectionsUses its own slab allocator and hash table to ensure virtual memory never gets externally fragmented and allocations are guaranteed O(1)
![Page 9: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/9.jpg)
General usage
9
1.Set up a pool of memcached servers2.Assign values to keys that are stored in the
cluster3.The memcache client hashes the key to a
particular machine in the cluster4.Subsequent requests for that key retrieve the
value from the memcached server on which it was stored
5.Values time out after the specified TTL
![Page 10: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/10.jpg)
Memcached principles
10
Itʼs a non-blocking serverIt is not a databaseIt does not provide redundancyIt doesn't handle failoverIt does not provide authentication
![Page 11: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/11.jpg)
Memcached principles
11
Data is not replicated across the clusterWorks great on a small and local-area networkA single value cannot contain more than 1MB of dataKeys are strings limited to 250 characters
![Page 12: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/12.jpg)
Storing data in the pool
12
Advantage is in scalabilityTo fully see the advantage, use a “pool”memcached itself doesn't know about the poolThe pool is created by and managed from the client library
![Page 13: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/13.jpg)
www 2
memcached
www 1
www 3memcachedmemcached
13
![Page 14: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/14.jpg)
Deterministic failover
14
When one server goes down, the system fails over to another server in the poolMemcached does not provide thisSome memcache clients provide failoverIf you canʼt find the data in memcache, eat the look-up cost and retrieve from your data source again, storing it back to the cache
![Page 15: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/15.jpg)
www 2
memcached
www 1
www 3memcachedmemcached
www 3
memcached
Data inaccessible!
Recreate data;Store back to memcache
15
![Page 16: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/16.jpg)
The memcached protocol API
16
Storage commands:set, add, replace, append, prepend, cas
Retrieval command: get, gets
Deletion command: delete
Increment/decrement: incr, decr
Other commands: stats, flush_all, version, verbosity, quit
![Page 17: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/17.jpg)
$> telnet localhost 11211Trying ::1...Connected to localhost.Escape character is '^]'.set foobar 0 0 15This is a test.STOREDget foobarVALUE foobar 0 15This is a test.ENDquitConnection closed by foreign host.$>
17
![Page 18: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/18.jpg)
Setting it up
18
http://danga.com/memcached/$> ./configure; make; make install
$> memcached -d -m 2048 -p 11211
Done!Windows port of v1.2.4 at http://www.splinedancer.com/memcached-win32/
![Page 19: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/19.jpg)
Memcached clients
19
Perl, Python, Ruby, Java, C#C (libmemcached)PostgreSQL (access memcached from procs and triggers)MySQL (adds memcache_engine storage engine)PHP (pecl/memcache or pecl/memcached)
![Page 20: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/20.jpg)
pecl/memcache
20
The PHP client for connecting to memcached and managing a pool of memcached servershttp://pecl.php.net/package/memcache$> pecl install memcache
Stable: 2.2.3Beta: 3.0.1
![Page 21: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/21.jpg)
21
![Page 22: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/22.jpg)
Features of pecl/memcache
22
memcache.allow_failovermemcache.hash_strategymemcache.hash_functionmemcache.protocolmemcache.redundancymemcache.session_redundancy
![Page 23: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/23.jpg)
pecl/memcache interfaceMemcachePool::connect()MemcachePool::addServer()MemcachePool::setServerParams()MemcachePool::get()MemcachePool::add()MemcachePool::set()MemcachePool::replace()MemcachePool::cas()
23
![Page 24: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/24.jpg)
pecl/memcache interfaceMemcachePool::append()MemcachePool::prepend()MemcachePool::delete()MemcachePool::increment()MemcachePool::decrement()MemcachePool::setFailureCallback()
24
![Page 25: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/25.jpg)
Key hashing
25
Keys longer than 250 characters are truncated without warningGood practice to hash your key (with MD5 or SHA) at the userland level to ensure long keys donʼt get truncatedKeys are “global”Use something to uniquely identify keys, e.g. a method signature or an SQL statement
![Page 26: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/26.jpg)
Object serialization
26
Objects are serialized before being stored to memcache:get keyVALUE key 1 59O:8:"stdClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";s:3:"quz";}END
Extension unserializes them before returning the objectOnly objects that can be serialized safely can be stored to memcache, i.e. problems with DOM, SimpleXML, etc.
![Page 27: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/27.jpg)
Redundancy and failover
27
memcache.redundancy & memcache.session_redundancyImplement redundancy at the userland level?Again, memcache is not a database
![Page 28: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/28.jpg)
Extending MemcachePool
28
Implement global values vs. page-specific valuesEnsure a single instance of the MemcachePool objectDo complex key hashing, if you so chooseSet a default expiration for all your dataAdd all of your servers upon object instantiation
![Page 29: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/29.jpg)
Database techniques
29
Create a wrapper for mysql_query() that checks the cache first and returns an array of database resultsExtend PDO to store results to the cache and get them when you execute a statement
![Page 30: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/30.jpg)
Database techniques
30
For large datasets, run a scheduled query once an hour and store it to the cachePlease note: memcached can store arrays, objects, etc., but it cannot store a resource, which some database functions (e.g. mysql_query()) return
![Page 31: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/31.jpg)
Session storage
31
As of 2.1.1, you can set the session save handler as “memcache” and all will work automagicallysession.save_handler = memcachesession.save_path = "tcp://192.168.1.10:11211,tcp://192.168.1.11:11211,tcp://192.168.1.12:11211"
Store sessions to both the database and memcacheWrite your own session handler that stores to the database and memcache
![Page 32: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/32.jpg)
www 3
memcached
www 2
memcached
www 1
memcached
Session inaccessible!
Need to recreate the
session!
32
![Page 33: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/33.jpg)
33
For more information...http://danga.com/memcached/http://pecl.php.net/package/memcachehttp://pecl.php.net/package/memcached http://www.socialtext.net/memcached/
![Page 34: Give Your Site a Boost with Memcache](https://reader034.vdocument.in/reader034/viewer/2022051514/54b67dab4a79597f768b4744/html5/thumbnails/34.jpg)
Thank YouSlides available for download at benramsey.com.
Ben RamseySenior Software ArchitectSchematichttp://www.schematic.com/
34