The Delivery Boy: CDNs for Static Assets
As our platform grew, so did our ambition. We were no longer just an Indian company; sellers were organically discovering us from all over the world. We had stores popping up in Southeast Asia, Europe, and even South America. We were solving a global problem.
This global reach, however, exposed a new weakness in our architecture, one that had nothing to do with CPU, memory, or our code. It had to do with the plain, old, inescapable physics of the speed of light. Our servers were in Mumbai, but our users were everywhere. And for those users, our site was painfully slow. This chapter is about how we solved our global performance problem by building our own international delivery network.
Part 1: The Long Haul
The first sign of trouble came from a seller in Brazil. She ran a small shop selling handmade crafts and had fallen in love with Dukaan's simplicity. But she had a problem. "Your platform is wonderful," she wrote to our support team, "but my customers here in São Paulo tell me the product images take forever to load. They think my website is broken."
At the exact same time, Suumit forwarded me our monthly AWS bill with a single line item highlighted in red: Data Transfer Out from EC2. The cost was spiraling, growing much faster than our server costs.
We were facing another two-headed dragon. Our international users were having a terrible experience, and a mysterious line item on our AWS bill was eating our profits. As it turned out, the two problems had the exact same cause.
Identifying the Problem: The Mumbai Library
All of our infrastructure-our application servers, our load balancers, our databases-was located in one single place: the Amazon Web Services (AWS) data center region in Mumbai. This was great for our Indian users, but terrible for everyone else.
Let's think about the journey of a single product image.
- Our seller in São Paulo uploads a beautiful, high-resolution photo of her craft. That 2MB image file has to travel over 14,000 kilometers across the internet's undersea cables to our server in Mumbai to be stored.
- A customer, also in São Paulo, visits her store. Their web browser requests that image.
- The request travels 14,000 km from São Paulo to our Mumbai server.
- Our server finds the 2MB image and sends it all the way back across those 14,000 km to the customer's browser in São Paulo.
This round trip, dictated by the speed of light, adds seconds of delay to the loading of every single image. This was why our Brazilian seller's store was so slow for her local customers.
This also explained our massive AWS bill. That line item, "Data Transfer Out," is essentially the postage fee that AWS charges you to send data from their data centers out to the public internet. Every time we served an image to a user, we were paying a small fee. Serving millions of large image files to a global audience meant we were paying a fortune in digital postage.
Our server was acting like a single, central library in Mumbai that held all the world's photo albums. If someone in Brazil wanted to see a photo, we were literally mailing them a copy. It was slow, and it was expensive.
Technical Deep Dive: What is a Content Delivery Network (CDN)?
The solution to this problem is a Content Delivery Network, or CDN.
A CDN is a global network of servers specifically designed to deliver static content (like images, videos, CSS, and JavaScript files) to users as fast as possible.
- Analogy: Instead of one central library in Mumbai, imagine a global franchise of libraries. There's a main branch in Mumbai, but there are also local branches in São Paulo, London, Singapore, and Tokyo. When a new photo album is published in the main Mumbai library (our "origin" server), a copy is automatically sent to every franchise library around the world.
Now, when our customer in São Paulo wants to see a photo:
- Their request doesn't go to Mumbai. It is automatically routed to the nearest "library branch"-the CDN's edge location in São Paulo.
- The São Paulo edge location already has a copy of the photo. It serves it directly to the customer, almost instantly.
This process is called caching at the edge. The first time a user in a region requests a file, the edge location fetches it from our origin server in Mumbai and saves a copy. For every subsequent user in that same region, it serves the cached copy.
A CDN solves both of our problems at once:
- Speed: By serving images from a server that is physically close to the user, we could cut the load time from seconds to milliseconds. The experience for our Brazilian users would be just as fast as for our Indian users.
- Cost: The data is now being served from the CDN's network, not ours. CDN providers buy bandwidth in massive bulk and can serve data far more cheaply than we can from our application servers. This would drastically slash our "Data Transfer Out" bill.
We understood the concept. It was time to sign up for a global library franchise and teach our application how to use it.
Part 2: The Global Delivery Network
We understood the theory of CDNs perfectly. It was a simple, elegant solution to both our global speed problem and our spiraling data transfer costs. Now, it was time to implement it.
Since our entire infrastructure was already running on Amazon Web Services (AWS), the natural choice was to use their native CDN solution, Amazon CloudFront. This meant we could manage our CDN from the same console as our servers and databases, keeping things simple.
Technical Deep Dive: Setting up AWS CloudFront
The process of setting up a CDN involves three main steps: creating a central storage location for your original files, configuring the CDN to pull from that location, and finally, telling your application to use the new CDN links.
Step 1: The Origin - Amazon S3
First, we needed a "main library" for all our static assets. Serving images directly from our application servers was inefficient. Instead, we used Amazon S3 (Simple Storage Service).
S3 is essentially a massive, incredibly durable, and very cheap hard drive in the cloud. It's specifically designed for storing files. We changed our application logic so that whenever a seller uploaded a product image, instead of saving it to the server's local disk, our Django app would now upload it directly to a dedicated S3 bucket named dukaan-product-images. This bucket, located in the Mumbai region, would be our single source of truth for all user-generated images.
Step 2: Creating a CloudFront "Distribution"
With our origin library in place, we went to the AWS CloudFront console and created a new "Distribution," which is what they call a CDN configuration. The setup was a simple wizard:
- Origin Domain: We told CloudFront where to find the original files. We pointed it directly to the S3 bucket we had just created.
- Cache Behavior: We configured the rules for our global franchise of libraries. We set a TTL (Time-To-Live) of 24 hours. This meant that once an edge location cached an image, it would serve that copy for 24 hours before checking back with our S3 bucket in Mumbai to see if there was a newer version.
- Deployment: We clicked "Create Distribution." AWS then spent about 15 minutes deploying our new configuration across its entire global network of hundreds of edge locations.
When it was finished, AWS gave us a unique domain name for our CDN, something like d123xyzabcdef.cloudfront.net. This was now the public address for our global delivery network.
Step 3: Updating our Application
This was the final step. Our website's code was still generating old image links. An image URL looked like this: https://dukaan.app/media/seller123/product.jpg
We had to update our code to generate new links that pointed to the CDN instead. We also set up a custom, more professional-looking domain name, cdn.dukaan.app, that pointed to the ugly CloudFront address. The new image URLs now looked like this: https://cdn.dukaan.app/seller123/product.jpg
We deployed the code change. The surgery was complete.
The Result: A Faster Site and a Cheaper Bill
The impact was immediate and dramatic.
We reached out to our seller in Brazil and asked her to check her store again. The first time she loaded it, it was still a bit slow. This was the "cache miss"-CloudFront's edge location in São Paulo was seeing her images for the first time and pulling them from our S3 bucket in Mumbai.
But the next time she, or any other customer in South America, loaded the page, the experience was completely different. The images were served instantly from the São Paulo edge location. Her page load time dropped from over 6 seconds to under 2. She was thrilled.
At the end of the month, Suumit and I looked at the AWS bill. The "Data Transfer Out from EC2" line item had been decimated. It was replaced by a new, much smaller line item for "CloudFront." We had cut our bandwidth costs by over 70%.
The two-headed dragon was slain. With one relatively simple architectural change, we had drastically improved the user experience for our global customers and saved a huge amount of money in the process.
- A CDN is a mandatory tool for any application with a geographically diverse user base. It is one of the easiest and highest-impact changes you can make to improve global performance.
- CDNs solve two problems at once: speed and cost. They reduce latency for your users by serving content from a nearby server, and they lower your costs by reducing expensive data transfer from your origin servers.
- Separate your static assets from your application server. Use a dedicated object storage service like Amazon S3 as the origin for your CDN. This is a more scalable and cost-effective architecture.
- The implementation is straightforward: Create a storage bucket for your original files, point a CDN distribution to that bucket, and update your application's HTML to use the new CDN URLs for your images, CSS, and JavaScript files.