How caching works
On this page
The edge stores a file the first time a viewer asks for it and serves every later request from that copy until it expires, is purged, or is evicted to make room on a full cache. What is stored, and for how long, is decided by your origin's headers and the zone's Cache expiration time, in a fixed order.
What is stored#
Only a successful answer to a GET or HEAD: a 200, or a 206 from an origin answering the byte range we asked for. An error, a 404 or a redirect (unless Follow redirects is on, on a standard origin) is relayed to the viewer as it came and never stored, so a file that was missing for a minute is fetched again on the next request rather than staying missing. The one exception is a bucket origin: a private bucket's 401 or 403 becomes our 502 page, and every other error from a bucket, public or private, keeps its status with a one-line body: see Bucket origins. Files up to 10 MB are stored whole; larger ones are stored in 5 MB pieces as they are watched, up to 100 GB, and a file beyond that is passed through. Those sizes are binary (10 MB is 10,485,760 bytes); the statistics and the invoice use decimal units. See Video and large files.
How long a copy lives#
The edge decides when it stores a file, and again each time it refreshes an expired copy, from the zone's setting and your origin's headers at that moment. The first rule that applies wins.
- A response with
Cache-Control: no-storeorCache-Control: privateis never stored, whatever the zone is set to. - A response with a
Varyheader naming anything other thanAccept-Encodingis never stored: we cannot keep one copy per value of a header we do not key on. Three exceptions:Vary: Cookie, allowed when Strip response cookies is on, because the cookie never reaches your origin;Vary: Accepton an image, which is kept once per format (see Images in more than one format); andVary: OriginwithAccess-Control-Request-MethodandAccess-Control-Request-Headers, what a CORS set-up sends (see CORS and the cache). Only the firstVaryheader on a response is read, so send one rather than several. - A response that sets a cookie is never stored, unless Strip response cookies is on and the cookie is removed.
- Cache expiration time set to Do Not Cache: nothing on the zone is stored.
- Cache expiration time set to a fixed time: the copy lives that long, overriding
max-age,s-maxage,Expiresandno-cachefrom your origin, playlists included. - Cache expiration time set to Respect Origin Cache-Control, which is the default:
no-cache: not stored.s-maxageif present, otherwisemax-age: the copy lives that many seconds. Zero means not stored.- Otherwise
Expires: until that time, measured against our clock. A date in the past means not stored. - Otherwise the defaults in the table below, chosen by the file's type.
Two things sit on top of that. Under Respect Origin Cache-Control, an HLS or DASH manifest (.m3u8, .mpd) whose lifetime came from Expires or the defaults is capped at 10 seconds, so a playlist that changes is picked up quickly; give it a max-age to set your own figure, and note that a fixed Cache expiration time applies to playlists with no cap. And a lifetime is fixed at the moment of storing: changing Cache expiration time afterwards does not shorten or lengthen the copies already in the cache, so purge the zone when you need the new value at once.
| When the origin says nothing | The copy lives |
|---|---|
| HLS and DASH manifests | 10 seconds |
| JSON under an API-shaped path (/api/, /v1/, /v2/, /v3/, /graphql) | 5 minutes |
| Video, audio and downloads (archives, installers, PDFs, binaries) | 7 days |
| Images | 24 hours |
| Stylesheets, scripts, fonts and other web assets | 1 hour |
| A file of unknown type of 10 MB or more | 7 days |
| Anything else | 1 hour |
Directives that make no difference: public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate, stale-if-error and Pragma. An Age header from your origin is not subtracted. Only the first Cache-Control header on a response is read, and a viewer's own Cache-Control or Pragma is ignored, so a browser's hard refresh is served from the cache like any other request.
What a viewer's request can do#
- A request carrying an
Authorizationheader bypasses the cache and goes to your origin, and its answer is not stored. - A request carrying a
Cookiedoes the same with Strip response cookies off. With it on, the cookie is dropped and the request is served from the cache like any other. - A request with
If-None-MatchorIf-Modified-Sincegets a304when the cached copy matches. A file that is not cached yet is answered in full. - A request with a
Rangeheader is served that range from the copy, fetching only the pieces it needs. - A WebSocket handshake goes to your origin and, once your origin accepts it, is relayed both ways; an answer typed
text/event-streamis never stored and reaches the viewer as your origin writes it. See Server-sent events and WebSockets. - A
POST,PUT,PATCH,DELETEorOPTIONSis passed to your origin with its body, of any size, and never cached; a POST is refused with 403 instead while Block POST requests is on, and a bucket origin refuses all five with 405 (see Bucket origins). Any other method is answered 405 on every zone.
What makes two requests the same file#
A copy is filed under the zone, the path and the query string. The path is case-sensitive and read after percent-decoding, so /A.mp4 and /a.mp4 are two files and /my%20film.mp4 and /my film.mp4 are one. By default the query string is taken exactly as written, so /intro.mp4?v=2 is a different file from /intro.mp4 and from /intro.mp4?v=3, and ?a=1&b=2 is different from ?b=2&a=1; the zone's Query string setting below changes that. The hostname joins the key only when Forward host header is on; otherwise every hostname on the zone shares one copy. HTTP and HTTPS share it too.
Images in more than one format#
If your origin picks an image's format from the browser's Accept header (AVIF to browsers that take it, WebP to those that take that, JPEG or PNG to the rest) and says so with Vary: Accept, the edge keeps one copy of the image per format: AVIF, WebP, and one for every other client. Each copy is fetched from your origin with a standard Accept for its format, never the visitor's own, so every visitor gets the format their browser asked for. The first request for such an image goes to your origin and is not stored, which is how the edge learns that the image varies; every request after that is served from the copy for the visitor's format. An image your origin does not vary keeps one copy for everybody, as before. Only images are kept this way: a page or an API that answers differently by Accept is still never stored. Purging the URL removes every format's copy.
CORS and the cache#
A file your origin answers with Vary: Origin is stored once and served to every visitor. The copy is the one your origin gives a request without an Origin, because the edge never sends one when it fetches a file to keep. When a visitor's browser sends an Origin, the edge asks your origin, once, which CORS headers that site is given, and sends the copy with them: Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Access-Control-Expose-Headers and Timing-Allow-Origin, with Vary: Origin. The answer is kept for five minutes, so a change to your CORS rules reaches visitors within that time. A site your origin does not allow gets none of them, and its browser refuses the read, as it would from your origin.
A bucket's CORS rules are asked once per site for the whole bucket, whether or not the store sends Vary. A file the Add CORS headers switch already sends with Access-Control-Allow-Origin: * needs no answer and keeps it.
Query strings#
The Query string card, on the zone page's Caching tab, decides how much of the query string, the part of a link after the ?, makes a different cached copy. A player's cache buster (?_=1760612345) or a tracking tag (?utm_source=newsletter) otherwise stores the same file once per value and sends each one to your origin.
| Position | What it does |
|---|---|
| All | Each different query string is a copy of its own. Every zone starts here. |
| None | The query string is ignored: /video.mp4?t=12 and /video.mp4 are one copy, fetched from your origin without the query string. |
| Only these | Only the parameters you list make a different copy, in any order (?w=300&h=200 and ?h=200&w=300 are one copy); the rest are ignored and left off when we fetch from your origin. |
- Parameter names are case-sensitive, up to 64 characters of letters, digits and
. _ - ~ [ ], and up to 20 of them. - A request we never cache keeps its whole query string on the way to your origin: one carrying a cookie or an
Authorizationheader, or aPOSTor other method. - On a token-authenticated zone the token's own parameters are never part of the key, whatever the setting.
- Changing the setting starts new copies under the new key; a file purge clears the copy under either.
Compression#
A viewer whose browser accepts Brotli (br) is sent Brotli for text, whether or not your origin compresses: HTML, CSS, JavaScript, JSON, XML, SVG, plain text, HLS and DASH playlists, subtitles and fonts other than WOFF and WOFF2. A viewer that accepts gzip but not Brotli, such as an Android app, a TV's video player or another server calling your API, is sent gzip for the same files. It is always on and there is nothing to set. A request's Accept-Encoding is sorted into one of four buckets (plain, gzip, brotli, and brotli-only for a client that refuses gzip), each bucket holds its own copy, and each is fetched from your origin with a matching Accept-Encoding. A file that is never compressed (video, audio, images, archives) is the same bytes in every bucket, so it is fetched and stored once, whichever bucket asks first.
- A file of 10 MB or less that is stored is compressed once, when it is fetched, and every later viewer in that bucket is sent the stored Brotli or gzip copy. A gzip file from your origin is re-encoded as Brotli the same way, and sent to gzip viewers as it came.
- A response that is not stored (a page sent with a cookie, one marked
no-store) is compressed as it is sent, whether your origin sent it uncompressed or as gzip. - Never touched: your origin's own Brotli (or any coding but gzip), a response marked
Cache-Control: no-transform, a file over 10 MB, a range your origin answers, anything but a200, an event stream, a body under 256 bytes, and a type that is compressed already (PNG, JPEG, GIF, WebP and AVIF images, video, audio, WOFF and WOFF2, archives) or not given at all. - A viewer that accepts neither Brotli nor gzip is sent what your origin sent.
A response compressed at the edge carries Content-Encoding: br or gzip, Vary: Accept-Encoding and your origin's ETag made weak (W/"..."), because the bytes are no longer the ones your origin tagged. A weak tag still answers If-None-Match, so browsers and the edge revalidate as before. Bandwidth is counted on the compressed bytes sent.
Serving stale while a refresh runs#
When a copy has expired it is not thrown away. For up to 24 hours past its expiry it is served straight away, marked HIT, while the edge asks your origin in the background whether the file has changed, with If-None-Match or If-Modified-Since where your origin gave it an ETag or a Last-Modified. A 304 renews the copy. A 200 replaces a file stored whole; a file stored in pieces is dropped instead when the refresh shows it changed, or when it has neither an ETag nor a Last-Modified to check with, and the next request fetches it afresh. If your origin cannot be reached, the stale copy keeps serving until the 24 hours are up, so a slow or absent origin is not seen by viewers of a file the edge holds; a piece of a large file the edge never fetched still needs your origin. If your origin answers the refresh with a 5xx error, it is treated as down: the stale copy keeps serving, and the refresh is tried again no sooner than 10 seconds later, so a struggling origin is not asked once per viewer. If it answers with any other error, a 404 or a 403, the copy is dropped and the next request fetches afresh. Past 24 hours the copy is discarded and the next request is a miss. Replacing a file at your origin changes nothing at the edge until then: either purge it, or give the new version a new name or query string.
Caching strategy#
When a location does not hold a file a visitor asks for yet, it can fetch it from another of our locations that already holds it, rather than from the zone's Home PoP or your origin. The Caching strategy card, on the zone page's Caching tab, decides how readily it does so, with three positions: Speed, Balanced and Origin savings. Every zone is on Balanced unless you change it, and the setting is offered on every origin type.
| Position | Another location's copy is used |
|---|---|
| Speed | Only when it is clearly faster: at least 20% and at least 10 ms faster than fetching from your Home PoP or origin |
| Balanced (the default) | Unless it is much slower: when it takes at most 1.5 times as long as fetching from your Home PoP or origin, or at most 30 ms longer, whichever allows more. Your origin is asked less, with little effect on speed |
| Origin savings | Always: the nearest location holding a copy, however far away it is, so your origin is asked as little as possible |
The times are measured continuously by our network from real traffic. Until a path has been measured, Speed does not use another location, Balanced uses one only when it is known to be very close, and Origin savings uses any location holding the file.
On a miss the order is: another location that holds the file, as the strategy allows; then the Home PoP, if the zone has Origin shield (Home PoP) on; then your origin. The Home PoP setting is separate and works as before: with it on, a miss that no other location can answer still goes through the Home PoP.
A file served from another location's copy is a cache hit, since your origin was never asked: it reads X-CDN-Cache: HIT, it is not in Fetched from your origin, and the cache hit ratio counts it as from our cache (see Statistics). Traffic between our locations is free: you are billed only for the bytes delivered to your visitors, as always.
It applies only to cached content, the files your origin lets us cache under the rules above. A request that is never cached behaves exactly as before: a method other than GET and HEAD, an upload, a response marked private or no-store, and a request passed straight through because of a cookie or an Authorization header.
A copy fetched from another location keeps its age: it expires when it would have expired where it was first fetched, because its lifetime counts from your origin's answer, not from the second fetch. A purge reaches every location, and a location that has not yet applied a purge you made never hands the purged copy to another location.
What a hit sends back#
A response served from the cache carries a fixed set of your origin's headers: Content-Type, Content-Encoding, Content-Language, Content-Disposition, Cache-Control, Expires, ETag, Last-Modified, Vary, the Access-Control-* headers, Timing-Allow-Origin, Cross-Origin-Resource-Policy, Content-Security-Policy, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, Link and X-Robots-Tag, the first value of each, plus an Age saying how long ago the copy was fetched from your origin or last refreshed; Link is replaced by the canonical one while Add canonical headers is on. A header your origin sends that is not on that list is seen on the request that fetched a file of 10 MB or less from your origin and not on the hits that follow, and never on a larger file, whose every response is built from the stored set; Alt-Svc, Age and Server from your origin are never relayed. So a custom header your application relies on belongs on a page that is not cached, not on a cached file. Headers has the full list of what we add and what your origin receives.
Reading what happened#
Every response carries X-CDN-Cache: HIT was served from a copy, this location's or one another of our locations held, fresh or being refreshed; MISS was fetched from your origin on this request, or could not be stored (the edge's own error pages read MISS too); BYPASS was passed straight through because of the request itself, a cookie, an Authorization header, or a method other than GET and HEAD. A file that reads MISS on every request is being refused by one of the rules above, and your origin's Cache-Control is the first thing to check.