Skip to content

Monotonicity in UUIDv7 #148

Closed
Closed
@bensie

Description

UUIDv7 was shipped in #139 (and is included in v1.5.0), but appears to have skipped the part of the spec that expects monotonic ordering for batch creation.

Additionally, care MUST be taken to ensure UUIDs generated in batches are also monotonic. That is, if one-thousand UUIDs are generated for the same timestamp; there is sufficient logic for organizing the creation order of those one-thousand UUIDs. For batch UUID creation implementions MAY utilize a monotonic counter which SHOULD increment for each UUID created during a given timestamp.

In the following code sample, the output is always:

➜  go run main.go
uuidv7 false
package main

import (
	"fmt"

	"github.com/google/uuid"
	"golang.org/x/exp/constraints"
)

func main() {
	length := 10000

	uuids := make([]string, length)
	for i := 0; i < length; i++ {
		uuidString, _ := uuid.NewV7()
		uuids[i] = uuidString.String()
	}

	fmt.Println("uuidv7", isSorted(uuids))
}

func isSorted[T constraints.Ordered](collection []T) bool {
	for i := 1; i < len(collection); i++ {
		if collection[i-1] > collection[i] {
			return false
		}
	}

	return true
}

Can this implementation be modified to support monotonically increasing UUIDs?

Thank you!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions