HomeNikola Knezevic

In this article

Banner

Should we replace GUIDs?

04 Oct 2024
5 min

Sponsor Newsletter

GUID (Globally Unique Identifier) is a 128-bit integer (16 bytes) that can be utilized across any computer or network when a unique identifier is needed.

The likelihood of such an identifier being duplicated is extremely low, which is why it is heavily used in distributed systems.

However, there are two interesting alternatives to classic GUIDs: one is coming with .NET 9, and the other is already available through a third-party library.

Problem with GUIDs?

UUIDs (Universally Unique Identifiers) are unique identifiers that can be generated independently, without the need for a central authority.

GUIDs are based on version 4 specification of UUIDs.

This is the most common UUID version and it ensures that UUIDs are generated from completely random numbers.

In C#, you can easily create one using Guid.NewGuid() in your application, and it has served us well:

csharp
Guid id = Guid.NewGuid();
Description of Image

However, the problem with GUIDs, even though they are truly random, is that they are not time-ordered.

That's why I'm introducing you to GUID version 7 and ULIDs.

GUID Version 7

GUIDs Version 7 is coming soon with .NET 9 and, as you might expect, it is based on the version 7 UUID specification.

UUID Version 7 is a time-ordered UUID that is generated using a timestamp and random bits to create a unique identifier. The timestamp takes 48 bits, and the rest is reserved for random bits.

Guid.CreateVersion7() will generate version 7 UUIDs, and it also supports an overload that accepts a DateTimeOffset.

csharp
Guid id = Guid.CreateVersion7();
Description of Image

However, there is a third-party alternative called ULID and you don't need to wait to start using it!

ULID

ULID (Universally Unique Lexicographically Sortable Identifier) is also a 128-bit identifier designed to be universally unique but also sortable, similar to GUID version 7.

Although ULIDs aren't as compact as purely numeric IDs, they are easier to read for humans compared to traditional UUIDs that use a hexadecimal format.

ULIDs are generated similarly to traditional GUIDs, using Ulid.NewUlid().

If needed, you can convert them to a hexadecimal format like GUIDs by using the .ToGuid() method.

csharp
Ulid ulidId = System.Ulid.NewUlid();
Guid guidId = System.Ulid.NewUlid().ToGuid();
Description of Image

I was using this popular library with over 5 million downloads, check it out on GitHub, and if you like it, give it a star:

GitHub: Cysharp/Ulid

When I first started using ULIDs, GUID version 7 hadn't even been announced yet. When I ran some benchmarks, I was surprised by the results - ULIDs were faster than traditional GUIDs.

What's even more interesting is that when I benchmarked the new GUID version 7, I found that ULIDs were more than twice as fast compared to the new GUIDs.

Description of Image

Conclusion

Since .NET 9 is still not released, I hope they will make some performance improvements. In any case, if you want a time-ordered solution with good performance, you can use ULIDs.

If you want to conduct additional testing, you can find the source code here:

Source Code

NOTE: You will need .NET 9 SDK and Visual Studio Preview!

I hope you enjoyed it, subscribe and get a notification when new blog is up!

Subscribe

Stay tuned for valuable insights every Thursday morning.