UUID vs ULID, How ULID improves write speeds

Maingi Samuel
3 min readApr 7, 2023

--

UUID — (Universal Unique Identifiers), ULID — (Universal Unique Lexicographically Sortable Identifier)

UUID’s are a widely-used type of identifier in modern software development. They provide a way to generate unique identifiers that are almost guarateed to be unique, even across multiple machines or systems. However, UUID’s have some limitations that can make them less than ideal for certain use cases. One alternative to UUID is the ULID, which offers some significant advantages over UUID’s in certain situations.

UUID’s are 128-bit values that are typically represented as a hexadecimal string of 32 charaters, such as “550e8400-e29b-41d4-a716–446655440000”. They are generated using a combination of the current time, the machine’s MAC address, and a random number. This combination ensure that the probability of generating a duplicate UUID is extremely low. However, the downside of this approach is that generating UUID’s can be relatively slow, especially in high-volume environments.

ULID’s, on the other hand, are 128-bit identifiers that are based on the same principle as UUIDs, but with some important differences. First of all, ULID’s are designed to be lexicographically sortable, meaning that they can be sorted in aphabetical order. This makes them particulary useful for databases and other systems that need to be able to quickly sort and search large numbers of identifiers

The other key difference between ULIDs and UUIDs is how they are generated. ULIDs are based on a combination of the current time (measured in milliseconds since January 1, 1970) and a random number. However, the random number in ULIDs is generated using a cryptographically secure pseudorandom number generator (CSPRNG), which is much faster than the method used to generate the random component of UUIDs.

The use of a faster CSPRNG for ULID generation means that ULIDs can be generated much more quickly than UUIDs. In fact, benchmarks have shown that ULID generation can be up to 50% faster than UUID generation in some cases. This can be a significant advantage for systems that need to generate large numbers of identifiers quickly, such as high-volume databases or event streams.

In addition to their faster generation speed, ULIDs also offer other advantages over UUIDs. For example, ULIDs are more compact than UUIDs, requiring only 26 characters to represent (in the standard Base32 encoding) compared to 32 characters for UUIDs. This makes them more space-efficient and easier to work with in certain situations.

Another advantage of ULIDs is that they are designed to be URL-safe, meaning that they can be used in URLs without the need for encoding or escaping. This can be particularly useful for web applications and APIs that need to generate and work with identifiers in URL parameters.

In summary, ULIDs offer a number of advantages over UUIDs in certain situations. They are faster to generate, more compact, lexicographically sortable, and URL-safe. While UUIDs will likely remain the most widely-used type of identifier in many systems, ULIDs are a promising alternative that can offer significant benefits in certain situations.

Which is Better between UUID and ULID

Choosing between UUIDs and ULIDs ultimately depends on the specific requirements of your application. If your application requires globally unique identifiers that can be generated quickly and efficiently, and you do not need to sort or search the identifiers, then UUIDs may be the better choice.

On the other hand, if your application requires lexicographically sortable identifiers that can be generated quickly and efficiently, especially in high-volume environments, then ULIDs may be the better choice. ULIDs are also more space-efficient and URL-safe, which can be important considerations for some applications.

It’s worth noting that ULIDs are not a replacement for UUIDs in all situations. For example, if you need to generate identifiers that are truly random and have no predictable pattern, such as for cryptography or security purposes, then UUIDs may be a better choice. ULIDs, by design, are not intended to be cryptographically secure and should not be used for sensitive applications.

In summary, both UUIDs and ULIDs have their strengths and weaknesses, and the choice between them will depend on the specific requirements of your application. It’s important to carefully consider your needs and evaluate the trade-offs between the two options before making a decision.

--

--