What Is a Cdn and How Does It Work?
In this post I want to share with you what is CDN or content delivery network, how you can use it, what are pros and cons it has and also I want to share what CDN I am using for my own projects.
What is CDN?
If you are working as a programmer in some company probably you heard something like "let's move all our data to CDN" or "We can't really handle ourselves our data, let's move it to CDN". So the question is what exactly CDN is and how it is working?
CDN is just a bunch of servers across the world.
The main idea is that all your data is not stored in a single location, but it is available and cashed in different servers across the world. Why we are doing it at all? Typically users nowadays are loading lots of content. For example videos, audios or images. And you might say here "But my web site doesn't have a lot of images or videos". It can happen, but your web site for sure loads lots of CSS, JavaScript and HTML. And actually all this stuff you also can cache inside CDN.
How CDN is working?
The next question is how CDN is helping us at all. And for this you must understand how web is working. We have just a single server somewhere let’s say in Europe, for example in London. And all your clients are going to your web site. And actually it means that they are sending their requests to this single server in London.
- First of all it may be difficult for your single server to give correct responses for all your clients, when you have lots of users on your web site.
- Secondly we have such thing, which is called latency. This is the amount of the time, that your request needs to hit the server and then back the user with the response.
Which actually means for people from London or at least from Europe it will be quite fast, but if you are getting this data across the ocean, for example from US, then it will be long. And actually nowadays we want to make our web sites fast and minimize loading time. Because in other case people will just go to other website.
Here is a nice image, which shows how CDN is working.
As you can see here is our origin server, based somewhere in the US. And we have different users across the world. And they are hitting our server. If we don't have any CDN it means that your responses will be fast only somewhere here in US. But they won't be fast from Europe, because here is the distance is really enormous.
If we are talking about CDN, this are these green boxes which we typically have across the world. So we have it here on Europe somewhere, in Asia and in US. Which actually means people across the world won't hit your server at all. They will hit not your server, but CDN server, where you have cache for all your data. In this case all requests for them will be extremely fast and this is exactly what we want. This is why nowadays we are using CDN in all big projects.
Benefits of CDN
So let’s now talk about benefits of CDN.
The first one is improving loading time of data inside your web site.
But here is something important to understand. All our CDN servers are not storing our data. Which means it is not replication it is simply a cache, we still have just a single storage of data in our case here somewhere in the US. And all our CDn servers will simply give users our cache data. And even better they can also on their own optimize all our content. For example they can use different ways to compress your files.
The second important point is reducing bandwidth costs. And actually it costs enormous amount of money if you have just a single web server. If we are using CDN and cache our data inside them, we will pass less real to our users, which means we will pay less for bandwidth.
One more important point is content availability and reliability, which actually means we are storing our content in single place, but we cache it in different places. And actually this means that for example if this CDN server somewhere in the US for some reason is out and can't work anymore, then user won't just access data from our main server, but it will access it from another CDN.
Which actually means we have inside something like load balancer. If we have too many client or your CDN server is not working, then the request will go to the next CDN server, which is the closest. And it means that not our web server will be down, but just one of the CDN and it will still work for all users.
So the most important point here, that CDN distributes all traffic that is coming to your web site to different CDN servers.
And this is extremely efficient from different perspectives. For example nowadays we are talking a lot about DOS attacks, which actually means somebody want to put your website down. And it simply sends like millions of requests to your web site and typically if you have just one server, then your web server is going down, because it can't handle millions of requests.
In the case of CDN it's not like this. For example let's say that some hacker is sending millions of requests to our website. And this request won't make anything down or just a single CDN server, because of load balancing which distributes all this requests across all other CDN servers. And even if some of the servers are down, our main web server is still intact and even doesn't know anything about these attacks. Which actually means our web server even if it is super small and super cheap will still work.
One more important point is the difference between cloud servers and CDN. Actually a lot of people nowadays are using cloud computers or cloud servers, which actually means you just have somewhere one cloud, so one server, where you throw your data. And it can be really nice for you, because it is comfortable, you just upload your data there. But still it is not a replacement to CDN, because you have just a single server. You still store your data in just a single place.
So CDN and cloud servers are completely different things. You are using cloud sever just like a normal server to store your data somewhere comfortably. But you are using CDN to implement caching and make your data available and cached across the world for low latency.
And last but not least, if you are looking for CDN for yourself I want to show you what I am using myself. And this is not an advertisement, because this is exactly what I am using to host my courses. And this company didn't pay me anything to tell you about them.
So as you can see here it is called https://bunnycdn.com and actually it is quite cheap but reliable CDN. And if you will check the prices for some popular CDN networks, they are really high. And if you have some small project, but you just want CDN and maybe additionally a place to store all your data then you don't really want to pay for them that much. Because they are really expensive.
Here it is not that case and this is how it looks like inside. So this is just an easy UI, where you can create your storage and you upload some data. For example here as you can see I have a folder with my posts for my web site. And here inside I have thumbnails for every single post. And it is all stored inside just one single server here inside bunny CDN, but it is also cached in different CDN servers across the world.
And just to tell you some numbers so you understand the cost. I am storing somewhere around 250 GB of data and mostly it is only videos. Typically I am paying for this CDN in month 4-5 $, which is not that expensive. So now you know how CDN is working and why do we need CDN at all.
Want to conquer your next JavaScript interview? Download my FREE PDF - Pass Your JS Interview with Confidence and start preparing for success today!