Open main menu

Memcached

Revision as of 08:37, 3 April 2024 by Admin (talk | contribs) (Fix flush all command syntax)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Memcached is a BSD licensed, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. See the Caching Story that explains how every web application eventually turns to caching to increase performance.

Memcached commands

Dump all the keys in memcached

memdump --servers=localhost
simpleSAMLphp.session.5c88998d9c84839323e37f563ba75fbd

GET a specific key value

echo 'get simpleSAMLphp.session.5c88998d9c84839323e37f563ba75fbd' | nc 127.0.0.1 11211
VALUE simpleSAMLphp.session.5c88998d9c84839323e37f563ba75fbd 0 1879
a:2:{s:9:"timestamp";d:1711046755.552315;s:4:"data";O:18:"SimpleSAML\Session":10:{s:9:"sessionId";s:32:"5c88998d9c84839323e37f563ba75fbd";s:9:"transient";b:0;s:7:"trackid";s:10:"6db0c2e647";s:16:"rememberMeExpire";N;s:5:"dirty";b:0;s:19:"callback_registered";b:0;s:9:"dataStore";a:1:{s:16:"core:errorreport";a:3:{s:8:"509a5645";a:3:{s:7:"expires";i:1711061090;s:7:"timeout";i:14400;s:4:"data";a:7:{s:12:"exceptionMsg";s:174:"SimpleSAML\Error\NotFound: The requested page 'https://localhost/simplesaml/module.php/admin/' could not be found. The module 'admin' was either not found, or wasn't enabled.";s:14:"exceptionTrace";s:0:"";s:8:"reportId";s:8:"509a5645";s:7:"trackId";s:10:"6db0c2e647";s:3:"url";s:46:"https://localhost/simplesaml/module.php/admin/";s:7:"version";s:5:"2.1.1";s:7:"referer";s:7:"unknown";}}s:8:"6f3f88ff";a:3:{s:7:"expires";i:1711061101;s:7:"timeout";i:14400;s:4:"data";a:7:{s:12:"exceptionMsg";s:174:"SimpleSAML\Error\NotFound: The requested page 'https://localhost/simplesaml/module.php/admin/' could not be found. The module 'admin' was either not found, or wasn't enabled.";s:14:"exceptionTrace";s:0:"";s:8:"reportId";s:8:"6f3f88ff";s:7:"trackId";s:10:"6db0c2e647";s:3:"url";s:46:"https://localhost/simplesaml/module.php/admin/";s:7:"version";s:5:"2.1.1";s:7:"referer";s:7:"unknown";}}s:8:"ccb23c67";a:3:{s:7:"expires";i:1711061155;s:7:"timeout";i:14400;s:4:"data";a:7:{s:12:"exceptionMsg";s:174:"SimpleSAML\Error\NotFound: The requested page 'https://localhost/simplesaml/module.php/admin/' could not be found. The module 'admin' was either not found, or wasn't enabled.";s:14:"exceptionTrace";s:0:"";s:8:"reportId";s:8:"ccb23c67";s:7:"trackId";s:10:"6db0c2e647";s:3:"url";s:46:"https://localhost/simplesaml/module.php/admin/";s:7:"version";s:5:"2.1.1";s:7:"referer";s:7:"unknown";}}}}s:12:"associations";a:0:{}s:9:"authToken";N;s:8:"authData";a:0:{}}}
END

Empty the memcached (it doesn't actually delete, but it expires everything)

echo 'flush_all' | nc 127.0.0.1 11211


See Also

Try

echo stats | nc 127.0.0.1 11211

"Cache is King" - borrowed from the aphorism "Cash is King" For a full exploration of object caching in MediaWiki see https://www.mediawiki.org/wiki/Object_cache

Making cache scalable

MediaWiki connects to memcached through a proxy called McRouter (Mick-ROW-ter) [1] Mcrouter was developed by Meta engineering back in 2014 for scaling memcached deployments [2]. It's a core component of cache infrastructure at Facebook and Instagram where mcrouter handles almost 5 billion requests per second at peak.

Because the routing and feature logic are abstracted from the client in mcrouter deployments, the client may simply communicate with destination hosts through mcrouter over a TCP connection using standard memcached protocol. Typically, little or no client modification is needed to use mcrouter, which was designed to be a drop-in proxy between the client and memcached hosts.

Mcrouter supports typical memcache protocol commands like get, set, delete, etc. and specific commands to access stats, version and so on.[3]

MediaWiki Interfaces

WANObjectCache (or WANCache) is the primary interface in MediaWiki for interacting with Memcached and mcrouter. As the name implies, WANObjectCache is a multi-datacenter aware caching interface. See the class docs for more detail on using and deploying WANObjectCache. Source in Gerrit includes a README describing stats that go into StatsD

Aside: WANObjectCache can also be deployed using Dynomite as the intermediary - to use Amazon's Dynamo cache in a distributed way. See the 2007 whitepaper for details on Amazon's Dynamo.

If you are a MediaWiki developer - especially a front-end dev, you've probably seen references in code to the 'BagOStuff' and wondered "what the heck is it?" BagOStuff is an abstract Class representing a cache/ephemeral data store. Concrete classes then build upon or implement BagOStuff in ways that are compatible with those concrete storage mechanisms. [4] WANCache builds on top of BagOStuff, which is the lower level key-value interface to Memcached and other storage backends.

illustration of Memcached flow from MediaWiki

References