Memcached Install, Overview & Benchmarks
What is Memcached? • Really, its Memcache-d (d as in daemon) • TCP/IP service that allows you to store stuff in RAM, somewhat persistently • Basically, a persistent hash table • Super awesome
Getting Started • Download the memcached source code (http://www.danga.com/memcached/) • Download the memcached PHP extension • Extract, configure & compile memcached • Extract configure & compile php extension
Compile Memcached • Standard configure, make, make instal • A few gotchas • Memcached requires libevent (http:// www.monkey.org/~provos/libevent/) • Even once you get libevent, you may need a symlink to get memcached to see it. • I recommend using a --prefix
Considerations • For testing / small needs, run it on your web server and give it a few MB of RAM • Connections are via TCP/IP, so it can run anywhere, but obviously you’ll want to strive for minimal network overhead • There is NO security, repeat, NO security, so if you don’t run it on localhost, use some sort of firewall
Memcache PHP Extension • Memcache (notice there is no D here because we’re not talking about the daemon) is a PECL extension • Either use the “pecl install” feature or download the source code and compile it yourself (then copy to ext/ folder and add to php.ini)
So Now What? • Understand what memcached will and will not do for you • Understand what and when you should cache • Learn how to use the functions provided by the PHP extension
What Memcached will not do: • Tell you what is stored in it (list keys) • Allow you to wildcard delete keys • Cache very large items effectively • native failover • write contents to disk (planned)
What Memcached will do: • Expire things automatically if you set a TTL • Expire things when it runs out of space (first in - first out) • Tell you general stats (# connections, bytes used, etc) • Persistent connections • Let you flush all the current keys
What should[n’t] you cache? • The more specific a “question” you can ask of the cache, the more benefit you will get • Don’t attempt to cache mysql result sets (many reasons...) • Caching objects or arrays that have had computational work done on them is good • Caching snippets of HTML/XML is better • Prevailing wisdom is that objects in cache should be less (way less) than 1MB
How to know what’s cached? • Create an index in a database table • Create an index in memcache... • Don’t care, just trust memcache_get() to return false and handle it appropriately
Premature Optimization • Don’t cache just because it sounds neat • Fix your database schema & algorithms first • If a script is too slow to run in your test environment, caching is not a long term solution for you
So how fast is it? • It’s all relative to how much processing you have to do between a database (or other data source) and a user, but this demo shows between 12x and 40x faster than re- processing every time
Recommend
More recommend