Matt
Matt

Reputation: 4387

cdn dynamic query? or not

I need some feedbacks on the use of dynamic query for a CDN. I'm would know about the best choise between dynamic and static query.

In my case, we have a web game with a lot of static content. We performing realeases each two weeks and on the CDN we add some content but we also update a few of them.

I see three possible ways: 1) use versionning for each file(ex: welcome01.jpg, welcome02.jpg) 2) use dynamic query for each file(ex: welcome.jpg?v=01, welcome.jpg?v=02) 3) use naming without versionning and for each file which has been modify, do an invalidation(purge) on the CDN.

For me, the solution 3 is the worst because we can't set a high cache and we must invalidate each updated files, already in the CDN.

Between solution 1 and 2, I think the 1 is the best because when I upload my new static content on my orign server and perform a deployment on th CDN, all my content is send at each Edge. In this case I can set a high cache content value. In the solution 2, I think the performance are worst than with the solution 1 because when I add new content in my origin server(with some file updating), only new files are send to the Edge. Updated's files are only available when a client execute a query with a new parameter(ex: welcome.jpg?v=03). In this case, the Edge server will download the welcome.jpg on my origin server and associates him with the new parameter. But he's not replicated in all other Edge.

For me solution 2 is less than solution 1 at the performance level but for both of them we can set a high cache content value.

Can you give me your opinion or more informations about that?

Thanks in advance.

PS: we use cloudfront but it's a general question for all CDN providers.

Upvotes: 1

Views: 268

Answers (1)

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10557

Not all proxy caches or ISP will cache resources with querystrings separately. In other words they might not invalidated your cache properly. More on that here

So I would have to say solution 1 is safer if you want to be sure your new files out there and downloaded. A new unique URI will get you this. Versioning your files will work well for this.

Depending on how your CDN works, you might be able to use mod-rewrite to force the download and not have to tediously version your files by hand.

For example if your CDN can pick up your changes via CNAME, for example this is how MaxCDN works, you could do something like html5boilerplate.

Filename-based cache busting

# If you're not using a build process to manage your filename version revving,
# you might want to consider enabling the following directives to route all
# requests such as `/css/style.12345.css` to `/css/style.css`.

# To understand why this is important and a better idea than `*.css?v231`, read:
# http://stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring

<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>

Folder-based cache busting

Or if you don't need/want versioning for each file and have to tediously change all your code and css. Something where your have an entire folder version system rather then file based might work better. For example let's say your physical resource files live in /assets/css and /assets/img and /assets/js you can use mod rewrite to make /v1.0/assets/* map to your real folder. This way you upload your new updated resources as normal and then increment your version variable in your site config and have all the resources point to the new folder. Here's the modrewrite for something like that.

<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^/v([0-9\.]+)/assets/(.+)$ /assets/$2 [L,QSA]
</IfModule>

Upvotes: 1

Related Questions