From INT to UUIDv7: The Architecture of Identifiers in Modern Systems
Why sequential integers pose major security and scalability risks. Discover UUIDv7: time-ordered, distributed identifiers optimized for database B-Tree indexes.
Engineering context & problem
Sequential integer primary keys leak business intelligence through enumeration attacks and prevent offline ID generation. Meanwhile, traditional random UUIDv4 severely degrades B-Tree index performance due to random disk fragmentation.
Architectural approach & solution
In PostgreSQL and PHP 8.4, generate UUIDv7 inside the application domain and store in native uuid columns, guaranteeing sequential B-Tree index locality:
Core code snippet & configuration
<?php
declare(strict_types=1);
namespace App\Domain\Shared\Id;
use Symfony\Component\Uid\Uuid;
final readonly class UuidV7
{
private string $value;
public function __construct(?string $id = null)
{
$this->value = $id ?? (string) Uuid::v7();
}
public function toString(): string
{
return $this->value;
}
}Key takeaways & engineering rules
UUIDv7 combines 128-bit collision resistance with millisecond-precision monotonic time ordering.
In PostgreSQL and MySQL, B-Tree indexes on UUIDv7 maintain insertion speeds nearly indistinguishable from BIGINT, eliminating cache misses.
Generating IDs inside the application layer prior to database insertion enables seamless asynchronous job queues and distributed workflows.
Trade-offs & when NOT to use
UUIDv7 takes 16 bytes vs. 8 bytes for BIGINT. For tables containing billions of rows, memory footprint increases, though offset by distributed collision-free scale.
Summary
Practical performance benchmarks comparing INT, UUIDv4, and UUIDv7 with production implementation patterns in Doctrine ORM and PostgreSQL.
Full article with source code
Want to read the full article with deep implementation details?
Click below to access the complete story on Medium with zero paywalls (free access enabled via author friend link).
Looking for backend architecture guidance?
I help teams modernize legacy monoliths, containerize workloads, and build pragmatic AI tooling.