Skip to content
Search

Globally unique identifier

lastmod 2022-02-24created 2015-02-274 edits

A globally unique identifier (GUID) is a pseudo-random number used in application software. There is no guarantee that a GUID is always unique when generated, but because the total number of possible values is very large at 2^128 = 3.4028×10^38, the chance of generating the same number twice is very small given a proper algorithm.

GUIDs are used in many places such as Oracle Database, but the most prominent implementation is probably Microsoft's. The standard is the Universally Unique Identifier (UUID) specified by the Open Software Foundation (OSF).

Resource IDs: GUID or sequential?

As far as I know, there are usually two main ways to create an ID that uniquely identifies a resource. One is to use the sequential integer values produced by setting Auto Increment on a DB table's Primary Key as-is, and the other is to generate and use a GUID (also called a UUID), a random 128-bit value, on the fly.

RDBMSs are responsible for much of the data behind the countless services on the web, and the Auto Increment of such DBMSs is not only optimized internally but also easy for developers to understand and predict, and sorting data in insertion order is simple. It just adds 1 to the number each time. However, this approach has problems: in certain cases it can expose information you would rather keep hidden for security reasons (for example, a competitor can easily guess key metrics such as the number of users of your service), or it can cause trouble in distributed architectures.

Using GUIDs has the exact opposite characteristics. Because a GUID is a practically unique 128-bit value with a near-zero chance of collision, created on the fly without any other dependency, it causes no problems in distributed architectures and there is no risk of unintentionally leaking other meaningful information to the outside. However, writing randomly generated values to an RDBMS can degrade performance, and by itself it cannot be sorted in insertion order. To make up for these weaknesses, some use things like Timeflake, which is not fully random but mixes in time information to give partial sequentiality. I haven't used it myself, but I hear frameworks like Laravel use this approach too.

You can also use ULID as an alternative.

See also

Favorite site

author Zer0 sitemap