Memcached and Caching Patterns Tutorial
Learn Memcached and Caching Patterns step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Memcached? Learn what Memcached is, what problem it solves, how a cache sits between your app and its database, and what this course will and will not run in your browser.
- Installing and Running Memcached Install Memcached with apt, Homebrew or Docker, learn the command-line flags that matter (-m, -p, -l, -t, -c), and start a server safely bound to localhost.
- The Memcached Text Protocol Speak to Memcached by hand: the line-based text protocol, the set and get formats, response codes like STORED and END, and a Python parser that mimics a server.
- Your First Memcached Client in Python Use Memcached from Python with pymemcache, handle misses and a down server gracefully, and practice with a MiniMemcached stand-in that runs right in your browser.
Caching Concepts
- Why Caching Works Understand why caches speed up systems: the latency gap between memory and databases, repeated reads, read-heavy workloads, and when caching does not help.
- Hit Ratio and Average Access Time Learn how to compute cache hit ratio, miss ratio and average access time, read the stats counters get_hits and get_misses, and set a realistic hit ratio target.
- Locality and Working Sets See why a small cache can serve most requests: skewed popularity (Zipf), the working set, and how hit ratio grows with cache size, with a simulation you can run.
- Eviction Policies: LRU, LFU and FIFO Compare cache eviction policies (FIFO, random, LRU, LFU), implement them in Python, run them on one request trace, and learn how Memcached's segmented LRU works.
- TTL and Expiration Learn how Memcached expiry works: the exptime rules (0, relative seconds, Unix timestamps, negative), lazy expiration, choosing TTLs, and adding jitter to avoid mass expiry.
Memcached Basics
- Storage Commands: set, add, replace, get and delete Master the core Memcached commands set, add, replace, get and delete: what each returns, how they differ, and how to use add as a simple lock or first-writer-wins.
- Append, Prepend, Incr, Decr and Touch Use Memcached's in-place commands: append and prepend for building values, incr and decr for atomic counters, and touch or gat to extend a TTL without rewriting data.
- CAS and Concurrent Updates Prevent lost updates in Memcached with gets and cas: how the CAS token works, what EXISTS means, how to write a retry loop, and where CAS cannot help.
- Keys, Values, Flags and Serialization Learn Memcached's rules for keys (250 bytes, no spaces), values (1 MB), flags, compression and serialization, plus a key-naming convention that avoids collisions.
- Stats and Multi-Get Read the Memcached stats output (get_hits, evictions, curr_items, bytes) to judge cache health, and use multi-get to fetch many keys in one round trip instead of many.
Strategies and Invalidation
- The Cache-Aside Pattern Learn cache-aside, also called lazy loading: check the cache, load on a miss, store the result, and handle writes and negative results correctly, with runnable code.
- Read-Through, Write-Through and Write-Behind Compare caching strategies beyond cache-aside: read-through, write-through, write-around and write-behind, with Python simulations and advice on which fit Memcached.
- Invalidation Strategies Learn how to keep cached data fresh: TTL, explicit delete on write, event-driven invalidation, tag-style groups, and the delete-then-refill race, with Python examples.
- Versioned Keys and Namespaces Invalidate whole groups of Memcached keys at once with a namespace version counter, or version a single entity so stale copies can never be read again.
- Stampede Protection Understand cache stampedes (the dogpile effect) and defend against them with add-based locks, stale-while-revalidate soft TTLs and probabilistic early refresh, all simulated in Python.
Distributed Caching
- Consistent Hashing and the Hash Ring Learn why modulo hashing breaks a cache cluster when a server is added or lost, and how a consistent hash ring moves only a small share of keys. Runnable Python.
- Virtual Nodes and Load Balance Measure how unevenly a one-point-per-server hash ring spreads keys, then fix it with virtual nodes and weights for servers of different sizes. Runnable Python.
- Client-Side Sharding and Server Pools See how Memcached clients shard keys across a pool, batch multi-gets per server, and how proxies like mcrouter and twemproxy move that logic out of the app.
- Hot Keys and Replication Find hot keys that overload one cache server and spread them with key replication, suffix sharding and a tiny local cache. Includes a runnable Zipf traffic simulation.
Consistency and Failure
- Stale Data and Race Conditions Replay the exact interleavings that leave stale values in Memcached after a database update, and learn which write order, TTLs and deletes keep the damage small.
- Cache Stampede Protection Simulate a cache stampede when a hot key expires and compare lock-based single flight, stale-while-revalidate and probabilistic early refresh using Memcached add().
- Node Failure and Failover Learn what happens to hit ratio and database load when a Memcached node dies, and compare ejecting the node, failing fast and using a gutter pool. Runnable simulation.
- Consistency Models and CAS Understand eventual consistency in a cache, how gets/cas gives optimistic locking with a version token, and how leases and versioned keys prevent lost updates.
Performance and Sizing
- Hit Ratio and Metrics Read Memcached's stats output, compute hit ratio, eviction rate and memory use, and learn which numbers to alert on. Includes a runnable stats parser and a hit-ratio simulation.
- Slab Allocator and Memory Sizing Understand how Memcached carves memory into slab classes, how chunk sizes waste space, and how to estimate the RAM a cache needs. Runnable Python slab calculator.
- Eviction Policies and Capacity Planning Compare FIFO, random, LRU and a segmented LRU on skewed traffic with a scan, learn how Memcached picks victims, and plan capacity with TTLs, headroom and growth.
Patterns and Comparisons
- Memcached vs Redis Compare Memcached and Redis on data model, threading, persistence, replication, eviction and operations, then use a decision helper and a data-transfer experiment to choose.
- Multi-Layer Caching and CDNs Stack browser, CDN, in-process and Memcached caches, compute the effective latency and database load of each layer, and keep their TTLs and invalidation under control.
- Serialization and Compression Compare JSON, pickle and compact formats for cached values, measure what zlib compression saves and costs, and handle schema changes and the 1 MB item limit safely.
- Cache Anti-Patterns and Security Avoid common caching mistakes such as missing user IDs in keys, caching errors and unbounded keys, and lock down Memcached: private bind, firewall, no UDP, SASL and TLS.
Projects and Practice
- Project: A Cache Cluster Simulator Build a small Memcached-style cluster in Python: an LRU server with TTLs and stats, a consistent hash ring client, a traffic generator and a node failure experiment.
- Project: A Cache Library with Stampede Protection Build a small caching library with remember(), TTL jitter, negative caching, versioned invalidation, add-based locks and hit counters, then test it against a stampede.
- Practice: Caching Exercises Practice distributed caching with hands-on exercises: capacity math, weighted rings, debugging a stale cache, key design, plus review questions with answers.