Posted by : Sushanth Thursday, 30 December 2021

 

Content delivery network (CDN)

Problem: On average, 80% of a website consist of static resources therefore accessing this static content from its origin server is time consuming and makes the website slower. This problem aggravates if the visitors of the website are from different geographic locations.

Solution:

CDN is essentially a group of servers that are strategically placed across the globe with the purpose of accelerating the delivery of your static web content.

CDN servers cache static content like images, videos, CSS, JavaScript files, etc. It enables the caching of HTML pages that are based on request path, query strings, cookies, and request headers.

CDN users also benefit from the ability to easily scale up and down much more easily due to traffic spikes.

How it works?

when a user visits a website, a CDN server closest to the user will deliver static content. Intuitively, the further users are from CDN servers, the slower the website loads. For example, if CDN servers are in San Francisco, users in Los Angeles will get content faster than users in Europe

How CDN improves load time?


CDN workflow:


1.       User A tries to get image.png by using an image URL. The URL’s domain is provided by the CDN provider. The following two image URLs are samples used to demonstrate what image URLs look like on Amazon and Akamai CDNs:

         • https://mysite.cloudfront.net/logo.jpg

         • https://mysite.akamai.com/image-manager/img/logo.jpg

2. If the CDN server does not have image.png in the cache, the CDN server requests the file from the origin, which can be a web server or online storage like Amazon S3.

3. The origin returns image.png to the CDN server, which includes optional HTTP header Time-to-Live (TTL) which describes how long the image is cached.

4. The CDN caches the image and returns it to User A. The image remains cached in the CDN until the TTL expires.

5. User B sends a request to get the same image.

6. The image is returned from the cache as long as the TTL has not expired.

Considerations of using a CDN:

Cost: CDNs are run by third-party providers, and charges are incurred for data transfers in and out of the CDN. Caching infrequently used assets provides no significant benefits so you should consider moving them out of the CDN.

Setting an appropriate cache expiry: For time-sensitive content, setting a cache expiry time is important. The cache expiry time should neither be too long nor too short. If it is too long, the content might no longer be fresh. If it is too short, it can cause repeat reloading of content from origin servers to the CDN.

 CDN fallback: You should consider how your website/application copes with CDN failure. If there is a temporary CDN outage, clients should be able to detect the problem and request resources from the origin.

 • Invalidating files: You can remove a file from the CDN before it expires by performing one of the following operations:

 • Invalidate the CDN object using APIs provided by CDN vendors.

• Use object versioning to serve a different version of the object. To version an object, you can add a parameter to the URL, such as a version number. For example, version number 2 is added to the query string: image.png?v=2. 


Kinds of CDNs based on how the content is cached and refreshed?

There are two kinds of CDNs based on how the content is cached and refreshed - Push CDNs and Pull CDNs

Push CDNs - In Push CDN model, content is pushed to the CDN cache servers whenever content is modified, or new content is added, on the origin server. Origin server is responsible for pushing the content to the CDN cache servers


Pull CDNs - In Pull CDN model, content is pulled by the CDN from the origin server when that content is first requested by a client. This content is then stored in the cache servers for a certain period of time, usually 1-30 days based on the TTL, so that subsequent requests for that content is served from the CDN cache server.

Popular CDNs

Cloud providers typically offer their own CDN solutions, since it’s so popular and easy to integrate with their other service offerings. Some popular CDNs include Cloudflare CDN, AWS Cloudfront, GCP Cloud CDN, Azure CDN, and Oracle CDN.

 When not to use CDNs?

If the service’s target users are in a specific region, then there won’t be any benefit of using a CDN, as you can just host your origin servers there instead.

CDNs are also not a good idea if the assets being served are dynamic and sensitive. You don’t want to serve stale data for sensitive situations, such as when working with financial/government services.

Scenario

You’re building Flipkart’s product listing service, which serves a collection of product metadata and images to online shoppers’ browsers. Where would a CDN fit in the following design?




Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Technical Articles - Skyblue - Powered by Blogger - Designed by Johanes Djogan -