Universally unique identifiers: UUID vs ULID

  • avatar
  • 4.6K Views
  • 2 Likes
  • 7 mins read

Historically a lot of software has used incrementing numbers to represent the identifier of a particular piece of data. They can be auto-generated by many data stores, they are easy to read, efficient to store and naturally time ordered.

As time passes, applications become larger. They need more resources and usually distributed in different places. That may cause some errors generating concurrent numbers and coordination issues. Incremental numbers can also be the source of security problems and they may include implicit information about datasets volume. Modern software architectures also trend to decouple infrastructure layer and move ID generation logic to the domain core.

Installation

To get started, use Composer package manager to add the package to your project's dependencies.

composer require symfony/uid

The Symfony UID component provides utilities to work with unique identifiers (UIDs) such as UUIDs and ULIDs.

Universally unique identifiers

For many reasons different applications needed to find another way to generate unique identifiers while allowing data hiding and scalability. UUIDs (universally unique identifiers) are one of the most popular UIDs in the software industry. UUIDs are 128-bit numbers usually represented as five groups of hexadecimal characters: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx (the M digit is the UUID version and the N digit is the UUID variant).

There are downsides to UUIDs, although they are reasonably small they still take up much more space than many other identifiers. In general, the lack of built-in sortability based on time becomes the root of a lot of issues.

Usage in code

Use the named constructors of the Uuid class or any of the specific classes to create each type of UUID:

use Symfony\\\\Component\\\\Uid\\\\Uuid;

// UUID type 1 generates the UUID using the MAC address of your device and a timestamp.
// Both are obtained automatically, so you don't have to pass any constructor argument.
$uuid = Uuid::v1(); // $uuid is an instance of Symfony\\\\Component\\\\Uid\\\\UuidV1

// UUID type 4 generates a random UUID, so you don't have to pass any constructor argument.
$uuid = Uuid::v4(); // $uuid is an instance of Symfony\\\\Component\\\\Uid\\\\UuidV4

// UUID type 3 and 5 generate a UUID hashing the given namespace and name. Type 3 uses
// MD5 hashes and Type 5 uses SHA-1. The namespace is another UUID (e.g. a Type 4 UUID)
// and the name is an arbitrary string (e.g. a product name; if it's unique).
$namespace = Uuid::v4();
$name = $product->getUniqueName();

$uuid = Uuid::v3($namespace, $name); // $uuid is an instance of Symfony\\\\Component\\\\Uid\\\\UuidV3
$uuid = Uuid::v5($namespace, $name); // $uuid is an instance of Symfony\\\\Component\\\\Uid\\\\UuidV5

// the namespaces defined by RFC 4122 (see https://tools.ietf.org/html/rfc4122#appendix-C)
// are available as PHP constants and as string values
$uuid = Uuid::v3(Uuid::NAMESPACE_DNS, $name); // same as: Uuid::v3('dns', $name);
$uuid = Uuid::v3(Uuid::NAMESPACE_URL, $name); // same as: Uuid::v3('url', $name);
$uuid = Uuid::v3(Uuid::NAMESPACE_OID, $name); // same as: Uuid::v3('oid', $name);
$uuid = Uuid::v3(Uuid::NAMESPACE_X500, $name); // same as: Uuid::v3('x500', $name);

// UUID type 6 is not part of the UUID standard. It's lexicographically sortable
// (like ULIDs) and contains a 60-bit timestamp and 63 extra unique bits.
// It's defined in http://gh.peabody.io/uuidv6/
$uuid = Uuid::v6(); // $uuid is an instance of Symfony\\\\Component\\\\Uid\\\\UuidV6

If your UUID value is already generated in another format, use any of the following methods to create a Uuid object from it:

