DigitalOcean Spaces is a great way to use your own server farm as a content delivery network. You can use it to serve up your own web and mobile applications, or you can use it to host other people’s websites. You can also use it to create an online store. One thing you need to be careful of is that DigitalOcean Spaces doesn’t have a lot of built-in security features. If someone gets access to your server farm, they could potentially do a lot of damage. So make sure you’re using strong passwords and encrypting your data. If you’re using DigitalOcean Spaces as a content delivery network, there are some things you can do to improve the performance of your site. One thing you can do is set up caching on your server farm so that pages load faster when they’re requested. Another thing you can do is set up throttling so that only certain pages or requests are processed at any given time.


The Spaces CDN works by using a network of edge servers to replicate your content across geographical regions. Users that request content from the CDN endpoint will be served by the edge node that’s closest to them.

Besides improving performance, the CDN reduces bandwidth costs by caching content and providing it via shorter delivery routes. It also offers a layer of extra redundancy in the event of a service failure. DigitalOcean will be able to continue exposing your content if an edge node fails by rerouting traffic to one of the others.

Enabling the CDN

Go ahead and create a new Space on DigitalOcean. You’ll be prompted to choose a datacentre region for your content. Each region is backed by multiple independent edge nodes.

 

You can enable the CDN at the time you create your Space or at any subsequent point. You’ll need to supply the same values whether you activate the CDN during Space creation or later on.

To configure the CDN for an existing Space, head to your DigitalOcean control panel, click Spaces in the left sidebar, and navigate to your target Space. Click the Settings tab at the top of the screen, then the “Edit” button next to “CDN (Content Delivery Network)”.

The “Use a custom subdomain” dropdown lets you attach your own subdomain that you’ll be able to use to access the Space’s content. This could look like my-space.example.com. DigitalOcean can automatically generate an SSL certificate via Let’s Encrypt, securing traffic to your Space. This requires you to already be managing your domain’s DNS records with DigitalOcean. Manual upload of an SSL certificate is offered as an alternative option when you’re using a different provider.

Without a custom subdomain, you’ll need to use DigitalOcean’s special cdn.digitaloceanspaces.com address when you want to load content via the CDN.

Here’s the correct URL if your Space is called my-space and it resides in the NYC1 datacentre:

Omitting the cdn part of the address results in you hitting the standard Spaces endpoint, without CDN support. This can be a common mistake when configuring third-party software – both URLs will work but only the cdn variant gives you the benefits of edge caching.

Setting the Cache TTL

The Edge Cache TTL is the other setting you can change when enabling your CDN. This defines how long individual edge nodes can cache your content for before considering it to be stale.

Getting this value right is important so your users receive content changes in a timely manner but without unduly jeopardizing CDN efficiency and performance. Edge nodes won’t look for changes to items they’ve retrieved from the central Spaces server until the TTL expires.

Long-lived large assets such as images and videos benefit from an extended TTL. DigitalOcean lets you use a maximum of 1 week in its default set of options. Conversely, files like documents or webpage components which are expected to change frequently might need a shorter TTL. This will reduce the delay between a change being uploaded and users getting to see it.

Treating content in your CDN as immutable is often an effective strategy when your system can be engineered to support it. Use a long TTL by default to reduce bandwidth and improve performance. If a resource does need to be changed, upload a new version with a different filename so it’s cached independently of the original. When this approach isn’t viable, it can be best to use multiple Spaces with different TTL policies to separate your long-lived and frequently changing content.

One final option is to manually override the TTL setting on specific items in your Space. This is achieved by setting the max-age metadata field. You can do this within the Spaces control panel or during upload from an S3-compatible client.

Find the item, click the “More” button to the right of its name, and select “Manage Metadata” from the menu. Click “Add Custom Pairing” and create a new field called x-amz-meta-max-age. Set your desired TTL in seconds as the value.

Evicting Items From The CDN Cache

You can purge your CDN’s cache from the DigitalOcean control panel. Options are available to either clear the cache entirely or evict individual items.

To initiate a complete purge, navigate to your Space via Spaces > . Click the Settings tab at the top and scroll to the “CDN (Content Delivery Network)” section. Click the “Purge Cache” button next to the heading name.

This will display a dialog that lets you choose the directories to purge from the cache. Select the appropriate level in the hierarchy and then press “Purge Selected Directories.” The cache invalidation request will be sent to the edge servers. It should have an almost immediate effect.

Individual items can be evicted by browsing the Space’s content and clicking the “More” button next to an object. Select “Purge from CDN Cache” from the menu that appears. You can purge multiple items simultaneously by ticking the checkboxes to the left of the screen, then selecting “Purge from CDN Cache” from the “Actions” menu that’ll appear in the toolbar.

Manual evictions are helpful when you need a quick cache bust to immediately deliver new content to your users. However regular use of this control usually indicates your TTL settings aren’t appropriate for your application. It’s best to develop your system so objects enter the cache with the correct TTL so you can have a hands-off management approach.

Disabling a Space’s CDN

You can disable the CDN for a Space at any time. This will retain all your content but you’ll need to use the regular digitaloceanspaces.com endpoint to access it. Objects will be immediately removed from the edge network so your existing CDN-based URLs will stop working.

Find your Space in the DigitalOcean control panel by navigating to Spaces > . Click the Settings tab at the top of the screen, then the “Edit” button next to the “CDN (Content Delivery Network)” section.

 

Press the “Disable CDN” button and then the blue “Save” button to confirm your change. You’ll need to type in the name of your Space to confirm your intentions. Click “Disable” to purge your content from the CDN, unlink custom subdomains, and revoke any issued SSL certificates.

Summary

The DigitalOcean Spaces CDN enhances performance and reduces bandwidth use by caching your object storage content on local edge nodes close to your users. You can access the CDN over your own subdomain or stick with the default cdn.digitaloceanspaces.com.

Configuring and using the CDN is usually straightforward but you should stay conscious of your TTL settings. It’s important to select a caching lifetime that will adequately balance your application’s typical content update frequency with the overall performance of your Space.