Search code examples
nginxscalabilityreverse-proxysharding

Sharding of images / distrubuting on multiple Server


i have a very large product database, and the free space is beeing very small to save all images. I want now to add more server and have to distribute the images on multiple Server. But i need a consistant way to shard the product images among all server. The ID is auto_increment so i can´t use it because sometimes products get deleted and their images too. So in that case if I just say: ID 1-10M on server A, 10M > ID <20M on server B etc.. I will end up with an empty Server A when all products with ID 1 to 10M get deleted.

And how to handle this when it gets to using the images on the website. All images should be available under one domain eg. staticexample.com using nginx to map the request based on the uri to the right upstream.

I need someone that point me in the right way.

Ps. i don´t want just add more space to my server because it will be just a temporary solution. I need a horizontal scale.

I appreciate any help

Thanks


Solution

    • Shard not based on ranges, but on a remainder, for example server_id = image_id % total_server_num. It is unlikely that images will be removed based on this pattern.
    • You may use subdomains or subfolders (static.site.com/1/, static.site.com/2/) and map todifferent backends.