Pie.host

What Is CDN and How To Build One

Everything you need to know about CDN and how to build one, to speed up website content delivery.

Rabikant

6 min read

Ever wondered how websites load images almost instantly, even when visitors are accessing them from the other side of the world?

A big part of the answer is a CDN.

A Content Delivery Network (CDN) helps deliver website content from servers closer to the visitor instead of making every request travel back to the original server. This reduces latency, improves loading times, and takes some of the load off your main infrastructure.

You don't necessarily need to build a massive infrastructure company to get started with one. In this guide, we'll walk through how to build a basic CDN using PieBucket, Pie.host's object storage service.

Let's get started.

What is a CDN?

A CDN, or Content Delivery Network, is a distributed network of servers that stores and delivers copies of your website's static content.

Think about a website hosted on a server in Mumbai. When someone from London visits it, their browser has to request images, CSS files, JavaScript, and other resources from that server.

The greater the physical distance between the visitor and the server, the longer the data can take to travel.

A CDN solves this problem by keeping copies of those files in multiple geographical locations.

Instead of fetching an image from Mumbai every time, a visitor in London can receive it from a nearby CDN server.

The result?

  • Faster page loading.
  • Lower latency for static assets.
  • Reduced traffic to your origin server.
  • Better handling of sudden traffic spikes.

What do you need to build a CDN?

At its simplest, a CDN setup needs three things:

  1. Origin storage: A place where your original files are stored.
  2. Delivery layer: A way to serve those files to visitors.
  3. Caching: A mechanism to keep copies of frequently requested files closer to users.

We'll use PieBucket as our origin storage.

PieBucket is Pie.host's object storage service. You can store files such as images, videos, documents, and other static assets without having to manage a traditional file server.

We'll then put a caching layer in front of it to serve content efficiently.

Step 1: Create a PieBucket storage bucket

First, head over to Pie.host and create an account if you don't already have one.

Open the PieBucket dashboard and create a new bucket.

Choose a meaningful name. For example:

my-website-assets

This bucket will hold the static files you want to deliver through your CDN.

You can upload:

  • Website images
  • CSS and JavaScript files
  • Fonts
  • Videos
  • Downloadable documents

Think of this bucket as the origin of your CDN.

Step 2: Upload your static assets

Once your bucket is ready, upload a few files.

For example, you might have:

my-website-assets/
โ”œโ”€โ”€ images/
โ”‚   โ”œโ”€โ”€ logo.png
โ”‚   โ”œโ”€โ”€ banner.webp
โ”‚   โ””โ”€โ”€ product-1.jpg
โ”œโ”€โ”€ css/
โ”‚   โ””โ”€โ”€ style.css
โ””โ”€โ”€ js/
    โ””โ”€โ”€ app.js

Keep your file structure organized. This becomes especially useful when your website grows and you have hundreds or thousands of assets.

Make sure the files have the appropriate content types and access permissions for the way you intend to serve them.

Step 3: Make your assets accessible

Your CDN needs a way to retrieve files from PieBucket.

Configure the bucket's access settings according to your use case. If you intend to serve public website assets, those objects must be accessible to your delivery layer.

For example, a public image might be accessible through an object URL provided by PieBucket.

Keep your storage credentials private. Never expose access keys or secret keys in frontend JavaScript.

If your bucket is private, use an authenticated origin connection or signed URLs rather than making the entire bucket public.

Step 4: Put a caching layer in front

Now comes the important part.

Object storage gives you a place to store files, but a CDN also needs a delivery and caching layer.

You can put a CDN or reverse proxy in front of your PieBucket origin.

The basic architecture looks like this:

         Website Visitor
                |
                v
          CDN / Cache
                |
         Cache Hit?
          /       \
        Yes        No
         |          |
         v          v
      Return    PieBucket
      Cached     (Origin)
      File          |
                    v
              Store in Cache
                    |
                    v
                Return File

When a visitor requests a file:

  1. The CDN checks whether the file is already cached.
  2. If it is cached, the CDN returns the cached copy.
  3. If it isn't, the CDN retrieves the file from PieBucket.
  4. The CDN stores a copy according to its caching rules.
  5. Future visitors can receive the cached copy without repeatedly accessing the origin.

This is called cache-hit and cache-miss handling.

Step 5: Configure cache headers

Caching is only useful when your delivery layer knows how long it should keep a file.

This is where HTTP cache headers come in.

For example:

Cache-Control: public, max-age=31536000, immutable

This tells browsers and compatible caching layers that the resource can be cached publicly for one year.

This works particularly well for versioned assets that never change after deployment.

For frequently updated files, use shorter cache durations:

Cache-Control: public, max-age=3600

For private or sensitive content, avoid public caching.

A good approach is to use versioned filenames:

app.v1.js
app.v2.js
logo.a81f3c.webp

When a file changes, upload it with a new filename. This prevents visitors from receiving an outdated cached version.

Step 6: Connect your website

Once your storage and caching layers are configured, update your website to use the CDN URLs.

Instead of:

<img src="https://your-origin.com/images/logo.png" />

You can use:

<img src="https://cdn.example.com/images/logo.png" />

Your website now requests static assets through your delivery layer.

You can gradually move images, stylesheets, JavaScript files, and other static resources to this setup.

Step 7: Test your CDN

Before moving all your production assets, test the setup.

Check the following:

  • Can you access uploaded files through your CDN domain?
  • Are files being cached?
  • Do repeat requests return cached content?
  • Are cache headers configured correctly?
  • What happens when you update an existing asset?
  • Does the CDN fall back to PieBucket when an object isn't cached?

You can inspect HTTP response headers using curl:

curl -I https://cdn.example.com/images/logo.png

Look for cache-related headers such as:

Cache-Control: public, max-age=3600
Age: 120

The exact cache-status headers depend on your CDN or caching provider.

You should also test your website from different geographical locations to understand how much the delivery layer improves performance.

Why use PieBucket as your CDN origin?

Running your own storage infrastructure for static assets can become expensive and difficult to maintain.

PieBucket gives you an object storage layer without requiring you to manage disks, file servers, or storage infrastructure yourself.

You can use it for:

  • Website images and media.
  • Application uploads.
  • Static website assets.
  • Backups and archives.
  • Files served through your own CDN.

And because your storage is separate from your application server, you can scale your application and storage independently.

Conclusion

Building a CDN doesn't necessarily mean deploying hundreds of servers across the globe.

You can start with object storage, add a caching delivery layer, configure your cache policies, and gradually build a more distributed delivery architecture as your traffic grows.

PieBucket gives you the storage foundation. Add the right caching layer in front, and you have the building blocks of a CDN.

Ready to get started?

Explore PieBucket and start building your own storage-backed content delivery setup with Pie.host.

Comments

No comments yet. Questions and corrections are welcome.