KatInterview

Build a link shortener with unique codes and fast redirect

System DesignModel answer
MediumModel answerSystem Designurl-shortenerdistributed-systemscaching

Last updated

Design a service that converts long URLs into compact codes and serves redirects at high throughput with low latency.

You are tasked with building a service that accepts full-length URLs and returns compact, shortened versions. When someone visits a shortened link, they should be redirected to the original destination instantly.

The traffic profile, feature requirements, and data retention rules are unspecified. Begin by asking for these details or clearly state your assumptions with concrete figures that inform your design choices.

Questions to Resolve

  • What is the daily volume of new links created, and what is the peak redirect rate per second?
  • Should users be able to specify their own custom short codes? Do links have an expiration policy, and can owners modify or remove them after creation?
  • If a user submits the same URL multiple times, should they always get back the same short code?
  • Are there constraints on code length, character sets, or unpredictability requirements?
  • Is there a need for tracking visitor metrics, and if so, what is the acceptable delay for this data?
  • Should redirect functionality remain operational even when link creation services are unavailable?

Part 1 — Interface Design and Code Assignment

Describe the API surface and the mechanism for assigning short codes such that no two distinct URLs ever receive the same code.

Expected Coverage

  • Endpoints for creation and redirection, including edge cases and support for user-defined codes
  • Code length calculation based on anticipated link volume and the character alphabet
  • A strategy that prevents collisions in a distributed environment, and fallback behavior if conflicts arise
  • Whether codes can be predicted from their sequence, and the security implications

Part 2 — Optimizing the Redirect Flow

Ensure redirects are served quickly and reliably at the expected query volume.

Expected Coverage

  • The complete request flow from incoming request to final redirect, including all caching layers
  • Selection of HTTP redirect status code and its impact on browser caching and analytics accuracy
  • Handling of nonexistent, expired, or removed codes, including negative result caching
  • Performance and uptime objectives

Part 3 — Data Persistence and Growth Management

Select the storage technology and schema, and describe how the system handles growth and maintains reliability as the link count increases.

Expected Coverage

  • Schema design and database selection rationale
  • Sharding strategy, redundancy, and multi-region deployment
  • Management of expired links, deletions, and the pipeline for click event processing
  • Countermeasures for abuse: detecting harmful destinations and rate-limiting creation requests

Hints

Hint 1

Start with capacity planning—how many links will you store, and what throughput must you support?

Hint 2

Think about whether codes are generated in advance or at request time, and how distributed servers avoid conflicts.

Hint 3

Consider where caching happens and what redirect status code best balances performance with analytics needs.