2

I want to generate unique uid for objects which will be stored in database. I am using UUID npm package to generate unique id. I want to know is there a chance that any of the id's generated by this package can repeat themselves? like the following answer https://stackoverflow.com/a/38944736/13126651 speaks about nano id package and how it doesnt generate duplicates, is there same conviction for uuid package too?

ofcourse there can be solution to check database before storing but since this will I/o operation i want to avoid this and since this will be in REST API so i cant even maintain state.

Note :- i am specualting that my API will hit around 1000 times in a day so is there chance of repeating uid's?

Update :- How unique is UUID? this question talks about how UUID are unique but if someone can explain does UUID npm package does something extra to ensure it never repeats?

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
  • 1
    Duplicate of [How unique is UUID?](https://stackoverflow.com/questions/1155008/how-unique-is-uuid) – esqew Jul 06 '21 at 17:07
  • If you are only ever generating UUIDs from one server process, then you can guarantee it is unique by appending an increasing counter to it. Each time you generate one, you increase the value of the counter and append the new value to the end of the UUID such as `lkasjfAalsjf-4` where the `-4` is from your counter. The laws of probability would say you probably don't need to do that, but if you still aren't sure, you can force it unique with the counter. And, you have to persistently store the counter so if your server restarts, the counter value is preserved. – jfriend00 Jul 06 '21 at 20:26

1 Answers1

1

No, it cannot:

No, a UUID can't be guaranteed to be unique. A UUID is just a 128-bit random number. When my computer generates a UUID, there's no practical way it can prevent your computer or any other device in the universe from generating that same UUID at some time in the future.

Jake
  • 97
  • 1
  • 9