pULID

An ID that tells you what it points to.

pULID is a 128-bit identifier that is a valid ULID and a valid UUID at once. It sorts by creation time and reserves two bytes for a scope, such as the entity type. Available for Go and JavaScript.

go get github.com/pixie-sh/ulid-go

JavaScript: npm install github:pixie-sh/ulid-js. MIT licensed. References: ulid-go and ulid-js.

The problem

A UUID tells you nothing. A serial ID tells everyone too much.

Random UUIDs do not sort and say nothing about what they identify. Serial integers sort, but leak volume and collide across tables. Given an ID in a log line, you still have to guess which table to look in.

Timestamp6 bytes, milliseconds since the Unix epoch. IDs sort by creation time.
Scope2 bytes, yours to assign.
Entropy8 bytes from a cryptographically secure source.

Two notations, one value

The same 16 bytes print as a 26-character ULID or as a UUID, so the ID fits a uuid column.

The scope names the entity

65,534 scopes are available. Give one to each entity type and an ID says what it is without a lookup.

Sortable

IDs created later sort later, which keeps indexes compact and lists in order.

ScopeAs ULIDAs UUID
201M7DFAH9J00108T5TB1A2837M01a1daf5-4532-0002-0468-ba5854240cf4
56701JJN1AD5B08VJ5SRBJAWCBWDQ0194AA15-34AB-0237-22E7-0B92B8C5F1B7
65535 (default in Go)01JJN0XQ6YZZZN7WGR4NZP1C1Q0194AA0E-DCDE-FFFF-53F2-18257F60B037

The scope is the third group of the UUID: 0002, 0237, FFFF.

Usage

The same ID in Go, in JavaScript and in the database.

import pulid "github.com/pixie-sh/ulid-go"

const (
    ScopeUser  pulid.Scope = 1
    ScopeOrder pulid.Scope = 2
)

id := pulid.MustNewScoped(ScopeOrder)

fmt.Println(id.String()) // 01M7DFAH9J00108T5TB1A2837M
fmt.Println(id.UUID())   // 01a1daf5-4532-0002-0468-ba5854240cf4

scope, _ := id.Scope()   // 2
created := time.UnixMilli(int64(id.Epoch()))

// parse either notation
id, err := pulid.UnmarshalString("01M7DFAH9J00108T5TB1A2837M")
id, err = pulid.UnmarshalUUID("01a1daf5-4532-0002-0468-ba5854240cf4")
const { pulid, pULID, pULIDGenerator } = require('@pixie-sh/pulid');

const id = pulid({ scope: 2 });      // "01M7DFJ38Z0012BKWF08R3CKGS"

const parsed = pULID.parse(id);
parsed.getScope();                   // 2
parsed.getTime();                    // Date the ID was created
parsed.toUUID();                     // "01a1daf9-0d1f-0002-25cf-8f0230364e19"

// a generator when most IDs share a scope
const orders = new pULIDGenerator({ defaultScope: 2 });
orders.generateString();
// Go service
id := pulid.MustNewScoped(ScopeOrder)
fmt.Println(id) // 01M7DFAH9J00108T5TB1A2837M

// JavaScript client, given that string
const order = pULID.parse('01M7DFAH9J00108T5TB1A2837M');
order.getScope(); // 2
order.toUUID();   // 01a1daf5-4532-0002-0468-ba5854240cf4, the value the Go service stored
// ULID implements TextMarshaler, sql.Scanner and driver.Valuer
type Order struct {
    ID pulid.ULID `json:"id"`
}

b, _ := json.Marshal(Order{ID: id}) // {"id":"01M7DFAH9J00108T5TB1A2837M"}

var stored pulid.ULID
err := db.QueryRowContext(ctx, "SELECT id FROM orders WHERE number = $1", 1001).Scan(&stored)
-- uuid_to_ulid_pg16.sql ships with ulid-go (PostgreSQL 16+)
SELECT uuid_to_ulid(id), *
FROM orders
ORDER BY id DESC   -- newest first, no created_at needed
LIMIT 10;
Benchmark

Generate and parse, next to the alternatives.

From the benchmark in the ulid-go repository, which creates an ID and parses it back, on an Apple M3 Max. Run go test -bench . to measure on your own hardware.

Generatorns/op
oklog/ulid125.9
pULID139.2
google/uuid246.9
go-nanoid288.0
Status

What works today, and what does not yet.

Open source at Pixie

One of a set that works together.

These libraries run in our own services every day. Use one, or all of them.