Website Performance

Edge Caching Explained: Cache Keys, TTLs and Purging

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Edge caching means storing copies of your responses on servers located close to your visitors, at the "edge" of a CDN's network, instead of generating or fetching them from your origin server every time. When a visitor in Cairo requests a page and a nearby edge location already has it, the response travels a few hundred kilometres instead of thousands, and your server does no work at all. Two concepts decide whether that happens: the cache key and the TTL. Understanding both is the difference between a CDN that serves most of your traffic and one that forwards nearly everything to your origin.

How a request flows through an edge cache

  1. The visitor's DNS lookup or anycast routing sends them to a nearby edge location.
  2. The edge builds a cache key from the request.
  3. If an object with that key exists and is still fresh, the edge returns it: a hit.
  4. If not, it requests the object from your origin (a miss), stores it if the headers allow, and returns it.
  5. When an object expires, the edge may revalidate it with the origin using ETag or Last-Modified, which can return a small 304 Not Modified instead of the full body.

For the general idea of hits and misses, see cache hit vs miss.

The cache key

The cache key identifies an object. By default most CDNs build it from the scheme, host, path and query string. Anything that ends up in the key creates a separate copy.

RequestSame object?
/style.css and /style.css?v=2No, if query strings are in the key
/page?utm_source=a and /page?utm_source=bNo, unless marketing parameters are ignored
/page with gzip and with BrotliSeparate variants via Vary: Accept-Encoding
/page with different cookiesSame object, unless cookies are in the key

Good practice:

  • Strip tracking parameters (utm_*, fbclid, gclid) from the key.
  • Sort query parameters if your CDN supports it, so ?a=1&b=2 equals ?b=2&a=1.
  • Add a header or cookie to the key only when the response really differs, for example a language cookie on a site that serves different languages at the same URL.

TTL: how long an object stays fresh

TTL (time to live) is how long an edge may serve an object without asking the origin. It usually comes from your headers:

  • Cache-Control: max-age=N applies to browsers and shared caches.
  • Cache-Control: s-maxage=N applies only to shared caches such as CDNs and overrides max-age there.
  • private or no-store tells shared caches not to store the response.
  • Many CDNs also let you override TTLs with rules in their dashboard.

A practical starting point:

ContentSuggested edge TTL
Hashed CSS, JS, fonts, images1 year, immutable
Non-hashed images and media1 day to 1 week
Public HTML pages1 to 10 minutes, purge on publish
Public API responsesseconds to minutes, depending on freshness needs
Logged-in pages, carts, admindo not cache at the edge

Example Nginx configuration for public HTML served by an application:

location / {
    proxy_pass http://127.0.0.1:3000;
    add_header Cache-Control "public, max-age=0, s-maxage=300, stale-while-revalidate=60" always;
}

The browser revalidates every time, while the edge keeps the page for five minutes.

Things that silently disable edge caching

  • A Set-Cookie header on the response. Many CDNs refuse to cache it, and caching it could leak a session.
  • Cache-Control: private or no-cache added by a framework by default.
  • Authorization headers on the request.
  • A Vary: * or Vary: Cookie header.

Check what your origin sends with:

curl -sI https://example.com/ | grep -iE 'cache-control|set-cookie|vary|age'

Purging and invalidation

When content changes before its TTL ends, you purge it. Options from cheapest to most disruptive:

  1. Versioned URLs: a new file name means no purge is needed.
  2. Purge by URL: remove a specific page after editing it.
  3. Purge by tag or prefix: supported by some CDNs, useful for "all pages of category X".
  4. Purge everything: a last resort, since every edge then hits your origin at once.

A full purge on a busy site can send a wave of misses to a small server. Purge selectively and keep your origin sized for that moment. Read more in cache invalidation strategies.

Security notes

Never let personalized responses reach a shared cache. Make sure account pages, carts and anything behind a login send Cache-Control: private, no-store, and test it by requesting the page twice from different browsers. A misconfigured edge cache that serves one user's page to another is a data leak, not just a bug.

The origin still matters

Edge caching reduces load, but every miss, revalidation and uncacheable request reaches your origin. A VPS in a region near your main audience keeps those requests fast. You can launch one from the deploy page.

FAQ

What is the difference between edge caching and browser caching?

Browser caching stores a copy on one visitor's device and helps only that visitor on repeat visits. Edge caching stores a copy on CDN servers shared by everyone in a region, so the first visit of a new visitor can also be fast. s-maxage controls the edge, max-age controls both.

How long should my edge TTL be?

Use long TTLs, up to a year, for files with a hash in their name, and short TTLs of minutes for HTML that changes. If you can purge reliably when you publish, you can use longer TTLs for HTML too. Personalized content should not be cached at the edge at all.

Why does my CDN show a miss on every request?

Usually because the origin sends private, no-cache or Set-Cookie, because the query string or a cookie is part of the cache key, or because traffic is spread across many edge locations that each need their first miss. Inspect the response headers first.

Related guides