UUIDv4 and v6* generation in Swift.

[API Reference]

A UUID is an identifier that is unique across both space and time, with respect to the space of all UUIDs. They are used for multiple purposes, from tagging objects with an extremely short lifetime, to reliably identifying very persistent objects across a network.

UniqueID supports any 128-bit UUID, and is fully compatible with Foundation’s UUID. It also includes features to generate 2 kinds of ID:

  • random: As defined in RFC-4122 (UUIDv4). A 128-bit identifier, consisting of 122 random or pseudo-random bits.
    These are the most common form of UUIDs; for example, they are the ones Foundation’s UUID type creates by default.
    The idea is that because this is such a large number, the chance of a system observing a collision is so low that it can be safely ignored.
    That said, they rely heavily on the amount of entropy in the random bits, and when a system is ingesting IDs created by distributed nodes or devices,the chances of collision may be higher.

  • time-ordered: Generated according to a draft update of RFC-4122 (UUIDv6). A 128-bit identifier, consisting of a fixed-precision timestamp, per-process sequencing number, and 47-bit node ID (which may be random or pseudo-random bits).
    Whilst RFC-4122 did include time-based UUIDs (UUIDv1), it ordered the bits such that they had poor locality and couldn’t be sorted easily. UUIDv6 rearranges these bits,which dramatically improves their usability as database keys. The node ID can be configured to provide even better resilience against collisions.

Using UniqueID in your project

To use this package in a SwiftPM project, you need to set it up as a package dependency:

// swift-tools-version:5.5
import PackageDescription

let package = Package(
  name: "MyPackage",
  dependencies: [
      url: "https://github.com/karwa/uniqueid",
      .upToNextMajor(from: "1.0.0")
  targets: [
      name: "MyTarget",
      dependencies: [
        .product(name: "UniqueID", package: "uniqueid")

And with that, you’re ready to start using UniqueID. One way to get easily experiment with time-ordered (v6) UUIDs is to use Foundation compatibility to simply change how you create UUIDs:

import Foundation
import UniqueID

// Change from UUID() to UUID(.timeOrdered()).
struct MyRecord {
  var id: UUID = UUID(.timeOrdered())
  var name: String

// Read the timestamp by converting to UniqueID.
let uniqueID = UniqueID(myRecord.id)

Bear in mind that v6 UUIDs are not yet an official standard, and the layout may change before it becomes an approved internet standard. This implementation aligns with draft 02, from 7 October 2021. Check the latest status here.

Why new UUIDs?

The IETF draft has a really good summary of why using time-ordered UUIDs can be beneficial. You should read it – at least the “Background” section.

A lot of things have changed in the time since UUIDs were originally
created. Modern applications have a need to use (and many have
already implemented) UUIDs as database primary keys.

The motivation for using UUIDs as database keys stems primarily from
the fact that applications are increasingly distributed in nature.
Simplistic “auto increment” schemes with integers in sequence do not
work well in a distributed system since the effort required to
synchronize such numbers across a network can easily become a burden.
The fact that UUIDs can be used to create unique and reasonably short
values in distributed systems without requiring synchronization makes
them a good candidate for use as a database key in such environments.

However some properties of RFC4122 UUIDs are not well suited to
this task. First, most of the existing UUID versions such as UUIDv4
have poor database index locality. Meaning new values created in
succession are not close to each other in the index and thus require
inserts to be performed at random locations. The negative
performance effects of which on common structures used for this
(B-tree and its variants) can be dramatic. As such newly inserted
values SHOULD be time-ordered to address this.

Previous time-ordered UUIDs, such as version 1 UUIDs from RFC-4122, store their timestamps in a convoluted format, so you can’t just sort UUIDs based on their bytes and arrive at a time-sorted list of UUIDs. Version 6 improves on that.

Let’s compare 10 UUIDv4s against 10 UUIDv6s:

for _ in 0..<10 {


for _ in 0..<10 {


Both lists are unique, and unique with respect to each other, but the time-ordered ones, naturally, came out in order of creation time. We can even extract the embedded timestamp – in this case, it says the UUID was created on the 3rd of November, 2021 at 08:42:01 UTC (down to 100ns precision, theoretically).

The combination of temporal and spacial components means these UUIDs are still robust to collisions – a new 60-bit universe exists every 100ns, and the IDs within that universe are still alloted based on random bits with high entropy. It’s tempting to think you might be paying a high cost in collisions for the ease of use, but it’s not as simple as that.


View Github