CDN: How Websites Serve Content Faster Globally
Imagine opening a website from India while its servers are located in the United States. You request an image. Your request travels thousands of kilometers to the server, the server processes it, and the response travels all the way back to you. It works. But what happens when millions of users around the world do the same thing? This is where a CDN (Content Delivery Network) comes in. A CDN…
A Content Delivery Network (CDN) is a network of servers spread around the world that stores and delivers frequently requested content closer to users. This setup helps reduce latency, improve performance, and take load off the main server. When a user requests content, their request travels to the nearest CDN edge server instead of the origin server.
The CDN then checks if the content is cached. If it is (a cache hit), the CDN delivers the content directly, bypassing the origin server. If not (a cache miss), the CDN fetches the content from the origin server and stores a copy in its cache for future requests. For example, if 1 million users request the same image, without a CDN, that's 1 million requests to the origin server.
With a CDN, the CDN can serve most requests from its cache, reducing the origin server's workload. CDNs typically cache static content like images, CSS files, JavaScript files, fonts, videos, PDFs, and static HTML. They store these resources in geographically distributed Points of Presence (PoPs) to minimize the physical distance data needs to travel.
When an edge server has the requested content (cache hit), it delivers it immediately, reducing response time. If not (cache miss), the CDN fetches the content from the origin server and caches it for later use. To ensure users get the most up-to-date content, CDNs use cache control headers like Cache-Control: public, max-age=86400, which specifies how long content can be cached. When content changes, the CDN must invalidate the cached version to prevent serving stale content.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.