What a cache is
Producing a web page costs time: the server runs code, queries the database, and the bytes travel across the network. A cache keeps the result so the next request can skip some or all of that work.
Two words describe every cache. A hit is a request answered from the stored copy. A miss is a request the cache could not answer, so it went further back to fetch the response, usually storing it for next time. The share of requests that are hits, the hit ratio, is the main measure of how well a cache works.
Every cache also has to answer one question: how long is the copy still good? That is its freshness, set by headers such as Cache-Control or by the cache's own settings.
Browser cache: copies on the visitor's device
The browser keeps images, stylesheets, scripts and sometimes whole pages on the visitor's disk or in memory. When the visitor opens another page of your site, or comes back tomorrow, files that are still fresh load without any network request at all. Nothing is faster.
The browser cache is private: it serves only that person on that device. That makes it safe for responses that are personal to the visitor, as long as the header allows it. Its downside is that you cannot reach into it. A copy a browser holds stays there until it expires or the visitor clears it, which is why long browser lifetimes belong on files whose name changes when their content does.
Server-side caches: less work on your origin
Your own server can cache at several levels:
Page cache. The finished HTML of a page is stored and sent again without running the application. WordPress page cache plugins and framework response caches work this way.
Object cache. Results of expensive operations, such as database queries or API calls, are kept in memory with tools like Redis or Memcached, so the application builds pages faster.
Opcode cache. PHP's OPcache keeps compiled code in memory so scripts are not compiled on every request.
Reverse proxy cache. A proxy such as nginx or Varnish in front of the application stores whole responses.
These caches make your origin faster, but every request still has to travel to your server and back. For a visitor far from it, the distance remains.
CDN cache: shared copies close to visitors
A CDN puts edge servers between your visitors and your origin. When a visitor requests a file, the nearest edge answers from its cache if it has a fresh copy; otherwise it fetches the file from your origin, sends it to the visitor and keeps a copy for the next one.
The CDN cache is shared: one copy at the edge serves every visitor who asks for the same URL. A popular image might be fetched from your origin once and then served from the edge thousands of times. That cuts the distance for visitors and the load on your server at the same time. It also means a shared cache must never hold a response meant for one person, such as a logged-in account page.
How a CDN works end to end is in What is a CDN?.
How the layers stack on one request
Follow one request for /images/hero.jpg:
1. The browser looks in its own cache. If the copy is fresh, it is done: no request leaves the device. 2. Otherwise the request goes to the nearest CDN edge. A hit is answered from there, within a short distance of the visitor. 3. On a miss, the edge asks your origin. A reverse proxy or page cache there may answer without running the application. 4. Only if every layer misses does your application build the response, perhaps with help from the object cache.
Each layer protects the one behind it. A good setup lets the browser keep files that never change, lets the edge keep public content for as long as it is valid, and leaves only truly dynamic or personal requests to your server.
# Age: how many seconds the copy has been in a shared cache
# X-Proxy-Cache-MT: HIT, MISS... at the cdn.com.tr edge
curl -sI https://www.example.com/images/hero.jpg | grep -i "cache-control\|age\|x-proxy-cache-mt"
Who decides how long: Cache-Control
The response header Cache-Control is how your origin tells every cache how long a copy is good. max-age sets the lifetime for all caches, s-maxage sets it for shared caches such as the CDN only, private keeps a response out of shared caches, and no-store keeps it out of every cache.
A common pattern is a long lifetime for versioned static files, a short one for HTML, and private or no-store for personal pages. The directives and recipes are explained in Cache-Control and max-age, and the free Cache-Control header generator builds the exact header for each kind of content.
When content changes: purge and versioned file names
A cache that holds a copy for a day will show yesterday's version for a day, unless you tell it otherwise. There are two tools for that.
Purge. You ask the CDN to remove a URL, a folder or everything from the edge, and the next request fetches the new version. See purging the CDN cache.
Versioned file names. Build tools add a hash to file names (app.3f9a2c.js). A new version gets a new name, so the old copy can stay cached for a year without ever being served for the new content. This is the only reliable way to refresh files already in visitors' browsers, because you cannot purge a browser.
What should not be cached
Keep personal and changing responses out of shared caches: account and order pages, carts and checkout, admin areas, and API responses that depend on who is asking. Send Cache-Control: private or no-store from those paths, or write a rule at the CDN that does not cache them.
A response that sets a cookie is usually personal too. That is why many caches, including ours, do not store a response that carries Set-Cookie.
Caching on cdn.com.tr
In the panel each delivery rule under Domains & Rules has one of three caching choices:
Follow your origin's cache headers. Your Cache-Control, s-maxage or Expires decides how long the edge keeps the copy, and the header reaches the browser unchanged.
Cache for a fixed time. You set the duration in the panel, useful when you cannot change the application's headers. The header the browser receives is rewritten with your value.
Don't cache this path. For admin areas, carts and anything personal.
Responses carry X-Proxy-Cache-MT (HIT, MISS and so on), so you can see whether the edge answered from cache. When content changes, a purge from the panel, the API or cdnctl clears it on the edge servers. The setup is in cache rules.
Clearing your browser cache vs. purging the CDN
Clearing the cache in your browser or on your phone deletes the copies on your own device only. It is safe: sites simply load a little slower the first time, and you may have to sign in again if you also clear cookies. It does not change what other visitors see.
If you updated your site and visitors still see the old version, the copy is most likely at the CDN edge or in their browsers. Purge the URL at the CDN, and use versioned file names for static files so browsers pick up the new version by themselves.
How to set it up
Step-by-step guides in the help centre:
Web caching FAQ
What does cache mean?
A cache is a stored copy of data kept close to where it is needed, so the next request can use the copy instead of fetching or building it again. On the web, browsers, CDNs and servers all keep caches.
Is the cache the same as cookies?
No. The cache stores copies of files and pages to load them faster. Cookies are small pieces of data a site stores to remember you, such as a login session or a cart.
Is it safe to clear my cache?
Yes. Clearing the cache only deletes stored copies; pages load a little slower the first time afterwards. Your accounts and settings are kept unless you also clear cookies and site data.
Why do I still see the old version of my site?
A copy is still fresh in some cache: your browser, the CDN edge or a page cache on your server. Purge the URL at the CDN, clear your page cache plugin if you use one, and reload the page with the browser cache bypassed.
What is a good cache hit ratio for a CDN?
It depends on the site. Static files such as images, CSS and JavaScript should be served from the edge almost every time; HTML pages that change often or are personal will hit less. Watch the ratio over time and look at the paths that miss most.