Last updated
What is a ULID?
ULID (Universally Unique Lexicographically Sortable Identifier) is a modern alternative to UUID that combines the benefits of unique identification with natural time-based sorting. A ULID is a 26-character string that encodes both a timestamp and random data, making it perfect for distributed systems and databases.
Unlike traditional UUIDs which are 36 characters long and randomly ordered, ULIDs are shorter (26 characters), naturally sortable by creation time, and use a Crockford Base32 encoding that's case-insensitive and URL-safe. This makes ULIDs ideal for use as database primary keys, API identifiers, and distributed system coordination.
ULID Structure
A ULID consists of two parts:
- Timestamp (48 bits): The first 10 characters represent a Unix timestamp in milliseconds, providing natural chronological sorting
- Randomness (80 bits): The remaining 16 characters are cryptographically random, ensuring uniqueness even when generated at the same millisecond
Timestamp part: 01ARZ3NDEK (represents 2016-07-01 00:00:00.000 UTC)
Random part: TSV4RRFFQ69G5FAV (ensures uniqueness)
Why Use ULID Instead of UUID?
ULIDs offer several advantages over traditional UUIDs:
- Shorter: 26 characters vs 36 characters (28% smaller)
- Sortable: Natural chronological ordering by creation time
- Case-insensitive: Uses Crockford Base32 encoding
- URL-safe: No special characters that need encoding
- Database-friendly: Better index performance due to sequential nature
- Human-readable: Easier to read and communicate than UUIDs
ULID vs UUID Comparison
| Feature | ULID | UUID v4 |
|---|---|---|
| Length | 26 characters | 36 characters |
| Sortable | Yes (by timestamp) | No (random) |
| Encoding | Crockford Base32 | Hexadecimal |
| Case-sensitive | No | No |
| URL-safe | Yes | Yes |
| Timestamp | Embedded | Not included |
How to Use ULID Generator
Step 1: Choose Quantity
Enter the number of ULIDs you need to generate in the quantity field. You can generate anywhere from 1 to 1000 ULIDs at once. For most applications, generating 5-10 ULIDs at a time is sufficient for testing and development purposes.
Step 2: Generate ULIDs
Click the "Generate ULIDs" button to create your unique identifiers. The generation happens instantly in your browser using cryptographically secure random number generation. Each ULID includes a timestamp component that shows exactly when it was created.
Step 3: Copy or Download
You can copy individual ULIDs by clicking on them, or download all generated ULIDs as a text file using the "Download" button. The downloaded file contains one ULID per line, making it easy to import into your application or database.
Understanding the Output
Each generated ULID is displayed with its decoded timestamp, showing you the exact millisecond when it was created. This timestamp information is useful for debugging and understanding the chronological order of your identifiers.
Common Use Cases for ULIDs
1. Database Primary Keys
ULIDs make excellent primary keys for databases because they're naturally sortable by creation time. This improves database index performance compared to random UUIDs, as new records are always inserted at the end of the index rather than in random positions. The sortability also makes it easier to query records by creation order without needing a separate timestamp column.
2. Distributed Systems
In distributed systems where multiple servers need to generate unique identifiers independently, ULIDs provide a perfect solution. Each server can generate ULIDs without coordination, and the identifiers will still be globally unique and naturally ordered by creation time across all servers.
3. API Request IDs
ULIDs are ideal for tracking API requests and responses. Their sortable nature makes it easy to trace request flows chronologically, and the embedded timestamp helps with debugging and log analysis. The shorter length compared to UUIDs also reduces payload sizes in API responses.
4. Event Sourcing
In event-sourced systems, ULIDs provide natural ordering of events without requiring additional timestamp fields. The embedded timestamp ensures events are always processed in the correct chronological order, even when generated across multiple nodes.
5. File and Document Naming
ULIDs work great for naming files and documents because they're URL-safe and sortable. Files named with ULIDs will automatically sort chronologically in file systems and cloud storage, making it easy to find recent files without checking metadata.
ULID Examples
Example 1: Node.js Implementation
const { ulid } = require('ulid');
// Generate a single ULID
const id = ulid();
console.log(id); // 01ARZ3NDEKTSV4RRFFQ69G5FAV
// Generate with specific timestamp
const customId = ulid(1469918176385);
console.log(customId); // 01ARYZ6S41TSV4RRFFQ69G5FAV
Example 2: Python Implementation
from ulid import ULID
# Generate a new ULID
new_ulid = ULID()
print(str(new_ulid)) # 01ARZ3NDEKTSV4RRFFQ69G5FAV
# Parse existing ULID
parsed = ULID.from_str('01ARZ3NDEKTSV4RRFFQ69G5FAV')
print(parsed.timestamp()) # Get timestamp
Example 3: Database Schema (PostgreSQL)
CREATE TABLE users (
id CHAR(26) PRIMARY KEY,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
-- Insert with ULID
INSERT INTO users (id, email)
VALUES ('01ARZ3NDEKTSV4RRFFQ69G5FAV', 'user@example.com');
Example 4: REST API Response
{
"id": "01ARZ3NDEKTSV4RRFFQ69G5FAV",
"type": "order",
"status": "pending",
"created": "2024-01-15T10:30:00Z"
}
Example 5: Sorting ULIDs
const ulids = [ '01ARZ3NDEKTSV4RRFFQ69G5FAV', '01ARZ3NDEKTSV4RRFFQ69G5FAW', '01ARZ3NDEKTSV4RRFFQ69G5FAX' ]; // ULIDs are naturally sortable ulids.sort(); // Already in chronological order!