In this article, we are going to learn how actually GUID is created.
Normally, we are using GUID commonly for uniqueness in the project, before start are we know about GUID? what is GUID? How to create GUID? If you know these answers, you ever wonder how actually its created and rare chance to duplicates?
So, first, let’s start with What is GUID?
GUID stands for Globally Unique Identifier or is also known as UUID which stand for Universally Unique Identifier. Normally Microsoft technology developers work with GUID and rest developer work with UUID. 16 bytes (128 bits) is acceptable for uniqueness.
How to create GUID?
You can create GUID by below line
var guid = Guid.NewGuid();
There are many methods for GUID, which you can use as per need.
The Main part is How GUID Is Generated?
If you noticed GUID that GUID is a combination of four-part. for example 70122D63-6FB3-E911-8369-ABBAE7037764,
Let’s break down GUID in four-part to understand.
- First 60 bits (7.5 Bytes) of timestamp
- Second 48 bits (6 Bytes) of computer identifier,
- Third 14 bits (1.75 Bytes) of uniquifier
- Last 6 bits (0.75 Bytes) are fixed,
Total Bits = 60 + 48 + 14 + 6 = 128 Bits
Total Bytes = 7.5 + 6 + 1.75 + 0.75 = 16 Bytes
There are many GUID generation algorithms. All algorithms are designed in a way that not generates the same GUID twice, but here is another question come in mind that, Is GUIDs are 100% unique?. The answer is No, there is not guaranteed to be uniqueness. but there is a rare and very small chance or in the next fifty billion trillion years of being generated the same GUID twice. For example, just assume the sea, which has 7 * 1022 fishes, every fish could have 8.8×1015 universally unique GUIDs.
There are some key point which we should keep in mind,
- Don’t use GUID as random numbers.
- Don’t split GUID in half or any part, otherwise, it will lose uniqueness.
- There are lots of algorithms to generate GUID.
- We can not prevent a user to use earlier generated GUID which can create collision if we, not setup that column as a primary key.
- Each GUID occupies 16 bytes, which occupy more space if you have millions of records.
- We should ignore GUID for a small project, as its effects on database performance.
Hope you guys found something useful, please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.