Search code examples
sqldatabasesqliteandroid-roomuuid

What is the convention for ensuring unique ID's remain unique, once uploading to a cloud database


Say 2 users have an entry on their local device, but by some chance, their entry had the same uuid generated. What should happen when both users they try to upload them to a central data base

Is it normal to just "re-id" one of the entries?


Solution

  • The chance of two devices generating duplicate UUIDs is so small that most systems will ignore the possibility. From How unique is UUID?:

    after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.

    This of course is dependent on the volume of data being generated by devices, if these are some types of sensor that generate huge volumes of records (e.g. on aeroplane engines) then the chance of a non-unique UUID starts to become a possibility.

    If that's the case and you need to be sure of 100% uniqueness in the IDs then as others have mentioned you'll need to do one of the following:

    • assign each device a (large) unique range in which to generate IDs
    • regenerate all IDs when data is merged centrally
    • check all incoming IDs against existing IDs and modify any duplicates to make them unique (this could be a very expensive operation)