Red de Distribución de Contenido

CDN · Caché de borde

Escrito por Darjus Kazakevic · revisado el

Cache-Control headers are the API; purging is the expensive part.

A content delivery network (CDN) is a set of cache servers in hundreds of cities. A request goes to the nearest edge server. On a miss, the edge fetches the response from your origin, stores it, and serves the next users from its copy. CloudFront, Cloudflare, Fastly and Akamai are common CDNs.

Latency drops, because the edge may be 10 ms away instead of 150 ms. Origin load drops, because most requests stop at the edge. The main cost is staleness: a wrong header serves old or private content, and a purge takes time to reach every edge.

En esencia — Serve cacheable responses from servers near the user, and let the Cache-Control headers from your origin decide what is stored and for how long.

Cuándo recurrir a él

Recurre a él cuando

  • Static assets for users spread across countries or continents
  • Public pages many visitors share that can be a minute old
  • Large downloads or video that would overload your origin

Reconsidéralo cuando

  • Responses that differ per user and must never be shared
  • Content that must change the instant you publish, under a fixed URL
  • All users sit near one data center and traffic is small

Conceptos clave

Edge and origin
The origin is your server or storage bucket, the source of truth. Edges are the CDN's servers near users. The hit ratio is the share of requests answered at the edge; aim for 90% or more on static assets.
Pull vs push
A pull CDN fetches from the origin on the first miss and caches the result; this is the default. With a push CDN you upload files ahead of time, which suits large downloads such as video or installers.
Cache key
What makes two requests count as the same for the cache: usually host, path and query string. Every header or cookie you add to the key splits the cache into smaller parts and lowers the hit ratio.
Cache-Control
The response header that tells the CDN and the browser what to do. Every cache follows max-age. Shared caches such as a CDN follow s-maxage instead, when it is set. With private or no-store, the CDN keeps no copy.
Invalidation (Purge)
Removing content before its TTL ends, by path or by tag. It works, but it takes seconds to minutes to reach every edge, and some CDNs charge per path. Versioned file names avoid most purges.

Reglas de diseño

Version file names instead of purging
Put a content hash in each asset name, like app.3f9a1c.js, and cache it for a year as immutable. A new build means new URLs. Keep purges for mistakes, not for deploys.
Mark anything personal as private
A CDN shares whatever the headers allow. A response that differs per user needs Cache-Control: private or no-store, or one user's page can be served to the next.
Keep the cache key small
Include only what changes the response. Every extra query parameter, header or cookie in the key splits the cache and sends more requests to the origin.
Serve stale while you refresh
Add stale-while-revalidate and stale-if-error. Users get the cached copy at once while the edge refreshes it, and pages stay up if the origin is down.

Errores habituales

  • A long max-age on index.html. Users keep loading old HTML that points at deleted assets, so the app breaks after each deploy.
  • A logged-in page cached because the origin sent no Cache-Control. The next visitor sees the first user's name and data.
  • Cookies or every query parameter in the cache key. The hit ratio falls from 95% to 30%, and the origin load comes back.
  • Purging on every deploy instead of versioning files. Deploys get slower, and some edges serve the old file for minutes.