0

My website serves many images and I would like to parallelize the image requests by requesting via subdomain.

e.g. http://mydomain.com/image1.png becomes http://img[0-9].mydomain.com/image1.png

What I am searching for is a hash algorithm that will allow me to convert

"some-random-image-name.png" to a single digit range 0-9

so that I can load images via:

http://img0.mydomain.com/foo.png

http://img1.mydomain.com/foo.png

etc.

I can not use a random number because that would not be efficient. If the image loads from img1 I would like the next request for that image to also go to img1.

This would need to be fast as it will be called many times per page load.

Thank you for your ideas.

Adding node:

Why do this at all?

The browser will request only a limited number of requests from a single server. This is discussed in this post here:

Max parallel http connections in a browser?

In order to have a page with many static assets such as images load faster you add subdomains. Even if they are still coming from the same server.

Community
  • 1
  • 1
Kevin
  • 3,690
  • 5
  • 35
  • 38
  • Are you saving these images in a database anywhere? If so, just use the image ID. – Justin Wood Dec 16 '13 at 22:13
  • 1
    I guess I don't understand the need for this at all. If you are using reasonable CDN provider, you should not have such a need. If for some reason, you still need to have different sets of servers serving the content, you could perhaps use a DNS load balancer to meet your needs. – Mike Brant Dec 16 '13 at 22:13
  • Use the last digit of the image's ID in your database. That'll give you an automatic digit-balanced 0-9 range. No guarantees that image 1001 won't be so popular that it kills the img1 server. – Marc B Dec 16 '13 at 22:14
  • if you have `arandomimage.png` and `anotherimage.png` that both evaluate to `5` in your hypothetical one digit hash, then how are you to distinct them? – php_nub_qq Dec 16 '13 at 22:15
  • Added a note for those that are asking why do this at all. – Kevin Dec 16 '13 at 22:34

1 Answers1

2

This should be quite fast:

$hash = abs(crc32($filename) % 10);
r3mainer
  • 23,981
  • 3
  • 51
  • 88