Image Magic

In this video you will learn how to 'trick' your client's browser into initiating more than one request concurrently. This is really handy if you have a relatively small budget, or are running a small scale project that doesn't have load balancers or expensive content delivery networks to serve up your content.

This isn't a PHP trick, it can apply to any programming language that serves data to the web.

The gist of this tip is to create multiple DNS entries for your site.

Let's imagine we were doing this for

Firstly, head over to your DNS panel at your hosting provider. Inside Linode this would be in the DNS Manager section, once logged in.

Choose your domain name from the list, then scroll down and add in a new CNAME record.

You want to repeat this a few times - having entries somewhat similar to:

  • Hostname: cdn{0-4}
  • Aliases to:
  • TTL: Default

At the end of this process, you should have between three and five new CNAME entries.

For each CNAME entry you need to create a new Apache / NGiNX vhost on your server.

The details for each vhost can be extremely basic. You need a directory to serve files from, and that's about it.

Then, inside each of these new sites you can symlink your images / assets directory, and programmatically determine one of the new CDN links to serve content from. In my case, I wrote a small function that took the first letter of the image, and then routed it somewhat similar to:

  • A,B,C,D,E - cdn0
  • F,G,H,I,J - cdn1
  • K,L,M,N,O -cdn2

For very little effort, this setup works surprisingly well. It's not enterprise standard, and shouldn't be considered as such, but for a small site wanting to do more with less, it's a simple tip that works well.

A word of caution - if your $5 digital ocean droplet is already getting slammed, doing this will make things worse. Use appropriately, and don't implement this if you have no idea what problem you are trying to solve, or why!


# Title Duration
1 Image Magic 05:03