What is a CDN?
It's a network of servers distributed in different locations around the world.
The CDN stores or caches static files such as:
Images
CSS
JavaScript
Videos
Fonts
PDFs
For example, imagine your project server is in Egypt:
project Server
πͺπ¬ Egypt
β
ββββββββββββΌβββββββββββ
β β β
CDN CDN CDN
π©πͺ πΊπΈ πΈπ¬
Germany USA Singapore
Suppose you have:
Project Server
β
/storage/products/iphone.jpg
Without CDN:
<img src="https://project-domain.com/storage/products/iphone.jpg">
The user in Germany requests the image:
Germany π©πͺ
β
Egypt πͺπ¬
β
iphone.jpg
β
Germany
Every request travels to your server.
With a CDN:
You configure a CDN, for example:
cdn.project-domain.com
Your image might become:
<img src="https://cdn.project-domain.com/products/iphone.jpg">
Now:
User π©πͺ
β
CDN π©πͺ
β
iphone.jpg
The image can be delivered from a server much closer to the user.
But where does the image come from initially?
This is the important part.
Suppose the image doesn't exist on the German CDN server yet.
The first request could work like:
User π©πͺ
β
CDN π©πͺ
β
"I don't have iphone.jpg"
β
Project/Storage πͺπ¬
β
iphone.jpg
β
CDN π©πͺ β cache it
β
User
Then another user in Germany requests the same image:
User π©πͺ
β
CDN π©πͺ
β
iphone.jpg β
The request doesn't need to go back to your project server.
That's one of the biggest benefits.
Another approach: Object Storage + CDN
For a modern application, you might have:
Your Project
β
β
Amazon S3 / Azure Blob Storage
β
β
CDN
β
β
Users
For example:
Your Project
β
S3
β
CloudFront
β
Users
Your application uploads:
iphone.jpg
to S3.
The CDN delivers it to users.
This is especially useful when you have millions of images because you don't need to store all those images on your application server.
Why do we use a CDN?
Faster
The user gets the content from a nearby server.Less load on your project
Instead of:
10,000 users
β
Project Server
You can have:
10,000 users
β
CDN
β
Project only when necessary
- Better scalability Your Project server focuses on: API Business Logic Database Authentication Orders Payments
while the CDN handles:
Images
CSS
JS
Videos
Static files
- Protection Many CDN providers also provide things like: DDoS protection Caching WAF TLS/HTTPS Rate limiting One important distinction
Top comments (0)