Course contentsShow
← System Design
Lesson 1510 of 1,91937. Real-World System: URL ShortenerFree lesson

Auto-Incrementing ID Approach

Generate short URLs by encoding sequential database IDs in Base62 for guaranteed uniqueness without collision checks.

Auto-Incrementing ID Approach

What you'll learn: How to generate collision-free short URLs by encoding sequential database IDs in Base62.


The Core Idea

Instead of hashing long URLs and checking for collisions, use your database's auto-incrementing primary key as the source of uniqueness. Each new URL insertion gets the next sequential ID (1, 2, 3...), which you then encode in Base62 to produce the short URL. Since database IDs are guaranteed unique, collisions become impossible by design.

How It Works

  1. Insert the long URL into your database
  2. Retrieve the auto-generated ID (e.g., 12345)
  3. Encode that ID in Base62 → dnh
  4. Return yourdomain.com/dnh as the short URL

When a user visits the short URL, decode dnh back to ID 12345, look it up in your database, and redirect.

Why This Eliminates Complexity

With the hash-based approach, you must check if each generated short URL already exists—adding a database query on every write. The auto-incrementing approach removes this entirely: every ID is new, so every encoded string is new. No collision checks, no retry loops, no wasted cycles.

The Tradeoff

Sequential IDs are predictable. Users can guess dnh comes after dnf. If your system requires unpredictable URLs for privacy (unlisted links), you'll need to add randomness elsewhere—perhaps by starting your counter at a large random offset or using a different generation strategy.

Analogy

Think of sequential IDs like ticket numbers at a deli counter. Each customer gets the next number (34, 35, 36...). No two customers ever get the same number, and you never need to verify uniqueness—the counter guarantees it.

Key Takeaway:
Auto-incrementing IDs with Base62 encoding guarantee collision-free short URLs with zero lookup overhead, trading simplicity for predictability.