Katkat

Design a URL Shortener

OpenAISoftware Engineer · Onsite

Last updated

This question tests your ability to design a highly scalable, distributed URL shortening service, focusing on database schema design, request routing, and efficient short-code generation.

Problem: URL Shortener Design

Design a system similar to Bitly that converts a long URL into a shorter, alias URL. Users should be able to navigate to the shortened URL and be redirected to the original long URL.

Requirements

Functional Requirements:

  1. URL Shortening: Accept a long URL and return a unique short URL (e.g., bit.ly/xyz123).
  2. Redirection: When a user visits the short URL, redirect them to the original long URL with a 301 (permanent) or 302 (temporary) status.
  3. Custom Aliases: Users should optionally be able to provide a custom string for their short URL.

Non-Functional Requirements:

  1. High Availability: Redirection must be highly available and low latency.
  2. Scalability: The system should handle high read traffic (100:1 read-to-write ratio).
  3. Persistence: Shortened URLs should persist indefinitely unless the user specifies an expiration date.

Expectations

Provide a high-level architecture diagram and describe the following:

  • API Interface: Define the request/response structure for shortening and redirection.
  • Data Model: Choose between SQL and NoSQL. Provide a schema or document structure and explain why it fits.
  • Short-Code Generation: Explain how to generate unique keys (e.g., Base62 encoding, hashing, or a centralized service).
  • Scalability Strategies: Discuss caching, load balancing, and partitioning (sharding) of your database.

Hints

Hint 1

Consider using Base62 encoding (a-z, A-Z, 0-9) to represent a unique numeric ID as a short string.

Hint 2

Think about how caching at the edge (CDN) or a centralized cache (Redis) could reduce latency for popular URLs.

Hint 3

Discuss the trade-offs between using a global counter versus a distributed UUID/hash approach for generating unique keys.

Follow-up questions

  • How would you handle analytics for the service, such as tracking the number of clicks per short URL?
  • How would you prevent malicious users from spamming your system with millions of short URLs?
  • If the database becomes a bottleneck, what sharding strategy would you use, and how would you handle cross-shard queries?