What a CDN changes about page speed, and what it cannot fix
A content delivery network (CDN) is a group of servers spread across many cities that keep copies of your website's files. When a visitor loads a page, the CDN sends those files from the server nearest to them instead of from your main host, which may be far away. Good CDN practice comes down to three jobs: store the right files, keep each one for the right amount of time, and measure results from where your visitors actually are.
A CDN cannot fix a slow origin server, oversized images, or scripts that block the page from showing. It moves bytes more efficiently, but it cannot make a slow page build itself faster. If your origin takes two seconds to generate a page, every cache miss passes that delay along to the visitor. That is why the steps below start with what gets cached and how it is labeled, not with which provider to pick.
This educational guide describes settings that are common across providers such as Cloudflare, Amazon CloudFront, Akamai and Fastly. Menu names differ between them, and defaults change over time, so treat the setting names here as a starting point and check them against each provider's current documentation.
Key Takeaways
- Give static files such as logos, fonts and versioned scripts long cache lifetimes, and keep personal or logged-in pages out of the shared cache.
- Choose edge locations and origin shielding based on where your real visitors live, since a large network count matters less than nearby coverage.
- Change filenames when content changes instead of relying on broad purges, because purging everything sends all traffic back to your origin at once.
- Check that Brotli or gzip compression and modern image formats are enabled, and confirm them by looking at the response headers.
- Measure cache hit ratio and time to first byte from several regions over repeated tests, because a single test result can mislead.
Setting cache rules by file type
Static files are the easiest place to gain ground. Logos, fonts, stylesheets and JavaScript bundles rarely change, so they can carry a long Cache-Control lifetime set by a max-age value. The trick is to include a fingerprint, such as a hash in the filename, so that when you change the file, the browser and CDN treat it as a new file. Without that, you either wait for old copies to expire or force a purge.
Dynamic pages need more care. Carts, account pages and search results often change per visitor. Many CDNs skip caching any response that sets a cookie, and that default protects users, but it can also stop you from caching pages that do not contain personal data. A header called Vary tells the CDN which request details, such as language or device type, change the response. Set it carefully, because each extra value splits your cache into more copies and lowers the hit rate.
The table below shows a common starting pattern. Your own settings may differ depending on how your site is built.
| File type | Typical caching approach | What to check |
|---|---|---|
| Versioned scripts and styles | Long max-age, often months | Filename changes with each release |
| Images and fonts | Long max-age, with a fingerprint or version path | Cookies are not set on these requests |
| Public HTML pages | Short edge cache, sometimes seconds to minutes | No personal data in the response |
| Logged-in or cart pages | Usually not cached at the edge | Cache-Control marked private or no-store |
Where edge servers sit and how origin shielding works
Edge servers are the points of presence that hold your cached copies. A CDN with many locations can help, but only if those locations are near your visitors. Look at your analytics to see which countries and cities send the most traffic, then check whether your provider's plan includes edge locations in those regions. Coverage can depend on the pricing tier, so confirm it before you assume it.
Origin shielding adds a middle caching layer between the edge servers and your origin. When many edges miss the cache at the same time, the shield collects those requests and sends only one to your server. This protects a slow origin from traffic spikes. Shielding usually adds a small amount of latency on a cache miss, so it tends to suit sites where the origin is the bottleneck.
Protocol support is another setting to check. HTTP/2 and HTTP/3 let the browser load many files over one connection, which often helps pages with lots of small assets. Most major providers offer these options, but they may be off on some plans or in some dashboards, so look at the protocol your responses actually use rather than assuming it is on.
Cache lifetimes, purging and stale content
Long cache lifetimes are only useful if you can update content when it changes. There are two main methods. The first is versioned filenames, which avoid purging entirely. The second is a purge request, either from the dashboard or through an API, which removes a specific URL or a group of URLs tagged together. Purge by URL or tag wherever possible, because purging the whole cache forces a rush of cache misses.
Purges do not always take effect everywhere at once. Propagation time across edge servers varies by provider and by how many locations hold the file, so test a change from more than one region after you purge. If a stale copy still appears, check whether a browser cache or an intermediate proxy is holding the old version.
Some directives help the CDN keep serving content while it refreshes in the background. stale-while-revalidate lets the cache return an older copy while it fetches a new one, and stale-if-error lets it serve a copy when the origin is failing. Support for these directives differs between providers, so confirm they are recognized before you rely on them.
Compression and modern image formats
Text files such as HTML, CSS and JavaScript compress well. Brotli usually gives smaller files than gzip for the same content, and most modern browsers accept it. Check the Content-Encoding response header to see which method is used. If you see neither, compression may be off at the origin or at the CDN, and turning it on is often a quick check in the provider's settings.
Images are often the largest files on a page. WebP and AVIF usually produce smaller files than JPEG or PNG at similar visual quality. Browser support differs, so many sites serve the newer format with a fallback rather than replacing the old one. Some CDNs can resize and convert images at the edge on request, which avoids storing many copies at different sizes. Resizing to the size the page actually displays is often more useful than any format change.
Measuring performance from where visitors really are
Test from several regions, not just one. Tools such as WebPageTest let you choose a test location, and running the same page from different places shows whether your edge coverage is working. Google PageSpeed Insights and Lighthouse report on load metrics, including Largest Contentful Paint, but they run a single simulated test, so repeat them to see the spread of results.
Real user monitoring, often called RUM, collects timing data from actual visitors' browsers. It shows how pages perform across devices, networks and countries in a way a lab test cannot. Pair it with your CDN's logs, which can show the cache hit ratio. A low hit ratio usually means cache rules are too strict, Vary headers are splitting the cache too much, or pages contain query strings that make every URL unique.
Keep an eye on cost as well. CDN billing often depends on data transferred and the number of requests, and rates can differ by region. Changes that improve hit ratio usually reduce origin load and sometimes reduce bills, but the effect depends on your traffic pattern and plan.
Common mistakes that slow down a CDN setup
The most common problem is a cookie set on static files. A tracking script or session cookie that rides along with image requests can stop the CDN from caching those files. Check the Set-Cookie header on asset URLs, and move any cookie-setting to pages where it is needed.
Another frequent issue is caching HTML that contains user-specific content, such as a name in the header. This can show one visitor's details to another. Confirm that personal pages are marked private or no-store. The reverse mistake, marking everything as no-cache, removes most of the benefit of the CDN.
When something looks wrong, a command-line tool such as curl can show response headers. Running curl -I against a URL reveals the Cache-Control value and the cache status header that each provider adds, such as X-Cache or CF-Cache-Status. Compare those results with what you expected. Many problems that look like slow servers are really cache misses caused by a single header.
Frequently Asked Questions
Will a CDN make my website load quicker for everyone?
A CDN helps most for visitors who are far from your origin server and for files that can be cached. It does little for visitors who are already close to your host or for pages that change on every request. The gain depends on your traffic mix, your cache rules and where your edge locations sit, so measure before and after a change.
Why is my cache hit ratio low even though I turned on caching?
Common causes include cookies on asset requests, a Vary header that lists too many values, and query strings that make each URL unique. Look at the cache status header on several requests to find which files are missing the cache. Your provider's logs or analytics may show the hit ratio by path, which helps narrow the cause.
Should I purge the whole cache after I update my site?
Usually not. Purging everything sends every request back to your origin at once, which can slow the site during the refill. Versioned filenames avoid purges for static files, and targeted purges by URL or tag cover the rest. Test from more than one region afterward, because propagation can take a short time to finish.
Do I need to convert all my images to WebP or AVIF?
Not necessarily. Newer formats often produce smaller files, but browser support differs, so many sites serve them with a fallback to JPEG or PNG. Resizing images to the size they display at can matter as much as the format. Check how your provider handles format negotiation before you replace originals.