

ULID Identifiers and ULID Tools Website
source link: https://pgrs.net/2023/01/10/ulid-identifiers-and-ulid-tools-website/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Historically, when I wanted to store data in a database, I (or the project/team I was on) used an incrementing integer to uniquely identify each row (e.g. the SERIAL type in PostgreSQL).
String IdsPermalink
Later, many of my teams/projects switched to random or pseudorandom string identifiers. These have many advantages over incrementing integers, especially when used as public identifiers (e.g. in URLs):
- String Ids can contain extra info, such as their type (e.g. whether it’s a User Id, Payment Id, etc). This helps with debugging and support.
- String Ids cannot be used to infer data size or growth from a random Id (e.g. if a newly created user returns a URL like /users/4321, you can infer there are ~4300 users).
- Typos or copy/paste errors don’t result in a valid but incorrect string Id the way they might with a numeric Id (e.g. 1234 -> 123).
- Sharding or splitting the dataset across databases is easier if you don’t have to worry about numeric sequences and collisions (and you can even embed shard info into the Id if desired).
ULIDsPermalink
One easy way to generate unique, random identifiers is by using a UUID. But lately, I’ve been using ULID types instead. ULID stands for Universally Unique Lexicographically Sortable Identifier
, which is like a time sortable UUID.
ULIDs look like 01GPC4NAN03RXV2EXS7308BHJ6
, and we can include extra information by prepending. For example, a Payment Id could be PAY01GPC4NAN03RXV2EXS7308BHJ6
Benefits from the spec:
- 128-bit compatibility with UUID
- 1.21e+24 unique ULIDs per millisecond
- Lexicographically sortable!
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
- Uses Crockford’s base32 for better efficiency and readability (5 bits per character)
- Case insensitive
- No special characters (URL safe)
- Monotonic sort order (correctly detects and handles the same millisecond)
A few more benefits:
- Sortable Ids are handy for things like pagination, especially when you use cursors instead of offsets (e.g. with GraphQL Pagination and Edges).
- Sortable Ids can be more performant and less fragmented in data structures and indexes (e.g. than a random UUIDv4).
- ULID Ids can replace a
created_at
column if desired since the time is embedded.
And there are implementations in many languages.
ULID Tools WebsitePermalink
One downside of ULIDs, however, is their lack of tooling. Periodically, I’d want a quick way to generate new ULIDs. Or I’d want to parse an existing ULID and see when it was generated (since they embed the timestamp).
So I made a simple website which used the javascript ULID library: https://pgr0ss.github.io/ulid-tools/
It currently does 3 things:
- Generates new ULIDs at the current time
- Generates new ULIDs at a user specified time
- Decodes existing ULIDs and displays the time
The code is at https://github.com/pgr0ss/ulid-tools. (Note: my html/javascript skills are pretty rusty.)
DownsidesPermalink
In fairness, everything comes with tradeoffs and ULIDs aren’t without their faults. For example:
- Numeric Ids take up a lot less space in the database.
- ULIDs are a bit long, which makes URLs super long (e.g.
/users/US01GPC6NGM662XD35QWYERHW6B6/payments/PAY01GPC6NSA8P3DWX6ATS29ABV84
). - There may be cases where it’s undesirable to expose when an Id was created.
FuturePermalink
There’s a draft spec for new UUID versions which are time sorted (inspired by ULID and others): https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html
Maybe these will be accepted and gain widespread adoption in the future.
Recommend
-
178
Professionals | Community Groups Programs | Google Developers
-
139
Laravel Hashid Laravel Hashid provides a unified API across various drivers such as Base62, Base64,...
-
35
README.md
-
10
Device Identifiers and Fingerprinting on iOSFor every era, there’s a monster that embodies the anxieties of the age. At the dawn of the Holocene,...
-
4
ULID vs UUID: Sortable Random ID Generators for JavaScriptWhat’s the right tool for generating universal identifiers?UUID
-
6
UUID是软件开发中最常用的通用标识符之一。然而,在过去的几年里,新的替代品挑战了它的存在。其中,ULID 是领先的竞争对手之一,因为它提供可排序的唯一 ID。在本文中,我将通过示例讨论 ULID 的特性,以便您更好地了解何时使用它。了解...
-
13
vredcloud commented
-
13
前提# 最近发现各个频道推荐了很多ULID相关文章,这里对ULID的规范文件进行解读,并且基于Java语...
-
9
ulid/spec: 全局唯一标识符ULID是传统UUID的替代 ULID 是 UUID 的替代品。它是可排序的并且基于时间戳+随机种子。有多种语言...
-
6
In this article, we discuss the syntax and usage details of keywords and identifiers in Python. Python keywords In Python, keywords are reserved words. It means that we cannot create custom variables or functions or any other ident...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK