ID collision danger
This pseudo-random GUID algorithm is not globally unique, and could easily cause collisions in a multi-server environment, particularly if multiple Ids get generated in the same millisecond, because the V8 engine uses epoch time as a source of random entropy for random number generation. Collisions are actually quite common when multiple Ids are generated in the same millisecond using this technique.
https://github.com/punkave/apostrophe/blob/master/lib/apostrophe.js#L153
Possible solution: Switch to cuid.
For our needs it looks like using a better PRNG seeded by crypto.randomBytes would do the job.
On Sat, Feb 7, 2015 at 2:52 AM, Eric Elliott [email protected] wrote:
This pseudo-random GUID algorithm is not globally unique, and could easily cause collisions in a multi-server environment, particularly if multiple Ids get generated in the same millisecond, because the V8 engine uses epoch time as a source of random entropy for random number generation. Collisions are actually quite common when multiple Ids are generated in the same millisecond using this technique.
https://github.com/punkave/apostrophe/blob/master/lib/apostrophe.js#L153
Possible solution: Switch to cuid http://usecuid.org.
— Reply to this email directly or view it on GitHub https://github.com/punkave/apostrophe/issues/190.
*THOMAS BOUTELL, *DEV & OPS P'UNK AVENUE | (215) 755-1330 | punkave.com
Actually, looks like crypto.pseudoRandomBytes is that animal:
http://stackoverflow.com/questions/18130254/randombytes-vs-pseudorandombytes
On Tue, Feb 10, 2015 at 7:45 AM, Tom Boutell [email protected] wrote:
For our needs it looks like using a better PRNG seeded by crypto.randomBytes would do the job.
On Sat, Feb 7, 2015 at 2:52 AM, Eric Elliott [email protected] wrote:
This pseudo-random GUID algorithm is not globally unique, and could easily cause collisions in a multi-server environment, particularly if multiple Ids get generated in the same millisecond, because the V8 engine uses epoch time as a source of random entropy for random number generation. Collisions are actually quite common when multiple Ids are generated in the same millisecond using this technique.
https://github.com/punkave/apostrophe/blob/master/lib/apostrophe.js#L153
Possible solution: Switch to cuid http://usecuid.org.
— Reply to this email directly or view it on GitHub https://github.com/punkave/apostrophe/issues/190.
*THOMAS BOUTELL, *DEV & OPS P'UNK AVENUE | (215) 755-1330 | punkave.com
*THOMAS BOUTELL, *DEV & OPS P'UNK AVENUE | (215) 755-1330 | punkave.com
That should work, but you need to be aware that it's asynchronous.
Good point. We could use it to seed a better PRNG then. On Feb 10, 2015 1:22 PM, "Eric Elliott" [email protected] wrote:
That should work, but you need to be aware that it's asynchronous.
— Reply to this email directly or view it on GitHub https://github.com/punkave/apostrophe/issues/190#issuecomment-73753173.
You should definitely NOT write your own PRNG, (it's a much harder problem than you might think) but if you find one that is commonly used and well tested by the community, that could be an option.
You should also be aware that in order to create enough entropy, you'd need to update the seed very frequently, which could hurt the performance of your app and could also fail to match the built-in prng that you're currently using.
This problem isn't as trivial as it sounds, which is why I've seen v4 UUIDs fail uniqueness in JS (multiple engines), PHP, Java, and several DBMS solutions. The trouble is, this problem is pretty much invisible until you scale to millions of MAUs.
If you don't test the solution rigorously with cross-host tests, you may start to see really bad bugs when you reach into the millions of users. I've seen this cause problems at three different companies I worked at with users in the millions of MAUs, and it was responsible for the single biggest PR disaster I've ever seen at a company I worked for.
This is the whole reason I wrote cuid.
Thanks for the info @ericelliott. We'll look into if it makes sense to integrate cuid into our backend refactor for version 0.6.
We're now using cuid for id generation in 0.6