Mobile app version of vmapp.org
Login or Join
Cooney921

: Do browsers recognize the same files served by different subdomains? I have my website assets stored on Amazon S3 and served by CloudFront, from a separate domain name, to act as a cookie-less

@Cooney921

Posted in: #AmazonCloudfront #Browsers #Cache #Cdn #Subdomain

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.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Cooney921

2 Comments

Sorted by latest first Latest Oldest Best

 

@Martha676

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.

10% popularity Vote Up Vote Down


 

@Jamie184

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme