A content delivery network (CDN) refers to geographically distributed servers (also called edge servers) that provide fast delivery of static and dynamic content. Let’s take a look at how it works.
Suppose Bob who lives in New York wants to visit an eCommerce website that is deployed in London. If the request goes to servers located in London, the response will be quite slow. So we deploy CDN servers close to where Bob lives, and the content will be loaded from the nearby CDN server.
The diagram below illustrates the process:
1. Bob types in www.myshop.com in the browser. The browser looks up the domain name in the local DNS cache.
2. If the domain name does not exist in the local DNS cache, the browser goes to the DNS resolver to resolve the name. The DNS resolver usually sits in the Internet Service Provider (ISP).
3. The DNS resolver recursively resolves the domain name (see my previous post for details). Finally, it asks the authoritative name server to resolve the domain name.
4. If we don’t use CDN, the authoritative name server returns the IP address for www.myshop.com. But with CDN, the authoritative name server has an alias pointing to www.myshop.cdn.com (the domain name of the CDN server).
5. The DNS resolver asks the authoritative name server to resolve www.myshop.cdn.com.
6. The authoritative name server returns the domain name for the load balancer of CDN www.myshop.lb.com.
7. The DNS resolver asks the CDN load balancer to resolve www.myshop.lb.com. The load balancer chooses an optimal CDN edge server based on the user’s IP address, user’s ISP, the content requested, and the server load.
8. The CDN load balancer returns the CDN edge server’s IP address for www.myshop.lb.com.
9. Now we finally get the actual IP address to visit. The DNS resolver returns the IP address to the browser.
10. The browser visits the CDN edge server to load the content. There are two types of contents cached on the CDN servers: static contents and dynamic contents. The former contains static pages, pictures, videos; the latter one includes results of edge computing.
11. If the edge CDN server cache doesn't contain the content, it goes upward to the regional CDN server. If the content is still not found, it will go upward to the central CDN server, or even go to the origin - the London web server. This is called the CDN distribution network, where the servers are deployed geographically.
If you enjoyed this post, you might like our system design interview books as well.
SDI-vol1: https://amzn.to/3tK0qQn
SDI-vol2: https://amzn.to/37ZisW9
Hello Alex,
thanks for posting this in-depth article about CDNs.
If I might provide a small critic though, I find it unnecessary complicated.
Steps 1-9 are not very different from what happens when resolving a DNS entry to an IP address.
The few differences are:
- the DNS entry www.myshop.com always returns the IP of a DNS Server that is managed by the CDN provider
- This last DNS Server (what you called the CDN load balancer) returns a different IP for the CDN edge servers that changes at every client requests depending its geographical location, the CDN current load, the number of edge servers available, and many other factors that might depends from the type of content or the application.
As a famous example of a CDN, Netflix has a network of servers (acting effectively as a CDN) that are co-located within major ISPs around the globe.
Their CDN strategy is a mix of what is currently popular in a specific region, the release of new content (that might expect to become popular), and various other factors.
You can learn more at https://openconnect.netflix.com/en_gb/.
As a side note, I recently published an article about the role of CDNs in System Design at https://cloudnativeengineer.substack.com/p/the-role-of-content-delivery-networks. I doesn't explain the Netflix use case but it provides a good overview of why you might need a CDN in your architecture.
How do we ensure the authenticity of user accessing the CDN ?