Day 25 — CS Fundamentals December — CDNs — Content Delivery Networks
Content Delivery Networks is an easy and extremely important topic especially considering System Design which is asked a lot in software engineering interviews.
Also, one can’t ignore the fact that how extensively CDNs are used for the numerous web apps having focus on global audience.
So, this article is going to be about CDNs and let’s dive into it.
Let’s dive in!
Introduction — The Problem
Let’s talk about a situation.
Say, you host a static one-page website on a server in India. Now, you want people all over the world to check that website out.
Suddenly, your website becomes viral and now, everybody is talking about it.
Now, everybody in India and even surrounding countries like China, Pakistan, Nepal, etc. checked that website out and they are very happy with the website.
But people from farther countries like USA, Canada, France, etc. are complaining that the website is slow to load.
Now, you checked out and revised through the entire code of the website and it seems perfectly fine and efficient, and the proof is that the website is loading blazingly fast for India and surrounding countries.
“So, Yash! What’s the issue? :/”
The problem is distance. Remember that you hosted it on a server in India.
So, Indian and citizens of surrounding countries are much closer to the web server than say, Americans.
And see, at the end of the day, whenever you are trying to open a website, a request in the form of network packet travels from you to the web server and returns.
So, clearly, the farther the server, the more time network packet is going to take to travel and finally, the more user has to wait for the website to load.
So, how can we solve this problem?
Clearly, this is not a problem which can be solved by programming the website the right way as that’s not going to reduce the distance between user and server.
One approach — The lamer one
You can simply host website on servers all around the globe and people from one country or city or zone can use the domain name of the server closest to them.
But you know what. This method sucks.
It legit might involve setting up 50–100 servers, hosting the website individually on them.
Then, you would have to depend on the end user to use the right domain of the closest web server to him.
First of all, identifying which server is facing a problem or crashing, itself is going to be another task.
Apart from this, maintaining those 50–100 servers is going to be one hell of a tedious task.
Also, imagine updating the code or the content of the website. You literally would need to restart all of your servers around the globe which might cause inconsistencies and unwanted downtimes across the globe.
So, we gotta need a better approach.
Another approach — The cooler one
So, we will something which would “sound” similar to the previous approach but it would be different, believe me!
Instead of hosting the website on different servers across the globe, you would just host the website on one server at, say, India.
But now, we would introduce one more thing called “edge location”.
So, the edge location is basically going to be a computer/server only which is going to cache the website.
And there is going to be a huge number of edge location (these caching servers) spread uniformly across the globe.
And, behind the scenes, every request to the website would not travel to the main web server all the way to India.
Instead, every request is going to travel only to the nearest “edge location”.
- If this edge location has the website cached, then the edge location would simply deliver it to the user making it seem as if the main web server was very close.
- If this edge location does not have the website cached, then it would reroute the request to the main web server, get the website, pass it back to the user. And behind the scenes, if it sees that a lot of users are launching and using the same website/webpage, it will cache the website/webpage in it, for the future requests.
So, what is a Content Delivery Network?
It is a network of geographically distributed edge location and the main web server/servers which make sure that the any user across globe gets the web application with least latency as possible by the use of the closely distant edge location.
“But Yash! Users have the domain name to the main web server and not the edge location. So, whenever they are going to enter that domain, then DNS would redirect the request to main web server instead of some edge location.”
“Basically, the question is, how does the browser or DNS reroute the request from the same domain name to an edge location closest to the user in that moment?”
So, now, when you create a CDN or Content Delivery Network, you will get a general URL which will point entirely to the CDN.
Unlike previously, when the domain name pointed to a single main web server, It would not point to a single web server or single computer in the cloud or single edge location. It would seem to point to the entire CDN.
So, no matter from where in the world, you make the request to CDN’s URL, the URL will point to the same CDN and then, it is the job of CDN to pass and process the request from the nearest edge location and the return you the results quickest.
So, why CDN’s are so cool!
- Website load speed is going to be maximized because much closely distant edge locations.
- The load is going to be balanced as each edge location now won’t be facing the load from the entire world. It will just be facing load from the users near it which are going to be much lesser.
- Updating the website, just involves updating one or some servers where the website is actually hosted (main web server). Rest the edge locations would automatically update.
- Monitoring the website is going to be extremely convenient considering the services provided by CDN-service-providers now a days.
- Website uptime is going to be high and the downtime is going to be minimized or nullified.
Thanks for reaching till here :D
I hope you understood the article and got a good idea about CDNs and their working.
If you liked the article, do give it some claps :)
Stay tuned for another article which is going to come tomorrow associated with some other interesting CS fundamental.
Email — email@example.com