Skip to content
Tutorials Center
My learning

Topics

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

  1. 1 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.
  2. 2 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.
  3. 3 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.
  4. 4 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

  1. 5 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.
  2. 6 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.
  3. 7 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.
  4. 8 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.
  5. 9 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

  1. 10 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.
  2. 11 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.
  3. 12 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.
  4. 13 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.
  5. 14 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

  1. 15 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.
  2. 16 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.
  3. 17 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.
  4. 18 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.
  5. 19 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

  1. 20 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.
  2. 21 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.
  3. 22 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.
  4. 23 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

  1. 24 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.
  2. 25 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().
  3. 26 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.
  4. 27 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

  1. 28 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.
  2. 29 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.
  3. 30 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

  1. 31 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.
  2. 32 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.
  3. 33 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.
  4. 34 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

  1. 35 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.
  2. 36 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.
  3. 37 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.