Katkat

Design a URL Shortening Service

MicrosoftSoftware Engineer · Onsite

Last updated

This question tests the ability to design a distributed, scalable system for mapping long URLs to short aliases, focusing on hash generation, storage strategy, and performance optimization.

Design a URL Shortening Service

Design a service similar to Bitly that allows users to convert long URLs into short, unique aliases. Users should also be able to retrieve the original URL using these short links.

Requirements

  • Functional:
    • Shorten: POST /api/v1/shorten (takes long URL, returns short URL).
    • Redirect: GET /{alias} (redirects to the original URL).
    • Support custom aliases if provided by the user.
  • Non-Functional:
    • High availability and low latency for redirects.
    • Short URLs should be as compact as possible.
    • The system should handle 100 million new URL shortenings per month.
    • Links should persist indefinitely unless marked for deletion.

Scope of Answer

Provide a high-level design that covers:

  1. API Design: Define the endpoints and request/response structures.
  2. Database Schema: Recommend a storage solution (SQL vs. NoSQL) and the necessary table structure.
  3. Collision Handling: Explain how you generate unique aliases and what you do if two users request the same custom alias or if a hash collision occurs.
  4. Scalability: Describe how to handle high read traffic (redirects) and how to distribute data across multiple nodes.
  5. Caching: Explain your caching strategy for frequently accessed URLs.

Hints

Hint 1

Consider whether an algorithmic approach (like Base62 encoding of a counter) or a random hash approach better suits your collision and sequence requirements.

Hint 2

A distributed system likely requires a fast key-value store like Redis to cache the most popular URLs to avoid hitting the primary database for every redirect.

Hint 3

Think about how to use database sharding to handle the scale, and what partition key would be most effective for lookups.

Follow-up questions

  • How would you handle URL expiration or implement a TTL (Time-To-Live) for links that should only work for a limited time?
  • How would you integrate basic analytics, such as counting the number of clicks per URL, without slowing down the redirect response time?
  • How would you protect the system against abuse, such as mass-creation of spam URLs or scraping?