CUID v1 anatomy and a safe migration path
CUID v1 is deprecated, but existing identifiers still need to be read and preserved. This guide shows what its fields reveal, what parsing cannot establish, and how to introduce a successor without rewriting history.
Review notes,
The sample timestamp decode and format boundaries are covered by repository tests against cuid 3.0.0.
The field map follows the legacy implementation's documented layout. The example is decoded independently from its eight-character base36 timestamp segment, while all claims about origin remain explicitly unverified.
Anatomy of one 25-character value
cjld2cjxh0000qzrmn831i7rn| Field | Characters | Sample | Evidence status | What can be said |
|---|---|---|---|---|
| Prefix | 1 | c | Parsed | Literal marker used by the CUID v1 format. |
| Timestamp | 2–9 | jld2cjxh | Parsed | Eight base36 characters interpreted as Unix-epoch milliseconds. |
| Counter | 10–13 | 0000 | Position inferred | The value can be read, but the generator's counter state cannot be verified later. |
| Fingerprint | 14–17 | qzrm | Position inferred | The original host and process cannot be recovered from this field. |
| Random | 18–25 | n831i7rn | Position inferred | The characters are visible; their source and generation event are not provable. |
The legacy documentation warns that its groupings are not an API contract. Use a parser for diagnostics and migration work, not business logic that depends on undocumented field boundaries.
Worked timestamp decode
- 1. Validate the outer format: the site accepts exactly
cfollowed by 24 lowercase base36 characters. - 2. Select characters 2–9:
jld2cjxh. - 3. Decode base36:
parseInt("jld2cjxh", 36)=1535421552101. - 4. Interpret milliseconds:
2018-08-28T01:59:12.101Z.
cuid created the string at that moment. Anyone can construct text with the same visible shape, and a 25-character CUID2-format value can overlap the broad regular expression.Migration checklist
Inventory contracts
Find database columns, foreign keys, URLs, caches, queues, analytics exports, fixtures, and third-party integrations that assume a 25-character string or a leading c.
Choose the successor
Use the identifier decision guide. Record the generator, exact version, output length, storage type, and whether clients may allocate IDs.
Allow both formats at boundaries
During transition, parse legacy and new IDs as opaque alternatives. Never decide authorization or tenancy from the format.
Keep old primary keys stable
Do not rewrite historical identifiers merely for visual consistency. Add a new public key or mapping column only when a concrete contract requires it.
Switch writes before cleanup
Deploy readers that accept both formats, then move new writes to the successor, then observe logs and integrations before removing the legacy generator.
Enforce and monitor
Use a primary-key or unique constraint for the new value, handle duplicate insert errors, and alert on validation or mapping failures.
A two-format transition shape
ALTER TABLE project
ADD COLUMN public_id varchar(24);
CREATE UNIQUE INDEX project_public_id_unique
ON project (public_id)
WHERE public_id IS NOT NULL;
-- 1. Deploy readers that accept the old primary key or public_id.
-- 2. Generate public_id for new writes.
-- 3. Backfill only if external contracts require every old row to have one.This is a migration pattern, not a drop-in script. Foreign keys, nullability, online index creation, backfill rate, and rollback behavior depend on the database and workload.
What not to do
- Do not generate a “historical CUID” by replacing the timestamp field and treat it as equivalent to a library-generated value.
- Do not sort business events by a parsed CUID timestamp when the authoritative event time is stored separately.
- Do not accept a format match as proof of provenance, uniqueness, authenticity, or access rights.
- Do not replace every legacy key in place unless all references and external contracts can be migrated atomically.
The site's timestamp-field constructor is explicitly diagnostic: it demonstrates the field layout but does not recreate the original generator state or uniqueness properties.