Caché Distribuida

Capa Redis · Memcached

Escrito por Darjus Kazakevic · revisado el

Fast reads for every app server, and a new tier that can fail.

A distributed cache is a cluster of in-memory key-value servers that every app instance shares. Redis and Memcached are the usual choices, and cloud services like ElastiCache run them for you. A read takes 0.2–1 ms, against 5–50 ms for a typical database query, so the cache absorbs most read load.

This guide covers the cache as a component: how keys spread across nodes, how much memory it needs, and what breaks. How the app reads and writes through it is in Caching Strategies. The main cost is a new tier that can fail, and a database that now depends on it.

En esencia — A distributed cache is a shared in-memory cluster in front of the database. Plan its memory, its hot keys and the moment it fails, not only its hit rate.

Cuándo recurrir a él

Recurre a él cuando

  • Many app servers read the same hot data and should share one copy
  • The database is overloaded by repeated reads of the same keys
  • Shared state like sessions or rate-limit counters needs sub-ms access

Reconsidéralo cuando

  • One app server, where an in-process cache is simpler and faster
  • Reads are mostly unique, so the hit rate would stay low
  • The data must survive a restart and has no other home

Conceptos clave

Partitioning (Hash slots · client-side hashing)
Keys are spread over nodes by a hash. Redis Cluster splits the key space into 16,384 hash slots and assigns slots to nodes. Memcached clients usually use consistent hashing. Adding a node moves only some keys.
Hit rate
The share of reads the cache answers. At a 95% hit rate the database sees 1 read in 20; at 90%, 1 in 10, twice the load. Small drops in hit rate cause big jumps in load.
Memory and eviction
The cache holds only what fits in RAM. When it is full, it removes keys by a policy, such as allkeys-lru in Redis. Estimate size as keys × (key + value + about 50–100 bytes of overhead).
Hot keys
One key read so often that its node runs out of CPU or network while the others idle. A celebrity profile or a flash-sale product can draw 100,000 reads per second to a single node.
Cold start (Restart · failover)
A node can restart empty. Every key it held becomes a miss at once, and that load goes to the database. Replicas with automatic failover shorten the outage but do not remove it.

Reglas de diseño

Size the database for a cold cache
Know what load the database sees at a 0% hit rate and how long the cache takes to warm up. If the answer is "it falls over", add rate limits or a fallback before you add more cache.
Treat every key as disposable
Never keep the only copy of data in the cache. Any key can vanish through eviction, restart or failover. The app must fall back to the database and still return a correct answer.
Spread hot keys before they overload a node
Watch CPU and network per node, not only the cluster average. For a key above about 10,000 reads per second, add a 1–5 second in-process cache or copy the key to several nodes.
Time out faster than the database
A cache call should time out in a few milliseconds. A slow cache that blocks every request is worse than no cache, so treat a timeout as a miss and read the database.

Errores habituales

  • Sizing the database for the average hit rate, so one cache restart takes the database down too.
  • Running Redis with no maxmemory limit, so it grows until the operating system kills it instead of evicting keys.
  • Running KEYS * in production to find keys. It blocks the server for every client; use SCAN.
  • One hot key holding one node at 100% CPU while the cluster average looks healthy.