# The page your CDN dropped should not wake your container

A CDN keeps a page only as long as someone keeps asking for it, and on a serverless container that is the difference between sleeping and never sleeping.

## In short

- On a 520-page marketing site that worked out at about ten fetches per page per day, which is enough to keep a serverless container awake all day.
- edge.keep_pages puts the pages a release warms in your storage zone, and a small script at the edge answers from there when the cache has lost them.
- Your container stays the fallback, because a storage zone answers its own 404 and bunny.net will not fail over on one.

A serverless container that sleeps is the whole point of serverless. It costs nothing while it is asleep, and your provider starts it again the moment a request comes in. Put a CDN in front of it, cache every page that is the same for everybody, and a marketing site should be asleep most of the day.

Ours was not. A site of 520 pages, every page cacheable, `Cache-Control: public, s-maxage=604800`, and the container ran twenty-four hours a day.

## A cache time is a maximum, not a promise

The warm-up after a release works. Twelve minutes after one, 37 of 40 pages in a sample came back from Origin Shield in about 80 milliseconds. Three hours later, about half of that same sample took 400 to 1000 milliseconds — the time it takes to reach a container, not a cache.

That is not a misconfiguration. A CDN keeps a copy in a point of presence for as long as it is worth keeping, and drops what nobody has asked for in a while, however many days your cache time allows. bunny.net says so plainly in its own documentation. For a popular page that never matters. For the four hundredth page of a knowledge base, asked for twice a week, it matters every few hours.

Measured over a day on that site: about ten fetches per page per day, across 520 pages. Every one of them a request that reached the container and reset its fifteen-minute idle timer. The site never got a quarter of an hour of quiet, so it never scaled to zero.

## The obvious fix does not fit

bunny.net has Perma-Cache: cached files stored permanently on disk instead of being dropped. It is exactly the right shape, and it cannot be used here:

> You cannot use Perma-Cache and Origin Shield simultaneously. These features are mutually exclusive.

Every pull zone Brewless manages has Origin Shield, because one cache in front of your container is worth more than permanent storage behind a hundred. Trading it away to solve this would hand back a worse problem.

## And the storage zone has no fallback

The next idea is to put the pages in a storage zone and point an edge rule at it. That works right up until a visitor asks for a page that is not there. A storage zone answers its own 404. You can point it at a file of your own, and if that file is missing you get the standard 404 back. There is no "and otherwise ask the origin".

The load balancer does not save you either. It fails over on connection errors and on 5xx responses — a 404 is a perfectly successful answer, so it is never a reason to try somewhere else.

So an edge rule would mean every URL your application can serve has to be in that zone, forever, including the ones you have not thought of. That is not a trade worth making for a cache.

## What `edge.keep_pages` does instead

```yaml
environments:
  production:
    edge:
      cache: 7d
      keep_pages: true
```

With it on, the warm-up that already asks for every page of your sitemap after a release keeps what it gets, in the storage zone your built files already use. The pages cost no extra request: those fetches were going to happen anyway.

A small middleware script then sits between the cache and your origin. It runs on `onOriginRequest`, which runs only on a cache miss — exactly the moment the decision has to be made:

- the page is in the zone: answer from there, and the CDN caches that answer as if your container had sent it;
- the page is not in the zone: hand the request on to your container, exactly as before.

That second line is the whole design. There is no 404 to be had, because your application is still the fallback for everything the zone does not have.

On the site in question, a cold point of presence now answers like this:

```
/over          200  X-Brewless-Page: storage   cdn-cache: MISS
/kennisbank    200  X-Brewless-Page: storage   cdn-cache: MISS
```

and an address that does not exist still answers with the application's own 404 page, not with the storage zone's.

## What is never kept

One copy in the zone is served to everybody, so a release leaves out anything that was meant for one person. An answer is not kept when it sets a cookie, when its `Cache-Control` says `private`, `no-store` or `no-cache`, when it does not say `public` at all, or when it is not a page — a redirect, a 404, anything that is not HTML. An address with a query string is not kept either, and the script does not look one up: it may well be answered differently per visitor, so it stays your container's.

Get that line wrong and you have not built a slow page, you have built a leak. It is the part of this with the most tests on it.

## What it costs

bunny.net bills the script at $0.02 per 1000 seconds of processor time and $0.20 per million requests, with the first increment of each charged as soon as it runs. That is $0.22 a month, and it stays there until a month passes a million requests — and the script only runs when the edge misses, which is the thing this setting makes rarer. The pages themselves sit in a storage zone you already have.

## What it does not fix

It gives the edge somewhere to go for your pages. It does nothing about the rest of what reaches a container: form submissions, an MCP endpoint that AI assistants post to, and the long tail of addresses that scanners invent, where every new guess is a cache miss by definition and a 404 that is not worth storing.

If your container still does not sleep after this, those are the next place to look — and the honest way to find out is to read what actually reaches it, not to assume.

## Frequently asked questions

### Why does a cached page come back to my container a few hours after a release?

Because a cache time is a maximum, not a promise. A CDN keeps a copy in a point of presence for as long as it is worth keeping and drops what nobody has asked for in a while, however many days your Cache-Control allows. On a site with many rarely-visited pages that happens every few hours, and each of those fetches resets the fifteen-minute idle timer of a serverless container.

### Why not use bunny.net Perma-Cache for this?

Because it cannot be used together with Origin Shield, and every pull zone Brewless manages has Origin Shield. bunny.net's own documentation says the two are mutually exclusive. One cache in front of your container is worth more than permanent storage behind a hundred points of presence, so trading it away would make the problem worse.

### What happens when a page is not in the storage zone?

The request goes to your container, exactly as it did before. That is the reason this is a script and not an edge rule, because a storage zone answers its own 404 and bunny.net will not fail over on one, because a 404 is a successful response. With the script, your application stays the fallback for everything the zone does not have.

### Can a page with someone's session in it end up in the storage zone?

No. An answer is left out when it sets a cookie, when its Cache-Control says private, no-store or no-cache, when it does not say public at all, or when it is not HTML. One copy in the zone is served to every visitor, so that line is the part of this with the most tests on it.

### What does edge.keep_pages cost?

bunny.net bills the script at $0.02 per 1000 seconds of processor time and $0.20 per million requests, with the first increment of each charged as soon as it runs. That comes to $0.22 a month and stays there until a month passes a million requests. The script only runs on a cache miss, and the pages sit in a storage zone you already have.

Source: https://brewless.eu/blog/page-your-cdn-dropped
