Cloud & Infrastructure

What a CDN is and when you actually need one

What is a CDN and how does it improve website speed? In this article, we examine the edge caching mechanism, the real impact on speed, and the cases where a CDN does not help.

Cloud & Infrastructure

What Is a CDN? A Definition That Actually Helps You

If you have a website or web application, you have probably heard of CDN. But what is a CDN and do you really need it? CDN stands for Content Delivery Network; a network of servers distributed across different geographic locations that keeps your website content cached at the "edge." When a user from Iran or Germany opens your website, instead of connecting directly to the origin server (which might be in the Netherlands), they connect to the nearest edge server.

The important point is that a CDN is not a "host"; rather, it is a layer between the user and the origin server. Static content such as images, CSS, JavaScript, and video files are cached on edge servers, and subsequent requests are served from there. This reduces latency, decreases the load on the origin server, and improves user experience.

Edge Caching Mechanism: How Does It Work?

To better understand what a CDN is, you need to become familiar with the concept of edge caching. When a user requests a static file for the first time, the edge server fetches it from the origin server, stores it in its memory, and delivers it to the user. Subsequent requests for the same file are served directly from the edge cache.

HTTP Headers and Cache Control

The duration a file is kept in the edge cache is controlled by HTTP headers. The two main headers are:

  • Cache-Control: max-age=86400 — the file stays in the cache for 24 hours.
  • ETag — used to validate whether the file has changed or not.

A real example of setting headers for static files in Nginx:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

With this configuration, the user's browser and edge servers know that these files will not change for 30 days and can cache them.

Difference Between Edge Cache and Browser Cache

Browser cache only works for a specific user; however, edge cache is shared among all users connected to that server. This means if 1,000 users from the same city access your website, only the first request goes to the origin server, and the rest are served from the edge cache.

The Real Impact of a CDN on Speed: What Changes?

Many people think a CDN is a "magic button" that solves all speed problems. The reality is that the impact of a CDN depends on the type of content and the architecture of your website.

Reducing Network Latency (RTT)

Suppose your origin server is in Frankfurt and a user in Tehran makes a request. The round-trip time (RTT) is about 80 to 100 milliseconds. With an edge server in Turkey or the UAE, this number drops to 30 to 40 milliseconds. For a web page with 50 separate requests, this difference can reduce loading time by 2 to 3 seconds.

Reducing the Load on the Origin Server

If your website receives 100,000 visits daily and 70% of the traffic is related to static files, a CDN can remove 70,000 requests from the origin server. This means your server can operate with fewer resources, and responses to dynamic requests (such as APIs) become faster.

Resilience Against Sudden Traffic Spikes

When news about your website is published and you receive 10 times the normal traffic, a CDN can absorb a large portion of this pressure. Without a CDN, the origin server might go down.

When Does a CDN Not Help?

Now we get to the important part. What is a CDN and what can it not do? If you think a CDN solves all speed problems, you are sorely mistaken.

Dynamic and Personalized Content

If your website is fully dynamic — for example, a user panel that reads each user's information live from the database — a CDN cannot cache the main content. In this case, a CDN can only cover static files (CSS, JS, images), and its impact is limited.

Server-Side Problems

If your origin server is slow — for example, a weak database, heavy queries, or inefficient code — a CDN hides these problems but does not solve them. The user still waits for the dynamic response.

A Real Example of a Common Mistake

Suppose you have an online store and have designed the product page to be fully dynamic. Every time a user opens the page, the price and stock are read from the database. If you enable a CDN but do not cache the main page, only images and CSS files load faster. However, if you enable caching for the product page with a short TTL (for example, 60 seconds), you can significantly improve speed — provided that you manually purge the cache after every price change.

Common mistake: Many developers set the Cache-Control: no-cache header on all responses to avoid caching issues. This makes the CDN practically ineffective. Instead, configure static files with long caching and dynamic pages with short caching (or no caching).

When Do You Really Need a CDN?

Now that you know what a CDN is and what its limitations are, let's see under what conditions you should use a CDN.

Geographically Dispersed Audiences

If your users are spread across several countries or several major cities in Iran, and your origin server is located in only one place, a CDN can noticeably reduce latency. For example, if your server is in Tehran and you have users in Mashhad, Ahvaz, and Tabriz, a CDN with points of presence in these cities can improve speed by 30% to 50%.

Heavy and Multimedia Content

If your website is full of high-quality images, videos, or downloadable files, a CDN is essential. A 10 MB video downloaded 1,000 times generates 10 GB of traffic. With a CDN, this traffic is distributed among edge servers, and the origin server is not put under pressure.

High and Fluctuating Traffic

If your website receives high traffic at specific times of the day (for example, during an event), a CDN can act as a shock absorber. Without a CDN, you would need to provision the origin server for maximum possible traffic, which is costly.

Improved Security and Resilience Against Attacks

Many CDNs have the ability to filter malicious requests and mitigate DDoS attacks. If your website has been targeted by an attack before, a CDN can add a layer of protection.

How to Properly Set Up a CDN?

If you decide to use a CDN, follow these steps:

  1. Identify static content: Separate CSS, JS, images, fonts, and video files.
  2. Set cache headers: Use Cache-Control: public, max-age=31536000, immutable for static files.
  3. Enable the CDN: Connect your domain to the CDN and change the DNS records.
  4. Test the cache: Use tools like curl -I to verify that headers are sent correctly.
  5. Enable monitoring: Keep an eye on the Cache Hit Ratio. If it is below 80%, adjust your settings.

Example test with curl:

curl -I https://yourdomain.com/assets/style.css
# You expect to see the following header:
# cache-control: public, max-age=31536000, immutable
# age: 12345
# x-cache: HIT

If you see x-cache: MISS, it means the file has not yet been stored in the edge cache and the request went to the origin server.

Conclusion: Do You Need a CDN?

The short answer is: if your website has significant static content and your users access it from different geographic locations, then yes. If your website is fully dynamic and has a limited number of users, you can probably work without a CDN.

Remember that a CDN is a tool, not a magic solution. First, solve your infrastructure problems — keep the server up to date, optimize the database, and rewrite inefficient code. Then add a CDN as a complementary layer.

If you are looking for a hosting solution with proper infrastructure to get started, ServerNet offers web hosting services and cloud infrastructure that can be a good foundation for launching your website. But the final decision about using a CDN should be based on your website's real needs, not marketing hype.

Ultimately, the best approach is to measure your website's speed before and after enabling a CDN using tools like Google PageSpeed Insights or GTmetrix. The real numbers will tell you whether a CDN is worth it for your website or not.

ServerNet Support

ServerNet engineering & editorial team — specialists in infrastructure, networking and web hosting.

Cloud Infrastructure (IaaS)
Share:

Comments 0

No comments yet — be the first!

Leave a comment

Related service

Cloud Infrastructure (IaaS)

Servers, private networks, firewalls and storage — all API-driven and billed hourly. Infrastructure as code that scales with you.