← Lessons

quiz vs the machine

Silver1120

System Design

Edge Caching and Cache Keys

Serving responses near users and defining exactly what makes a response unique.

4 min read · intro · beat Silver to climb

Caching at the edge

Edge caching stores responses on servers physically close to users, often in a content delivery network. A request that hits the cache is served from nearby without ever reaching your origin, which makes it faster and takes load off your servers.

The cache key

The cache key is what the cache uses to decide whether two requests are the same. By default it is the URL. But many responses depend on more than the URL.

  • If content varies by language, the accept language header belongs in the key.
  • If responses differ for logged in users, an auth dimension belongs in the key.
  • Leaving out a dimension serves the wrong content to someone.

Beware over keying

Adding too many dimensions, like a unique session id, makes every request unique so nothing is ever shared. The cache hit rate collapses. Choose the smallest set of dimensions that still keeps responses correct.

Key idea

Edge caching serves responses near users, and the cache key must include every dimension that changes the response while excluding ones that needlessly fragment the cache.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the purpose of a cache key?

2. What happens if you include a unique session id in the cache key?