Title: Learning the basics of Memcached
Slug: memcached-learning-basics
Date: 2022-01-30 23:15:00
Author: Kartones
Lang: en
Tags: Development, Resources, Patterns & Practices, Systems-IT
og_image:
Description: A list of resources and some tips for learning the basics of Memcached.


Quick post to write down some resources to learn a bit about [Memcached](https://www.memcached.org/). I've used it from time to time but never got into learning how it works under the hood (at least an overall view).


### Useful links

[The protocol description](https://github.com/memcached/memcached/blob/master/doc/protocol.txt) is the first and foremost resource you should read. To complement it, I highly encourage reading also [LZone's Memcached Cheat Sheet](https://www.lzone.de/cheat-sheet/memcached).

Regarding how to scale it, [Skyscanner post regarding scaling](https://scribe.rip/@SkyscannerEng/scaling-memcached-cdef01e150a1) was a simple but good introduction, also useful to learn about consistent hashing. 

[Journey to the centre of Memcached](https://scribe.rip/@SkyscannerEng/journey-to-the-centre-of-memcached-b239076e678a) summarizes how the slabs, pages, chunks, and items work. It also helps you to begin using the `stats` command and interpreting the data it returns.

[Oracle's page about memcached general statistics](https://docs.oracle.com/cd/E17952_01/mysql-5.6-en/ha-memcached-stats-general.html) also has some useful tidbits.

[Memcached Command-Line Options](https://docs.oracle.com/cd/E17952_01/mysql-5.6-en/ha-memcached-cmdline-options.html) Explains precisely that, the CLI options just in case you can/need to tweak them.

[Memcached Exporter for Prometheus](https://github.com/prometheus/memcached_exporter) is quite self-descriptive, a way to obtain all metrics from the `stats` command for consumption by Prometheus.

And finally, regarding tooling I'm still checking what's out there, so don't have much to mention. The default [memcached-tool script](https://github.com/memcached/memcached/blob/master/scripts/memcached-tool) is useful, but I like the efficiency metrics that [memcached-tool-ng](https://github.com/davidfoliveira/memcached-tool-ng) provides.


### Side-Notes


- Docker's [official memcached container](https://hub.docker.com/_/memcached/) doesn't contains any configuration file (runs with all the defaults), don't search for one.
- **maximum connections (`-c`)** controls the bandwidth, meanwhile **backlog queue limit (`-b`)** is a single queue for that bandwidth that allows to not have client connection errors when the connections limit is reached.
- **evictions** are bad; expirations, reclaims, etc. are fine.