Dis.Uuid 1.1.0
Dis.Uuid
A high-performance .NET library for generating UUID version 7 (UUIDv7) identifiers with time-ordering capabilities.
๐ฏ What is UUID v7?
UUID version 7 is a time-ordered UUID variant that combines:
- 48-bit timestamp (millisecond precision)
- 12-bit random data for sub-millisecond ordering
- 62-bit random data for uniqueness
This design ensures:
- โ Chronological ordering - newer UUIDs are lexicographically greater
- โ Database performance - excellent for primary keys and indexes
- โ Global uniqueness - collision-resistant across distributed systems
- โ Thread safety - safe for concurrent use
๐ Installation
dotnet add package Dis.Uuid
Or via Package Manager Console:
Install-Package Dis.Uuid
๐ Usage
using Dis.Uuid;
// Generate a new UUID v7
Guid id = UuidV7.New();
Console.WriteLine(id); // e.g., 018f-4230-1234-7000-abcdef123456
// Use in your models
public class Order
{
public Guid Id { get; set; } = UuidV7.New();
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
// ... other properties
}
๐ง Features
- High Performance: Uses
Span<byte>andThreadLocal<RandomNumberGenerator>for optimal performance - Thread Safe: Concurrent generation across multiple threads
- Memory Efficient: Zero heap allocations during UUID generation
- Standards Compliant: Follows RFC 4122 UUID v7 specification
- .NET 8+ Optimized: Takes advantage of latest .NET performance improvements
๐ Performance
UUID v7 generation is extremely fast and efficient:
// Benchmark example
var sw = Stopwatch.StartNew();
for (int i = 0; i < 1_000_000; i++)
{
var uuid = UuidV7.New();
}
sw.Stop();
Console.WriteLine($"Generated 1M UUIDs in {sw.ElapsedMilliseconds}ms");
๐ Time Ordering
One of the key benefits of UUID v7 is natural time ordering:
var id1 = UuidV7.New();
await Task.Delay(1); // Small delay
var id2 = UuidV7.New();
// id2 will always be lexicographically greater than id1
Assert.True(string.Compare(id1.ToString(), id2.ToString()) < 0);
๐ฏ Use Cases
Perfect for:
- Database Primary Keys - Better performance than traditional UUIDs
- Distributed Systems - Global uniqueness with time ordering
- Event Sourcing - Natural chronological ordering of events
- Logging Systems - Time-ordered log entries
- Microservices - Consistent ID generation across services
๐ Thread Safety
The library is fully thread-safe and can be used concurrently:
// Safe to use across multiple threads
var tasks = Enumerable.Range(0, 10)
.Select(_ => Task.Run(() =>
{
var ids = new List<Guid>();
for (int i = 0; i < 1000; i++)
{
ids.Add(UuidV7.New());
}
return ids;
}));
var results = await Task.WhenAll(tasks);
// All generated UUIDs will be unique
๐งช Testing
The library includes comprehensive tests covering:
- Uniqueness validation
- Time ordering verification
- Thread safety testing
- Performance benchmarks
Run tests:
dotnet test
๐ Requirements
- .NET 8.0 or higher
- Compatible with all .NET 8+ workloads (Console, Web, Desktop, etc.)
๐ค Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if needed
- Submit a pull request
๐ License
This project is licensed under the BSD 3-Clause License - see the LICENSE.txt file for details.
๐ References
Made with โค๏ธ for the .NET community
No packages depend on Dis.Uuid.
v1.1.0: Added compatibility with .NET Standard 2.0 (supports .NET Core 2.0+, .NET Framework 4.6.1+)
.NET 6.0
- No dependencies.
.NET 8.0
- No dependencies.