Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ULID vs UUID

ULID vs UUID

ULID, UUID v7 (Time-Based IDs) and UUID v4 (Random Ids) offers completely different characteristics.

Learn about the data layout and when to prefer one or the other based on your type of database and behaviour you want to obtain.

Learn how ULID can cause hotspots in distributed databases, and how UUID v4 can cause fragmentation and increased disk I/O in traditional B+Tree based databases.

https://youtu.be/2MbFDR7qt5U

Matteo Bertozzi

August 13, 2023
Tweet

More Decks by Matteo Bertozzi

Other Decks in Programming

Transcript

  1. UUID ULID (16-bytes) 128-bits (16-bytes) 128-bits 128-bit (16-bytes) block of

    data 128-bit (16-bytes) block of data Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  2. UUID 4bits Version 2bits Variant 128-bit (16-bytes) block of data

    128-bit (16-bytes) block of data ULID 6-bit reserved for the version and variant Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  3. UUID v4 4bits Version 2bits Variant Random 12bits Random 62bits

    Random 48bits 4 128-bit (16-bytes) block of data 128-bit (16-bytes) block of data ULID 6-bit reserved for the version and variant 122-bit Random data, Good distribution Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  4. ULID Unix epoch (Big Endian) 48bits UUID v4 4bits Version

    2bits Variant Random 12bits Random 62bits Random 48bits 4 128-bit (16-bytes) block of data 6-bit reserved for the version and variant 122-bit Random data, Good distribution 48-bit Timestamp, Big-Endian encoded 128-bit (16-bytes) block of data Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  5. ULID Unix epoch (Big Endian) 48bits UUID v4 4bits Version

    2bits Variant Random 12bits Random 62bits Random 48bits 4 128-bit (16-bytes) block of data 6-bit reserved for the version and variant 122-bit Random data, Good distribution Lexicographically sortable, time-based locality 48-bit Timestamp, Big-Endian encoded 128-bit (16-bytes) block of data Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  6. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID v4

    4bits Version 2bits Variant Random 12bits Random 62bits Random 48bits 4 128-bit (16-bytes) block of data 128-bit (16-bytes) block of data 6-bit reserved for the version and variant 122-bit Random data, Good distribution Lexicographically sortable, time-based locality 48-bit Timestamp, Big-Endian encoded Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  7. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID v4

    4bits Version 2bits Variant Random 12bits Random 62bits Random 48bits 4 4bits Version 2bits Variant Random 12bits Random 62bits Unix epoch (Big Endian) 48bits 7 UUID v7 128-bit (16-bytes) block of data Lexicographically sortable, time-based locality 48-bit Timestamp, Big-Endian encoded 128-bit (16-bytes) block of data 6-bit reserved for the version and variant 122-bit Random data, Good distribution Universal Unique Identifier Universally Unique Lexicographically Sortable Identifier
  8. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa

    3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  9. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d

    efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  10. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d

    efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  11. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 01H6YGDAJKAYX4K1XJ2WYVNCVQ (+100ms)

    2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  12. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 01H6YGDAJKAYX4K1XJ2WYVNCVQ (+100ms)

    01H6YGDANQCEWNZC5DMSGRV46J (+200ms) 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  13. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 01H6YGDAJKAYX4K1XJ2WYVNCVQ (+100ms)

    01H6YGDANQCEWNZC5DMSGRV46J (+200ms) 01H6YGZMDFKFXS7M47DW956NWW (+10min) 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  14. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 01H6YGDAJKAYX4K1XJ2WYVNCVQ (+100ms)

    01H6YGDANQCEWNZC5DMSGRV46J (+200ms) 01H6YGZMDFKFXS7M47DW956NWW (+10min) 01H6YGZMGKCY1MG4HM9TWHY2XN (+10min) 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  15. Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7 01H6YGDAFF12Y7KEZBAPAZRF6A 01H6YGDAJKAYX4K1XJ2WYVNCVQ (+100ms)

    01H6YGDANQCEWNZC5DMSGRV46J (+200ms) 01H6YGZMDFKFXS7M47DW956NWW (+10min) 01H6YGZMGKCY1MG4HM9TWHY2XN (+10min) 01H712T1FFXBJ919A0GSQRWMRZ (+24h) 2f52de59-4a7b-45eb-8d71-2df6aa950061 9f108a35-35af-4244-af86-c2222ffb475d efe25407-e91b-4b59-8048-64833b85c7aa 3f56beae-235f-472d-8b40-d80bff249fb9 ace3857f-6b92-483c-a18c-a7c7acd34ea6 e92ee082-4380-4b2e-b599-0bf32d5138ee Random Data Distribution Time-Based Data Locality NanoId Cuid2 Snowflake XID KSUID
  16. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits
  17. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0
  18. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0
  19. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality.
  20. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline.
  21. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline. up-to-date databases have UUID type support
  22. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline. up-to-date databases have UUID type support or BINARY(16) can be used (avoid text format for keys) BINARY(16) can be used (avoid text format for keys)
  23. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline. Opaque non-guessable IDs 
 using the randomness of UUID v4 up-to-date databases have UUID type support or BINARY(16) can be used (avoid text format for keys) BINARY(16) can be used (avoid text format for keys)
  24. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline. Temporary non-secure Tokens: 
 the client can check the timestamp for expiration. Opaque non-guessable IDs 
 using the randomness of UUID v4 up-to-date databases have UUID type support or BINARY(16) can be used (avoid text format for keys) BINARY(16) can be used (avoid text format for keys)
  25. ULID Random 80bits Unix epoch (Big Endian) 48bits UUID 4bits

    Version 2bits Variant 12bits 62bits 48bits Encoded as a 26 character string using Crockford's Base32 (5bit per character) 01H6YGDAFF12Y7KEZBAPAZRF6A Encoded as a 36 character string using Base16 (4bit per character) 71acfe9a-7fca-4d41-b0d7-1ba36d7343c0 Lexicographically sortable, time-based locality. Do not require coordination to be generated, and can be generated offline. Do not require coordination to be generated, and can be generated offline. Temporary non-secure Tokens: 
 the client can check the timestamp for expiration. Event Id chronological order of events Opaque non-guessable IDs 
 using the randomness of UUID v4 up-to-date databases have UUID type support or BINARY(16) can be used (avoid text format for keys) BINARY(16) can be used (avoid text format for keys)
  26. 4bits Version 2bits Variant Random 12bits Random 62bits Random 48bits

    4 Random 80bits Unix epoch (Big Endian) 48bits Time-Based Ids Random-Based Ids UUIDv4 ULID UUIDv7