Course: Full Stack with AI (NUS)
Module: 18 - Performance Optimization and Scalability
This exercise provides a practical look at Content Delivery Networks (CDNs). We will first explore how a major website (CNN.com) uses CDNs to deliver content efficiently. Then, we will deploy our own simple static website to a global CDN using the free service Surge.sh. This will help illustrate the concepts of performance optimization and scalability discussed in Module 18.
In this part, we’ll investigate how a high-traffic website like CNN uses CDNs.
Observe the URL: Look at the URL of the image in the new tab or the copied address. You’ll likely see that it doesn’t start with www.cnn.com or cnn.com. Instead, it probably starts with something like media.cnn.com.
Example URL might look like: https://media.cnn.com/api/v1/images/stellar/prod/xxxxxxxx.jpg?q=w_xxxx,h_xxxx,x_xxxx,y_xxxx,c_fill
media.cnn.com subdomain is a strong indicator that CNN is serving its static media assets (images, videos) from a different system than its main website content. This system is almost certainly a Content Delivery Network (CDN).Understanding CDN Concepts:
media.cnn.com, CNN offloads the heavy lifting of delivering these large files from its main application servers. The main servers can focus on generating the article text, handling user interactions, and serving dynamic content.media.cnn.com, the CDN directs your request to the server geographically closest to you. This reduces latency (delay) and speeds up loading times significantly.Connection to Modern Web Apps:
Traditionally, every click might have resulted in a full page request to the main server. Modern frontend frameworks (like React, Vue, Angular) often build Single Page Applications (SPAs). The initial load might fetch the core application code (HTML, CSS, JavaScript). These static application files themselves are often hosted on a CDN for fast initial loading. Subsequent interactions then typically involve making API requests to the backend server just to fetch or update data, not entire pages. This architecture leverages CDNs heavily for performance.
(Optional: For a deeper dive, you can open your browser’s Developer Tools (usually F12), go to the ‘Network’ tab, and reload the CNN article page. You’ll see requests going to various domains, including CDNs, for different assets like images, scripts, and stylesheets.)
Now, let’s deploy a very simple static HTML file to a global CDN using Surge.sh. Surge is a simple, free service for deploying static web projects.
npm install -g surge
my-cdn-test.
mkdir my-cdn-test
cd my-cdn-test
index.html: Inside the my-cdn-test folder, create a file named index.html. Open it in a text editor and add the following minimal HTML content:
<!DOCTYPE html>
<html>
<head>
<title>My CDN Test</title>
</head>
<body>
<h1>Hello from the CDN!</h1>
</body>
</html>
Save the file.
my-cdn-test folder in your terminal. Run the deploy command:
surge
/path/to/your/my-cdn-test). Press Enter to confirm.random-words.surge.sh). You can accept it by pressing Enter, or type your own unique subdomain name and press Enter.Analyze the Output: Surge will upload your file and then show output similar to this (your domain and details will differ):
domain: your-unique-name.surge.sh <-- Your live site URL
size: 1 files, 116 bytes <-- Size of your project
upload: [=========================] 100%
CDN: [=========================] 100% <-- CDN propagation status
┌──────────────────────────────────────────────────────────────────────────────┬────────────────────┐
│ Certificate: issuer=C = GB, ST = Greater Manchester, L = Salford, O = Sec… │ Valid │
│ *.surge.sh, surge.sh │ XX more days │
└──────────────────────────────────────────────────────────────────────────────┴────────────────────┘
┌──────────┬──────────────────────────────────────────────────────────────────────────────┬─────────┐
│ │ ns1.surge.world ns2.surge.world or CNAME… │ │
│ NS │ ns3.surge.world ns4.surge.world geo.surge.world │ │
├──────────┼──────────────────┬───────────────────────┬─────────────────────┬─────────────┼─────────┤
│ HTTP │ sfo.surge.sh │ US, San Francisco │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ lhr.surge.sh │ GB, London │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ yyz.surge.sh │ CA, Toronto │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ jfk.surge.sh │ US, New York │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ ams.surge.sh │ NL, Amsterdam │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ fra.surge.sh │ DE, Frankfurt │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ sgp.surge.sh │ SG, Singapore │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ blr.surge.sh │ IN, Bangalore │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ syd.surge.sh │ AU, Sydney │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
│ HTTP │ nrt.surge.sh │ JP, Tokyo │ XXX.XXX.XXX.XXX │ Provider │ ✔ ◍ │
└──────────┴──────────────────┴───────────────────────┴─────────────────────┴─────────────┴─────────┘
Live preview ............................................. XXXXXXXXXXXXX-your-unique-name.surge.sh
Production ............................................................. your-unique-name.surge.sh
Success! - Published to your-unique-name.surge.sh
CDN: [===] 100% line. Surge explicitly tells you it’s pushing your content to its CDN edge locations.HTTP. It shows server locations across the globe (San Francisco, London, Singapore, Tokyo, etc.). This demonstrates the global distribution aspect of a CDN. Your index.html file is being copied to these locations.your-unique-name.surge.sh domain provided in the output. You should see your “Hello from the CDN!” message. Wow! Your simple webpage is now live on a global CDN.Optional Next Step:
Surge allows you to use custom domains. If you owned mydomain.com, you could configure a CNAME DNS record for static.mydomain.com to point to na-west1.surge.sh (or the appropriate Surge endpoint) and then deploy using surge ./ my-static-site.mydomain.com. This allows you to run parts of your website entirely from a CDN under your own domain!
In this exercise, you observed how real-world websites leverage CDNs for performance and saw firsthand how easy it can be to deploy your own static content to a global CDN. This reinforces the importance of CDNs for optimizing the delivery of web assets, reducing latency, and improving the scalability of web applications, as discussed in Module 18.