I am evaluating the following code to generate a short ID in my Node server (inspired by previous post: Short user-freiendly ID for mongo ):
> b = crypto.pseudoRandomBytes(6)
<SlowBuffer d3 9a 19 fe 08 e2>
> rid = b.readUInt32BE(0)*65536 + b.readUInt16BE(4)
232658814503138
> rid.toString(36).substr(0,8).toUpperCase()
'2AGXZF2Z'
This may not guarantee uniqueness, but my requirements are to have a short ID of maximum length 8 characters and it must also be all upper case. The purpose of this is to make the ID user friendly.
To ensure that there are no collisions, I am planning to create a collection in MongoDB that contains documents that map the short ID, which will be an indexed field, onto the MongoDB ObjectID of the actual document I want the short ID to refer to.
What is the best strategy for doing this to ensure scalability and performance in a concurrent environment where multiple process on multiple physical servers will be checking for short ID uniqueness?