CDN ¶
A content delivery network (CDN)
is a distributed network of servers that can efficiently deliver web content to users. CDNs store cached content on edge servers
that are close to the end users to minimize latency.
Caching and CDN
Caching is the process of storing data locally so that future requests for that data can be accessed more quickly. In the most common type of caching, web browser caching
, a web browser stores copies of static data
locally on a local hard drive. By using caching, the web browser can avoid making multiple round-trips
to the server and instead access the same data locally, thus saving time and resources. Caching is well-suited for locally managing small, static data such as static images, CSS files, and JavaScript files.
Similarly, caching is used by a content delivery network on edge servers close to the user to avoid requests traveling back to the origin and reducing end-user latency. Unlike a web browser cache, which is used only for a single user, the CDN has a shared cache. In a CDN shared cache, a file that is requested by one user can be accessed later by other users, which greatly decreases the number of requests to the origin server.
- CDNs are typically used to deliver static content such as images, style sheets, documents, client-side scripts, and HTML pages.
- The major advantages of using a CDN are lower latency of content to users, regardless of their geographical location in relation to the datacenter where the application is hosted.
- CDNs can also help to reduce load on a web application, because the application does not have to service requests for the content that is hosted in the CDN.
Tip
Static content is easy to serve becasue it does not need to be modified for each request.
Caching at various levels¶
Caching can occur at multiple levels between the origin server and the end user:
Web server (Origin)
: Uses a shared cache (for multiple users).Content delivery network
: Uses a shared cache (for multiple users).Internet service provider (ISP)
: Uses a shared cache (for multiple users).Web browser (Client)
: Uses a private cache (for one user).
CDN Internal Architecture¶
- A user types in
www.amarjitdhillon.com
in the browser. The browser looks up the domain name in the local DNS cache. - 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)
. - The DNS resolver recursively resolves the domain name. Finally, it asks the authoritative name server to resolve the domain name.
- If we don’t use CDN, the authoritative name server returns the IP address for
www.amarjitdhillon.com
. But with CDN, the authoritative name server has an alias pointing towww.amarjitdhillon.cdn.com
(the domain name of the CDN server). - The DNS resolver asks the authoritative name server to resolve
www.amarjitdhillon.cdn.com
. - The authoritative name server returns the domain name for the load balancer of CDN
www.amarjitdhillon.lb.com
. - The DNS resolver asks the CDN load balancer to resolve
www.amarjitdhillon.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. - The CDN load balancer returns the CDN edge server’s IP address for
www.amarjitdhillon.lb.com
. - Now we finally get the actual IP address to visit. The DNS resolver returns the IP address to the browser.
- 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, and videos; the latter one includes results of edge computing.
- If the
edge CDN server
cache doesn't contain the content, it goes upward to theregional CDN server
. If the content is still not found, it will go upward to thecentral CDN server
Challenges of using CDN¶
There are several challenges to take into account when planning to use a CDN.
-
Deployment
: Decide the origin from which the CDN fetches the content, and whether you need to deploy the content in more than one storage system. Take into account the process for deploying static content and resources. For example, you may need to implement a separate step to load content into Azure blob storage. -
Versioning and cache-control
: Consider how you will update static content and deploy new versions. Understand how the CDN performs caching and time-to-live (TTL). -
Testing
. It can be difficult to perform local testing of your CDN settings when developing and testing an application locally or in a staging environment. -
Search engine optimization (SEO)
: Content such as images and documents are served from a different domain when you use the CDN. This can have an effect on SEO for this content. -
Content security
. Not all CDNs offer any form of access control for the content. Some CDN services, including Azure CDN, supporttoken-based authentication
to protect CDN content. -
Client security
. Clients might connect from an environment that does not allow access to resources on the CDN. This could be a security-constrained environment that limits access to only a set of known sources, or one that prevents loading of resources from anything other than the page origin. A fallback implementation is required to handle these cases.