Caching and CDN: Speeding Up Your Website
Forty percent of visitors leave a website that takes longer than three seconds to load. That's not an anecdote, it's a consequence with direct impact on revenue and bounce rate. Caching and a CDN are the two most effective measures to drastically reduce load times.
The technology behind both concepts is not complicated: you store data so you don't have to request or calculate it again each time. But the details of how you do that, at what level, and for what type of content, determine how effective it is.
What is caching?
Caching is temporarily storing data in a faster or closer storage location, so you can return it immediately on the next request without consulting the original source again.
On a web server, this means: databases are not queried again for data that was just requested. PHP code is not re-executed for pages that rarely change. Images are not re-sent if they're already in the browser.
There are multiple caching layers, and they work complementarily. The more layers you use effectively, the less work the server has to do.
The four layers of web caching
Browser cache is the first layer and closest to the visitor. Static files (CSS, JavaScript, images, fonts) are stored by the browser after the first download. On a subsequent visit, they're not fetched again but loaded directly from the local cache. You control this with Cache-Control and Expires headers.
Browser cache relieves your server but has no influence on first load time. It does affect returning visitors and page transitions within the same session.
Object cache (Redis or Memcached) stores database results, computed values, and API responses in memory. For webshops, this is particularly valuable: the product catalog, category pages, price calculations, and search results are built once and then served directly from Redis. The database is barely queried for frequently requested queries.
Redis is faster than Memcached for more complex data structures and supports persistence. In most modern webshop setups, Redis is the standard.
Full-page cache goes a step further: the complete HTML output of a page is cached. When a visitor requests the homepage, it's not built from database queries and PHP code, but returned directly as a complete HTML string. Varnish is the most commonly used tool for this. It can handle tens of thousands of requests per second with a modest server configuration.
The downside of full-page caching is that it doesn't work for personalized content: shopping carts, logged-in users, specific offers. That requires a split approach where the base structure is cached but dynamic elements are loaded via JavaScript (ESI or AJAX).
Opcode cache is specific to PHP. PHP code is re-interpreted with every execution unless you enable OPcache. OPcache stores the compiled bytecode in memory, so PHP scripts are immediately executable. This is a small but consistent performance improvement that requires virtually no configuration. In any production PHP environment, OPcache should be enabled.
Caching layers and their impact
Percentages are guidelines with correct configuration for static or lightly dynamic pages.
What is a CDN?
A Content Delivery Network is a network of servers spread across multiple locations worldwide. When a visitor requests a file, the CDN delivers that file from the server that is geographically closest to them, instead of from your original server.
For a Dutch webshop with visitors in the Netherlands, a CDN might not make a dramatic difference for load times of the HTML itself, but it does make a big difference for static assets: images, CSS, JavaScript, and videos. These are downloaded from the original server to CDN edges (PoPs, Points of Presence) and cached there. The CDN then serves them extremely fast with every subsequent request.
Besides speed, a CDN also offers an availability advantage: it absorbs part of the traffic spikes before they reach your server. During a sudden traffic spike from social media or a news article, that's a significant buffer.
What do you cache on a CDN?
The rule of thumb: anything that is the same for every visitor can be cached on a CDN.
- Images (JPG, PNG, WebP, SVG)
- CSS and JavaScript files
- Fonts (WOFF2)
- Videos and other media files
- Static HTML pages (for SSG setups)
- PDFs and downloadable documents
What you don't cache on a CDN: pages with session-dependent content, checkout flows, logged-in user pages, and API calls that return personal data. For those, you set cache rules based on cookies or headers, so those requests always pass through to the original server.
Cache invalidation: the hardest part
Caching has one fundamental problem: when the original content changes, the cache doesn't automatically know. A customer sees an outdated product price, a CDN edge serves an image that has already been replaced, Redis returns a category page that is no longer accurate.
Cache invalidation, the controlled clearing of the cache when content changes, is for this reason one of the more complicated aspects of caching strategies. The common approaches:
- TTL (Time To Live): cached items expire after a set time. Simple, but you have no direct control. A TTL of 5 minutes means maximum 5 minutes of outdated data.
- Tag-based invalidation: content is tagged. When you update a product, you clear all cached items with the tag "product-123". Varnish and many modern CDNs support this.
- Event-driven invalidation: with every CMS save or product update, an invalidation request is automatically sent to the cache. This requires some more configuration but gives real-time accuracy.
How a CDN works
Popular CDN options
The market for CDN services is large. The most common choices for European websites:
Cloudflare is the most popular choice for smaller to medium-sized organizations. The free plan already offers CDN, DDoS protection, and SSL. Paid plans add more configuration options and analytics. Cloudflare works as a reverse proxy, meaning all traffic flows through their network.
BunnyCDN is a European provider (Slovenia) with good coverage and competitive pricing. For organizations that prefer to stay within Europe, this is a serious option.
Fastly and Akamai are the enterprise options with advanced configuration capabilities (Varnish-based VCL configuration at Fastly, extensive rule systems at Akamai). Higher costs, but more control for complex requirements.
AWS CloudFront is the logical choice if you're already in the AWS ecosystem. It integrates well with S3, EC2, and other AWS services.
Core Web Vitals in practice
Google uses Core Web Vitals as a ranking signal. Three metrics are relevant: Largest Contentful Paint (LCP), Interaction to Next Paint (INP), and Cumulative Layout Shift (CLS). A CDN directly improves LCP: the large element that becomes visible first (often a hero image) is delivered faster from a nearby edge location.
Redis and full-page caching improve Time to First Byte (TTFB), the time before the browser receives the first byte of the response. A fast TTFB has direct influence on how quickly a page starts rendering.
Conclusion
Caching and CDN are not luxury features. They are part of a serious web infrastructure, regardless of the platform or scale of your website. The investment in Redis, Varnish, and a CDN pays off in lower load times, less server load, and higher conversion.
Start simple: enable OPcache if it's not active yet, add Redis object caching, and put a CDN in front of static assets. Those are three measures you can implement in an afternoon and that give immediate results.
Need help speeding up your website? Explore our high-traffic solutions.