LESSON 05 · Cache and capacity

Make the read path cheap

Most traffic resolves existing keys, so optimize lookup without making cache the source of truth.

SYSTEM VIEW

The system

Diagram view
Redirects—
Cache hits—
DB reads—

Watch redirects come from memory, fall back to the database, and stay right for brand-new links.

  1. A visitor opens x7Kp2Qa. The Load balancer may pick any redirect server, since none keeps data another lacks. That server finds the mapping in the Mapping cache and answers 302 with a Location header.

    What changed

    • GET /x7Kp2Qa
    • Cache hit
    • 302 · Location
  2. A visitor opens p4Nz8Lc, which the Mapping cache doesn’t hold. The server reads it from the Mapping database, the durable authority, stores it in the cache and answers 302. The next request for p4Nz8Lc hits.

    What changed

    • Cache miss
    • 1 database read
    • p4Nz8Lc cached
  3. Bv3Yd7n doesn’t exist: the cache misses, the Mapping database finds nothing and the visitor gets 404. The server caches that “not found” as a negative entry, so repeats skip the database. It lasts seconds, since a later creation or recovery may change the answer.

    What changed

    • 404 Not Found
    • Negative entry for Bv3Yd7n
    • Kept for seconds
  4. Popular links create skew: a small set of keys may get most reads. At an example 8,700 redirects a second, a 90% hit rate leaves the Mapping database about 870 reads a second. The 5 million hottest mappings, 200 bytes each, take about 1 GB plus overhead.

    At 99% hits
    about 87 reads/s
    Fits in
    one Redis or Memcached node

    What changed

    • 8,700 redirects/s
    • 9 in 10 from memory
    • 870 database reads/s
  5. Before adding read replicas or sharding, which splits data across databases by key, measure redirect traffic, the cache hit rate and database lookup latency. Sharding pays when storage or write demand outgrows one database tier, not because traffic sounds large.

    Measure
    traffic · hit rate · latency
    Shard when
    storage or writes outgrow

    What changed

    • Traffic: 8,700/s
    • Hit rate: 90%
    • Lookup latency
  6. The Read replica, a read-only copy of the Mapping database, now answers cache misses. Hn5Vq2c was just written to the primary, but the replica applies it a moment too late, so its first visitor gets 404 for a link that exists.

    What changed

    • Read replica
    • Hn5Vq2c on the primary
    • 404 for a live link
  7. When recent-key lookups read the primary Mapping database instead, Hn5Vq2c resolves on its first redirect and fills the cache. Filling the cache after the durable write also works. Either way, negative entries should expire in seconds, so a “not found” from the lag can’t linger.

    What changed

    • Recent keys read the primary
    • Hn5Vq2c cached
    • 302 · Location
  8. A cache adds a part to run, a staleness window after edits and a new failure mode. At 8,700/s it keeps database reads near 870. Try 200/s, an example below a few hundred reads a second: an indexed Postgres lookup is often fast enough alone.

    Traffic

Space plays · ← → step · Home resets

THE IDEA

How it works

Place stateless redirect servers behind a load balancer. Stateless means a server keeps no data that another server lacks, so any of them can answer any request. On a cache hit they read the key-to-destination mapping and return the redirect. On a miss they query persistent storage, fill the cache, and respond. A missing key should not be cached forever because a future creation or recovery may change the answer; a remembered “not found”, called a negative cache entry, should last seconds, not hours.

Popular links create skew: a small set may receive most reads. As an example, at 8,700 redirects per second, a 90% hit rate leaves about 870 reads per second for the database, and 99% leaves about 87. Holding the 5 million hottest mappings at 200 bytes each takes about 1 GB plus overhead, well within one Redis or Memcached node.

Measure cache hit rate, database lookup latency, and redirect traffic before adding replicas or partitioning by key. A replica is a read-only database copy that can expand reads but may lag a new write; a just-created link should resolve reliably through the chosen consistency path. Sharding, which splits data across databases by key, becomes useful when storage or write demand exceeds one database tier, not merely because traffic sounds large.

A cache is not free. It adds a component to run, a staleness window after edits, and a new failure mode. Below a few hundred reads per second, an indexed lookup in Postgres is often fast enough without one.

DECISION POINT

A newly created link returns 404 for a few seconds while reads use lagging replicas. What should change?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Cache mappings for read speed while the database remains durable authority.
  • Treat the first redirect after creation as a consistency requirement.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

On a redirect cache miss, where should the service obtain the authoritative destination?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.