Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID Generated randomly is having duplicates

I'm using the below function to generate UUID

UUID.randomUUID().toString()

In production we have 50+ servers (application server - each is a JVM on its own) and for requests that land in these servers, as a first step we generate a UUID which essentially uniquely identifies a transaction.

What we are observing is that in Server 6 and Server 11, the UUIDs generated are matching at least for 10 to 15 messages per day which is strange because given the load i.e. about 1 million transactions a day, these UUIDs being duplicate within the same day is very odd.

This is what we have done so far

  1. Verified the application logs - we didn't find anything fishy in there, all logs are as normal
  2. Tried replicating this issue in the test environment with similar load in production and with 50+ servers - but this didn't happen in the test environment
  3. Checked the application logic - this doesn't seem to be an issue because all other 48 servers except 6 and 11 which have a copy of the same code base is working perfectly fine and they are generating unique UUIDs per transaction.

So far we haven't been able to trace the issue, my question is basically if there is something at JVM level we are missing or UUID parameter that we need to set for this one off kind of an issue?

like image 234
deepesh Avatar asked Jan 15 '15 19:01

deepesh


People also ask

Can random UUID be duplicate?

From the documentation and wikipedia we see that randomUUID is good - but there is a very small chance that duplicates can be generated.

How do I prevent duplicate UUID?

Alternatively, you can rely upon a randomly generated ID. In our experience, a random number of 7 digits is sufficiently random, to avoid duplicate IDs. Do this most simply with SurveyCTO's uuid() function in a calculate field to create and ID that is a random combination of numbers and letters.

Is UUID randomly generated?

This version of UUID is generated randomly. Although the random UUID uses random bytes, four bits are used to indicate version 4, while two to three bits are used to indicate the variant. These can be created using a random or pseudo-random number generator.

Can you have two UUIDs the same?

a uuid is “in theory” a 128-bit random number. that's a HUGE number (like atoms in the universe big) and if the algorithms were truly random (which they're not) it would be effectively impossible to have a duplicate.


1 Answers

Given time, I'm sure you'll find the culprit. In the meantime, there was a comment that I think deserves to be promoted to answer:

You are generating pseudo random UUIDs at multiple locations. If you don't find other bugs, consider either generating all the pseudo random UUIDs at one location, or generate real random UUIDs

So create a UUID server. It is just a process that churns out blocks of UUIDs. Each block consists maybe 10,000 (or whatever is appropriate) UUIDs. The process writes each block to disk after the process verifies the block contains no duplicates.

Create another process to distribute the blocks of UUIDs. Maybe it is just an a web service that returns an unused block when it gets a request. The transaction server makes a request for a block and then consumes those UUIDs as it creates transactions. When the server has used most of its assigned UUIDs, it requests another block.

like image 102
ahoffer Avatar answered Oct 22 '22 17:10

ahoffer