CDN comes from Content Delivery Network, or content delivery network. It is a set of servers spread around the world that store copies of your content, such as images, scripts and pages, and deliver each visitor from the nearest server.
The result is a faster site, with less load on the origin server and more stability during traffic spikes.
How it works
Without a CDN, every visitor fetches files from the same server, the origin server. If it is far away, the response takes longer.
With a CDN, the flow changes:
- the visitor requests a file;
- the CDN responds from an edge server near them;
- if the file is not there yet, the CDN fetches it from origin and stores a cached copy;
- the next visitors in the region receive it straight from cache.
How long the file stays stored is controlled by cache headers, such as Cache-Control.
Practical example
You have a blog hosted on a single server in Brazil and readers in several countries. By putting a CDN in front, images and CSS files start to be served from edge servers closer to each reader.
An example header that allows caching for one day:
Cache-Control: public, max-age=86400If you update a file and the visitor keeps seeing the old version, it may be cache. Many CDNs offer the option to purge cache manually.
What it's for
A CDN is used to speed up sites and applications, reduce server bandwidth costs, handle traffic spikes and help protect against some overload attacks, depending on each provider's features. It also usually delivers content over HTTPS.
For an online business, speed influences customer experience. For developers, it is a common step when publishing sites and interfaces.
Difference between CDN and DNS
DNS translates the site name into an address. A CDN delivers content from nearby servers. In general, you configure the domain DNS to point to the CDN.
Caveats
Not all content should be cached. Pages with personal data or login need special rules so they are not shown to the wrong user.