Difference between revisions of "Memcached"
m (Fix flush all command syntax) |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
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 [https://github.com/memcached/memcached/wiki/TutorialCachingStory Caching Story] that explains how every web application eventually turns to caching to increase performance. | 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 [https://github.com/memcached/memcached/wiki/TutorialCachingStory Caching Story] that explains how every web application eventually turns to caching to increase performance. | ||
− | See Also | + | == Memcached commands == |
+ | Dump all the keys in memcached | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | memdump --servers=localhost | ||
+ | </syntaxhighlight> | ||
+ | simpleSAMLphp.session.5c88998d9c84839323e37f563ba75fbd | ||
+ | |||
+ | GET a specific key value | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | echo 'get simpleSAMLphp.session.5c88998d9c84839323e37f563ba75fbd' | nc 127.0.0.1 11211 | ||
+ | </syntaxhighlight> | ||
+ | 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) | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | echo 'flush_all' | nc 127.0.0.1 11211 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ==See Also== | ||
*[https://github.com/memcached/memcached/wiki/Commands Memcache Commands] | *[https://github.com/memcached/memcached/wiki/Commands Memcache Commands] | ||
*MediaWiki has a diagnostic tool for interacting with memcached in the 'maintenance' directory: [https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/maintenance/mcc.php mcc.php] | *MediaWiki has a diagnostic tool for interacting with memcached in the 'maintenance' directory: [https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/maintenance/mcc.php mcc.php] | ||
*[https://stackoverflow.com/questions/8420776/how-do-i-view-the-data-in-memcache Viewing data in memcache] | *[https://stackoverflow.com/questions/8420776/how-do-i-view-the-data-in-memcache Viewing data in memcache] | ||
+ | |||
+ | Try <syntaxhighlight lang="bash">echo stats | nc 127.0.0.1 11211</syntaxhighlight> | ||
<blockquote>"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 </blockquote> | <blockquote>"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 </blockquote> | ||
Line 26: | Line 49: | ||
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. <ref>https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/includes/libs/objectcache/BagOStuff.php</ref> WANCache builds on top of '''BagOStuff''', which is the lower level key-value interface to Memcached and other storage backends. | 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. <ref>https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/includes/libs/objectcache/BagOStuff.php</ref> WANCache builds on top of '''BagOStuff''', which is the lower level key-value interface to Memcached and other storage backends. | ||
− | |||
[[File:Wikipedia_Memcached_flow_2022.png|alt=illustration of Memcached flow from MediaWiki|right|955x955px|Memcached flow from MediaWiki]] | [[File:Wikipedia_Memcached_flow_2022.png|alt=illustration of Memcached flow from MediaWiki|right|955x955px|Memcached flow from MediaWiki]] | ||
− | + | ||
+ | {{References}} | ||
[[Category:Caching]] | [[Category:Caching]] | ||
[[Category:Performance]] | [[Category:Performance]] | ||
− |
Latest revision as of 07:37, 3 April 2024
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.
Contents
Memcached commands[edit | edit source]
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[edit | edit source]
- Memcache Commands
- MediaWiki has a diagnostic tool for interacting with memcached in the 'maintenance' directory: mcc.php
- Viewing data in memcache
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[edit | edit source]
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[edit | edit source]
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.
References[edit source]
- ↑ https://wikitech.wikimedia.org/wiki/Memcached_for_MediaWiki#Mcrouter
- ↑ https://engineering.fb.com/2014/09/15/web/introducing-mcrouter-a-memcached-protocol-router-for-scaling-memcached-deployments/
- ↑ https://github.com/facebook/mcrouter/wiki
- ↑ https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/includes/libs/objectcache/BagOStuff.php