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

Custom Short URL Support

Allow users to specify custom short URLs (vanity URLs) while validating uniqueness and handling reserved keywords.

Custom Short URL Support

What you'll learn: How to let users choose their own memorable short URLs (like short.ly/acme) while avoiding conflicts and protecting system integrity.

Core Idea

When users create shortened URLs, they sometimes want vanity URLs—custom aliases like bit.ly/apple-event instead of random codes like bit.ly/x7Ks9a. This feature adds user delight but introduces complexity: you must validate uniqueness, prevent abuse, and handle reserved keywords.

Plain English Explanation

Think of vanity URLs like personalized license plates. Just as "LOVE" or "POLICE" might be taken or restricted, custom short codes face similar constraints.

When a user requests short.ly/ceo-blog:

  1. Check availability: Query your database to see if ceo-blog already exists
  2. Validate format: Ensure it's alphanumeric, appropriate length, no special characters (or define allowed patterns)
  3. Block reserved words: Reject codes like api, admin, delete, stats that conflict with your service's routes
  4. Handle conflicts gracefully: If taken, suggest alternatives like ceo-blog-2024

Architecture Considerations

Unlike auto-generated codes (hash-based, auto-increment), custom URLs introduce a race condition: two users might request the same vanity code simultaneously. You need:

  • Unique constraint on the short code column in your database
  • Transactional insert that fails fast if the code exists
  • A reserved keywords list maintained separately (could be cached)

The validation flow becomes:

User requests "mycode" → Check reserved list → Check DB uniqueness → Insert or reject

Tradeoffs

Custom URLs increase collision rates in your namespace—popular words get claimed quickly. You might limit this feature to premium users or implement expiration for inactive custom links.

Key Takeaway: Custom short URL support requires uniqueness validation, reserved keyword filtering, and race-condition-safe inserts to balance user flexibility with system reliability.