Choose how many KSUIDs to generate (1-1000)
Click "Generate KSUIDs" to create sortable identifiers
See decoded timestamps and download all KSUIDs
KSUID (K-Sortable Unique Identifier) is a globally unique identifier that combines a timestamp with random data to create identifiers that are naturally sortable by generation time. Developed by Segment, KSUIDs are used in production by companies like Segment and Stripe for tracking events, orders, and distributed system coordination.
A KSUID is a 20-byte identifier encoded as a 27-character Base62 string. The first 4 bytes represent a Unix timestamp in seconds, and the remaining 16 bytes are cryptographically random. This structure ensures KSUIDs are globally unique, naturally ordered by creation time, and human-readable enough to be used in logs and debugging.
A KSUID consists of two parts:
KSUIDs offer several advantages for distributed systems:
| Feature | KSUID | ULID | UUID v4 |
|---|---|---|---|
| Length | 27 characters | 26 characters | 36 characters |
| Sortable | Yes (by second) | Yes (by millisecond) | No |
| Encoding | Base62 | Crockford Base32 | Hexadecimal |
| Timestamp Precision | Seconds | Milliseconds | None |
| Random Bits | 128 bits | 80 bits | 122 bits |
Enter the number of KSUIDs you need in the quantity field (1-1000). For most applications, generating 5-10 KSUIDs at a time is sufficient for testing and development. Each KSUID is guaranteed to be globally unique and sortable by creation time.
Click the "Generate KSUIDs" button to create your identifiers. Generation happens instantly in your browser using cryptographically secure random number generation. Each KSUID includes a timestamp that shows exactly when it was created, down to the second.
Each generated KSUID is displayed with its decoded information including the timestamp, date/time in ISO format, and the raw payload. This makes it easy to understand when each ID was created and verify the chronological ordering.
Click any KSUID to copy it to your clipboard, or use the "Download" button to save all generated KSUIDs as a text file. The downloaded file contains one KSUID per line, making it easy to import into your application.
Segment uses KSUIDs for tracking events in their analytics platform. The sortable nature of KSUIDs makes it easy to query events in chronological order without needing a separate timestamp index. This improves query performance and simplifies data modeling for time-series event data.
Stripe and other payment processors use KSUIDs for order and transaction identifiers. The embedded timestamp makes it easy to find recent transactions, and the global uniqueness ensures no ID collisions across distributed payment processing systems. The human-readable format also helps with customer support and debugging.
KSUIDs make excellent primary keys for distributed databases because they're naturally ordered by creation time, which improves database index performance. Unlike random UUIDs that cause index fragmentation, KSUIDs are always inserted at the end of the index, reducing write amplification and improving performance.
In distributed systems with multiple services generating logs, KSUIDs provide a way to uniquely identify log entries while maintaining chronological order. The embedded timestamp makes it easy to correlate logs across services and trace request flows through the system.
Message queues and event streaming systems benefit from KSUIDs because messages are naturally ordered by creation time. This makes it easy to process messages in order, implement time-based retention policies, and debug message flow issues by examining the embedded timestamps.
package main
import (
"fmt"
"github.com/segmentio/ksuid"
)
func main() {
// Generate a new KSUID
id := ksuid.New()
fmt.Println(id.String()) // 0ujtsYcgvSTl8PAuAdqWYSMnLOv
// Get timestamp
fmt.Println(id.Time()) // 2017-10-09 21:00:47 +0000 UTC
}
const KSUID = require('ksuid');
// Generate a new KSUID
const id = KSUID.randomSync();
console.log(id.string); // 0ujtsYcgvSTl8PAuAdqWYSMnLOv
// Parse existing KSUID
const parsed = KSUID.parse('0ujtsYcgvSTl8PAuAdqWYSMnLOv');
console.log(parsed.date); // 2017-10-09T21:00:47.000Z
CREATE TABLE events (
id CHAR(27) PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
event_type VARCHAR(50) NOT NULL,
data JSONB,
created_at TIMESTAMP DEFAULT NOW()
);
-- Insert with KSUID
INSERT INTO events (id, user_id, event_type, data)
VALUES ('0ujtsYcgvSTl8PAuAdqWYSMnLOv', 'user123', 'page_view', '{}');
const ksuids = [
'0ujtsYcgvSTl8PAuAdqWYSMnLOv',
'0ujsszwN8NRY24YaXiTIE2VWDTS',
'0ujssxh0cECutqzMgbtXSGnjorm'
];
// KSUIDs are naturally sortable
ksuids.sort(); // Already in chronological order!
// Extract timestamps
ksuids.forEach(id => {
const ksuid = KSUID.parse(id);
console.log(`${id}: ${ksuid.date}`);
});
{
"order_id": "0ujtsYcgvSTl8PAuAdqWYSMnLOv",
"customer_id": "cus_abc123",
"amount": 4999,
"currency": "usd",
"status": "pending",
"created": 1507584047
}
Explore our other unique identifier generators:
Get $200 free DigitalOcean credit or sponsor us on GitHub!