Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have my website assets stored on Amazon S3 and served by CloudFront, from a separate domain name, to act as a cookie-less domain (so website.com for the website and websitestatic.com for static assets). I set a far ahead Expires header, so they can be cached by the browser.

Google PageSpeed recommends: Parallelize downloads across hostnames. I can achieve this by adding subdomains pointing to the same CloudFront distribution and then randomize the URL to my assets, so I can have media1.websitestatic.com, media2.websitestatic.com, media3.websitestatic.com.

My question is, will browsers recognize individual files as being the same as pre-cached ones, if they are served from different subdomains? As media1, media2, etc...all serve the same files, if over two page requests the same image was served from media1 first, then media2 next, will a browser recognize this or will it download it again?

I can randomize the subdomain URL in the HTML files that serves the file (media1, media4, media8, etc...) but I can't store this random number for any particular file so that it is served from the same subdomain every time.

I'm guessing the answer is no, they won't, so if I was to implement random subdomains, I'd negate the advantage of having a cached version, as theoretically every page request you could get a different subdomain for the same file.

share|improve this question
2  
I'd recommend not adding too many hostnames, otherwise you hit the separate problem of too many hostname lookups. –  DisgruntledGoat Feb 9 at 19:09

2 Answers 2

up vote 4 down vote accepted

will browsers recognize individual files as being the same as pre-cached ones, if they are served from different subdomains?

No. Two identical files served from different locations are different files as far as the browser (cache) is concerned.

The URL is the key by which the file is cached by the browser.

As media1, media2 etc all serve the same files, if over two page requests the same image was served from media1 first, then media2 next, will a browser recognize this or will it download it again?

It will be downloaded again.

share|improve this answer

The browser has no way to know that two distinct URLs are the same file.

I think the underlying question you are left with is: How do I host my files on multiple domains without getting as many downloads as there are domains (wow. is that English? sorry I'm French)

You can handle that by choosing wisely where your files are coming from. Two commons methods:

  • Use a deterministic algorithm to decide which domain to pick (i.e. no random in any way) For instance, "all JS goes to 1, all CSS to 2, all the rest to 3", or "all files starting by an 'a' go to 1, 'b' 2, 'c' 3, 'd' 4...

  • More difficult, but still doable in any language I know: Do consistent sharding. Take the URL, appply MD5 to it (or SHA1 or whatever good hash function), cast it to an integer number, take it's modulo with the number of domains you have. Example: "/wp-content/photos/2013/01/cat.jpg" MD5-hashes to 25d81ba602071aa703c98dfaa452322d, which converts to integer 50303532743047232046842026848642404426, which, given 2 domains, says "I belong to domain 0", because 50303532743047232046842026848642404426 % 2 = 0.

With both solutions, you will spread your files, but every one goes to only one domain. Second solution is better when you have well structure filenames, because it guarantees that they are evenly distributed, whereas first solution is faster, simpler, but may send all images in one domain, and get you no performance boost in the end.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.