Course contentsShow
← System Design
Lesson 1555 of 1,91938. Real-World System: PastebinFree lesson

Generating Unique Paste IDs

Use base62 encoding, UUID, or distributed ID generation to create short, collision-free paste identifiers.

Generating Unique Paste IDs

You'll learn how to create short, collision-free identifiers for paste URLs using proven techniques like base62 encoding, UUIDs, and distributed ID generation.

When someone uploads a paste to Pastebin, you need to generate a unique, short identifier like dQw4w9WgXcQ for the URL pastebin.com/dQw4w9WgXcQ. This ID must be unique across all pastes, short enough to share easily, and generated quickly at scale.

Three Primary Approaches

Base62 Encoding with Auto-Increment

Take an auto-incrementing database ID (1, 2, 3...) and encode it using base62 (0-9, a-z, A-Z). ID 12345 becomes 3D7 in base62. This guarantees uniqueness and predictable length, but sequential IDs can leak business metrics and allow paste enumeration.

UUID-Based Generation

Generate a UUID (like 550e8400-e29b-41d4-a716-446655440000) and take the first N characters or hash it into base62. This provides randomness and works across distributed systems without coordination, but requires collision checking since you're truncating randomness.

Distributed ID Generation

Use systems like Twitter's Snowflake or a dedicated ID service to generate globally unique, roughly time-ordered IDs across multiple servers. These IDs are then base62-encoded. This approach scales horizontally without central coordination and avoids collisions by design.

Collision Prevention

With base62 and 7 characters, you have 62^7 ≈ 3.5 trillion possible IDs. Even with billions of pastes, collision probability remains negligible. For extra safety, check uniqueness before inserting into your database using a unique constraint on the paste ID column.

Key Takeaway:
Use base62-encoded distributed IDs or UUIDs for short, collision-free paste identifiers; auto-increment works for smaller systems, while distributed generation scales globally without coordination bottlenecks.