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.
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.
The same 16 bytes print as a 26-character ULID or as a UUID, so the ID fits a uuid column.
65,534 scopes are available. Give one to each entity type and an ID says what it is without a lookup.
IDs created later sort later, which keeps indexes compact and lists in order.
| Scope | As ULID | As UUID |
|---|---|---|
| 2 | 01M7DFAH9J00108T5TB1A2837M | 01a1daf5-4532-0002-0468-ba5854240cf4 |
| 567 | 01JJN1AD5B08VJ5SRBJAWCBWDQ | 0194AA15-34AB-0237-22E7-0B92B8C5F1B7 |
| 65535 (default in Go) | 01JJN0XQ6YZZZN7WGR4NZP1C1Q | 0194AA0E-DCDE-FFFF-53F2-18257F60B037 |
The scope is the third group of the UUID: 0002, 0237, FFFF.
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;
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.
| Generator | ns/op |
|---|---|
| oklog/ulid | 125.9 |
| pULID | 139.2 |
| google/uuid | 246.9 |
| go-nanoid | 288.0 |
@pixie-sh/pulid is not published; install from GitHub for now.1 in JavaScript, 65535 in Go. Pass it explicitly when both sides create IDs.These libraries run in our own services every day. Use one, or all of them.