// all the following examples would generate the same Uuid object
$uuid = Uuid::fromString('d9e7a184-5d5b-11ea-a62a-3499710062d0');
$uuid = Uuid::fromBinary("\\\\xd9\\\\xe7\\\\xa1\\\\x84\\\\x5d\\\\x5b\\\\x11\\\\xea\\\\xa6\\\\x2a\\\\x34\\\\x99\\\\x71\\\\x00\\\\x62\\\\xd0");
$uuid = Uuid::fromBase32('6SWYGR8QAV27NACAHMK5RG0RPG');
$uuid = Uuid::fromBase58('TuetYWNHhmuSQ3xPoVLv9M');
$uuid = Uuid::fromRfc4122('d9e7a184-5d5b-11ea-a62a-3499710062d0');

And use these methods to transform the UUID object into different bases:

$uuid = Uuid::fromString('d9e7a184-5d5b-11ea-a62a-3499710062d0');

$uuid->toBinary(); // string(16) "\\\\xd9\\\\xe7\\\\xa1\\\\x84\\\\x5d\\\\x5b\\\\x11\\\\xea\\\\xa6\\\\x2a\\\\x34\\\\x99\\\\x71\\\\x00\\\\x62\\\\xd0"
$uuid->toBase32(); // string(26) "6SWYGR8QAV27NACAHMK5RG0RPG"
$uuid->toBase58(); // string(22) "TuetYWNHhmuSQ3xPoVLv9M"
$uuid->toRfc4122(); // string(36) "d9e7a184-5d5b-11ea-a62a-3499710062d0"

Universally Unique Lexicographically Sortable Identifier

ULIDs (Universally Unique Lexicographically Sortable Identifier) are 128-bit numbers usually represented as a 26-character string: TTTTTTTTTTRRRRRRRRRRRRRRRR (where T represents a timestamp and R represents the random bits). ULIDs are a great alternative to UUIDs when using those is impractical. They provide 128-bit compatibility with UUID, they are lexicographically sortable and they are encoded as 26-character strings (vs 36-character UUIDs).

Usage in code

Instantiate the Ulid class to generate a random ULID value:

use Symfony\\\\Component\\\\Uid\\\\Ulid;

$ulid = new Ulid(); // e.g. 01AN4Z07BY79KA1307SR9X4MV3

If your ULID value is already generated in another format, use any of the following methods to create a Ulid object from it:

// all the following examples would generate the same Ulid object
$ulid = Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8');
$ulid = Ulid::fromBinary("\\\\x01\\\\x71\\\\x06\\\\x9d\\\\x59\\\\x3d\\\\x97\\\\xd3\\\\x8b\\\\x3e\\\\x23\\\\xd0\\\\x6d\\\\xe5\\\\xb3\\\\x08");
$ulid = Ulid::fromBase32('01E439TP9XJZ9RPFH3T1PYBCR8');
$ulid = Ulid::fromBase58('1BKocMc5BnrVcuq2ti4Eqm');
$ulid = Ulid::fromRfc4122('0171069d-593d-97d3-8b3e-23d06de5b308');

And use these methods to transform the ULID object into different bases:

$ulid = Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8');

$ulid->toBinary(); // string(16) "\\\\x01\\\\x71\\\\x06\\\\x9d\\\\x59\\\\x3d\\\\x97\\\\xd3\\\\x8b\\\\x3e\\\\x23\\\\xd0\\\\x6d\\\\xe5\\\\xb3\\\\x08"
$ulid->toBase32(); // string(26) "01E439TP9XJZ9RPFH3T1PYBCR8"
$ulid->toBase58(); // string(22) "1BKocMc5BnrVcuq2ti4Eqm"
$ulid->toRfc4122(); // string(36) "0171069d-593d-97d3-8b3e-23d06de5b308"

Conclusion

UUIDs are a settled standard for identifiers generation. Probably, libraries and packages are available in every programming language. However, new approaches are worth considering in a world that's increasingly run by distributed systems.

Credits

Symfony documentation: https://symfony.com/doc/current/components/uid.html

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.