3 ms·
With respect to uniqueness, you can avoid collisions using, for example, some kind of sharding scheme, similar to what Instagram did: https://engineering.insta
by grzm 9y ago
With respect to uniqueness, you can avoid collisions using, for example, some kind of sharding scheme, similar to what Instagram did:
https://engineering.instagram.com/sharding-ids-at-instagram-1cf5a71e5a5c https://engineering.instagram.com/sharding-ids-at-instagram-...
- kbenson 9y agoIt's been quite a while since I've needed to, but when I was setting up HA databases, I just set the auto increment amount to some number more than the number of databases, and set the starting offset for each database to sequential ints (1,2,3...). This results in databases that can't generate duplicate ids. As a bonus, you know what database was used to create each record.
- grzm 9y agoThat's one way if you're using sequences to generate ids. If you're encoding information in UUIDs, you also have to take into account how many bits you want to allocate to the database (shard) id. One method I've used is to hard-code the shard id as the return value in a function: effectively a constant. (Edit to add: This serves the same purpose as setting the starting offset of the sequence.) You do have a bit of custom code on each database instance in this case, which is its own tradeoff.