Say for example I was running a popular image host and all my images have an ID which they can be accessed by. If I wanted this ID to be sequential and not random what would be the best way to make sure two images uploaded at the same time don't get assigned the same ID by different processes/servers.The best way I can think of is having different processes reserve a block for example one might reserve 1-1000 and the next one could reserve 1001-2000 so the IDs would never clash.What is the best solution to this problem and how is it usually solved. It seems like most sites use a random ID. Is there a reason for this?
How to pick sequential item IDs and avoiding a clash
architecture
The solution you described is called Hi/Lo and is one of the ways to solve this.Reason why some sites still use random numbers are few : With Hi/Lo, you still need some central authority to keep the track of the blocks. This might be problematic, because the stability requirements are huge for such system.Related to above, with random numbers being used, the systems generating them don't have to rely on specific system, making them more portable.Second reason is that it is not critical error if two items have same ID. It most probably result in error being shown to user if two same IDs are generated. But if the random space is big enough, sun will run out of fuel long before you get good enough chance to generate two same numbers. This chance goes even lower with some GUID generations guaranteeing uniqueness on single system. So generating same number twice would be an issue only in some extreme cases.