CDN stands for content delivery network or content distribution network and is mainly used to serve the static content of a web page like images, media, JavaScript and CSS.
How CDN Works?
The CDN companies strategically position their server locations in a manner to deliver static content to users from the nearest location. This helps in reducing the web page load time for any website as well as helps in downloading the static content parallelly from a CDN.
CloudFront is a CDN service provided by the Amazon Web Services and here are its edge locations.
The CDN stores a copy of each requested resources (as cache) on the network of servers in each of the geographical location to serve the content from the nearest location to the user.
When a visitor makes a request a webpage of a website, the request is processed by the host server and reverts back the HTML content. If the webmaster has configured the CDN, the HTML will have reference to the static content from a CDN edge server thus allowing the browser to process those elements parallelly from the CDN edge server. This makes the hosting server free to serve more page requests.
Here is an example of waterfall model fetching content in parallel mode from a CloudFront CDN.
So all the requests form a CDN URLs are being served by CloudFront thus the hosting server can serve a lot more requests and browsers can fetch content from host server as well as CDN edge servers to render elements parallelly.
Advantages of using a CDN:
- Nearest Location – A hosting server is located in the US can mean every element of a webpage will be served from US but if CDN is used the static content of a web page, which includes images, JavaScripts, CSS, Media files etc which is 70% to 80% of the overall data in a webpage can be served from a nearby location thus reducing the time to download the element.
- Scalable – CDN will make it easy to manage traffic spikes and reduce the load on the main server as significant portion of a webpage is served from CDN.
- Reliable – CDN has multiple servers to serve a request. So if one server isn’t available, requests are automatically sent to the next available server.
- Cost Effective – Consider the cost and the infrastructure needed to host one server in each continent. With CDN, you get the same benefit of hosting an edge server at various locations and still the cost is typically next to nothing because it is shared across all customers.
Disadvantages Of using a CDN:
- Only Static resources – CDN cache each requested element and so it can only be used to serve static resources and isn’t ideal for PHP or server-side processing.
- Long time to invalidate Cached Data – Any data requested on a CDN will be cached across every edge location. Once the data needs to be changed, it can long time to invalidate. As an example, if you have uploaded an image and you want to re-upload the same image with changes, the ideal solution is to use a new url for the image because it can take a long time to invalidate the existing image. Even if data is invalidated on the edge servers, the image may be cached in the user browsers and so users may not see the new image.