I'm working on a dotnet app where I need to generate a unique object ID for each database row. Since these records need to sync across branches, traditional auto-number fields won't cut it. I've been looking into using GUIDs with the C# code `var guid = Guid.NewGuid().ToString("N");`, which produces a 32-character alphanumeric ID. However, I'm wondering if GUIDs are truly unique or if there's a chance for duplication. Additionally, to save on storage and improve readability, can I just use the first 12-14 characters of the GUID without risking collisions?
1 Answer
Mathematically speaking, a GUID isn't guaranteed to be unique, but practically, the likelihood of generating a duplicate is extremely low. It's based on random number generation, and while technically duplicates can happen, you’d have to generate an unfathomable number of them for that to be a concern. So, you can use GUIDs with confidence, but just don't shorten them to say 12 characters if you want to maintain that uniqueness!
I had the same doubts when I started! From what I read, if you generate around 100 GUIDs a second, the chances of getting a duplicate in a year are still pretty slim.