views:

37

answers:

3

I'm thinking about adding another static server to a web app, so I'd have static1.domain.tld and static2.domain.tld.

The point would be to use different domains in order to load static content faster ( more parallel connections at the same time ), but what 'troubles' me is "how to get user's browser to see static1.domain.tld/images/whatever.jpg and static2.domain.tld/images/whatever.jpg as the same file" ?

Is there a trick to accomplish this with headers or I'll have to define which file is on which server?

+2  A: 

No, there's no way to tell the browser that two URLs are the same -- the browser caches by full URL.

What you can do is make sure you always use the same url for the same image. Ie. all images that start with A-M go on server 1, N-Z go on server 2. For a real implementation, I'd use a hash based on the name or something like that, but there's probably libraries that do that kind of thing for you.

Jonathan
+1  A: 

You need to have both servers able to respond to requests sent to static.domain.tld. I've seen a number of ways of achieving this, but they're all rather low level. The two I'm aware of:

  • Use a DNS round-robin so that the mapping of hostnames to IP addresses changes over time; very large websites often use variations on this so that content is actually served from a CDN closer to the client.
  • Use a hacked router config so that an IP address is answered by multiple machines (with different MAC addresses); this is very effective in practice, but requires the machines to be physically close.

You can also do the spreading out at the "visible" level by directing to different servers based on something that might as well be random (e.g., a particular bit from the MD5 hash of the path). And, best of all, all of these techniques use independent parts of the software stack to work; you can use them in any combination you want.

Donal Fellows
+1  A: 

This serverfault question will give you a lot of information: Best way to load balance across multiple static file servers for even an bandwidth distribution?

Kevin Hakanson