What is CDN and how do they work.
Tags: computerscience, webdev, tutorial, beginners
Published: 2020-05-03 15:37:33

CDN is short for Content Delivery Network. It's a geographically distributed group of servers that work together to provide fast delivery of internet content.

Content: The content or assets can be anything, HTML pages, CSS files, Javascript files, images, videos, songs etc.
So when we type this line :
```javascript
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
```
we're basically asking google fonts to provide us with the particular font design.

Delivery: Self-explanatory.

Network: Why is it called network?
It's called a network since bunch for servers work together to send "content" back to the client.

If you've played any of multiplayer online games like Dota or CS:GO, there's a command which we type: "ping" and it returns something like "100ms" or "40ms".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8x82rz2sbm4wbm4aua3d.jpg)
<small>dota 2 ping for different regions</small>

Ping is how much time it takes for the server to return the content asked by the client. So 100ms means that it takes 100ms for client -> server -> client data transfer. So in this case, if I am connected to a USEast server, then the data exchange between me and the server on which the actual game is running takes 26ms.

But why a cluster of servers?

Now imagine you're in US, and you want to play with your friend in India so you decide to join a server in India. So for data transfer between you and the server now takes 300ms on average, which doesn't seem much but for a high paced game like Dota, it's too much or as gamers call it "LAG". 

Here's an example of the same in Fortnite: 
<img src="https://media.giphy.com/media/VEWGvfrgzir97AP3BV/giphy.gif" /> 

In this case if player 1 ping = 300ms and player 2 ping = 26ms, the actions performed by player 2 are received, processed, and distributed much faster compared to player 1. 

The same applies to all other services like Netflix, Spotify etc. It would be unfeasible to store all movies and TV shows, at one location. 
To tackle this, they install a bunch/cluster of servers in many different locations that contain data, and these in turn are connected to the main server which provides all the data. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xldmv5thyqkugia0yx2c.png)

Implementing this kind of network has many advantages: 

1> <strong>Decreased load  </strong> : Each server will serve customers in that region which brings down server load of whole system.

2> <strong>Faster Content Delivery</strong> : Since load is decreased, it's easier on server transfer files.

3> <strong>Protection against potential DDOS attacks </strong> : Since there are many servers located, if one server is affected by DDOS attack, other backup servers will kick into action and serve customers in that region, and overloading the main server becomes difficult.

4> <strong>Analytics</strong> : Companies like Netflix use the data about things like, most-watched movies / TV shows in that region and use that data to update their regional servers so that relevant content is available to customers. 

Eg: if customers in US are watching "Stranger Things" at high demand and customers in South Korea are watching "Kingdom", Netflix will update their local US and Korean servers accordingly. 

Netflix, in particular, goes beyond. Netflix ties up with local ISP to install boxes similar to these : 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eax3jukmrjukladjimm7.jpg)

Since the population density of Mumbai is higher compared to that of New York, Netflix might end up installing more of these boxes in Mumbai to serve their customers. 


Comment down below if I missed something or messed somewhere. 

Thanks for reading :) 

Sources : 
https://www.youtube.com/watch?v=YXQpgAAeLM4
https://hackernoon.com/powering-your-app-with-a-realtime-messaging-cdn-13d92a6df5f3
https://www.cloudflare.com/learning/cdn/what-is-a-cdn/
https://medium.com/refraction-tech-everything/how-netflix-works-the-hugely-simplified-complex-stuff-that-happens-every-time-you-hit-play-3a40c9be254b?source=search_post---------0