Site performance is an important part of improving your search rankings, discoverability, and traffic. Especially when so much of the web is now viewed through mobile devices with limited bandwidth or high latency.
Content Deliver Networks or CDNs, work by caching static assets across servers geographically distributed all over the world. They reduce load times, improve performance, and minimize bandwidth and infrastructure costs.
Let’s dive into the details.
What makes a CDN?
- CDNs consist of multiple points of presence in locations around the world that consist of a number of servers that cache the assets from your origin.
- When someone visits your site, the CDN routes their request to the nearest server.
- If the assets are present locally, then you get a cached copy immediately (the majority of cases). If not, it loads the content from your original host and keeps it cached for the next request.
- Edge servers can mitigate denial-of-service attacks, provide rate-limiting, and are instrumented for analytics.
How does it work?
- When the CDN endpoint receives a request, it forwards it to the datacenter closest to the user. This happens at the DNS level and uses geo-location to assist in that.
- In the datacenter, a load balancer receives the request and routes it to the edge server that hands out the content.
- Edge servers are co-located in datacenters used by your ISPs for network interconnects.
- Requests never leave their geographical area of origin, which means they’ll see lower latency and higher throughput.
How does caching work?
- When the CDN receives the first request for an asset, it does not have it cached, so it fetches a copy from either an edge server close-by or the origin itself.
- That’s known as a cache “miss”, which tends to be a slower response.
- Future requests for the same asset routed to the same edge will then respond from the cache until the it expires. These will be much faster responses.
- Cache expiration is configured through HTTP headers.
- If you inspect HTTP responses, you’ll often find an ‘X-Cache’ header indicating whether your request hit or missed the cache.
Pull vs Push
- Most providers cache your data in two ways:
- Pull Zones work just as we described already. They’re commonly used to deliver content that updates frequently, like HTML, CSS or JS.
- Push Zones on the other hand, work with a designated storage location. When new data makes it into that location, your CDN will automatically push it to its fleet of edge servers. These are typical for files that don’t change often like archives, video or audio.
- Most object storage providers offer some form of CDN to pair with your buckets to handle that push scenario.
- CDNs will offload traffic from your origin.
- They provide advanced HTTP capabilities that you don’t have to worry about. Like HTTP2 support for example.
- Lower latency because of the location of the edge servers
- Better at managing traffic spikes and avoiding downtime due to load balancing across a large network.
- Reduced bandwidth and infrastructure costs.
- Increased security with SSL support, DDoS mitigation, DNS security, etc.