wasAsked.

How would you design a URL shortener like bit.ly?

Frequently Asked (All Companies) · Role not specified · system-design

Published

Year not specified · System Design

Community answers

Posted by wasAsked editorial collection

A URL shortener converts a long URL into a short code (https://sho.rt/aZ3k9Q) that redirects to the original. It is a classic system design question because it covers ID generation, storage, caching and scaling. 1. Requirements Functional - Create a short URL for a long URL (optionally with a custom alias and expiry). - Redirect short URLs to the original URL quickly. - Optional: click analytics, user accounts, deleting links. Non-functional - Very read-heavy (for example 100 reads per write), low redirect latency (under about 50 ms), high availability, unique and non-guessable codes where needed. 2. Capacity estimates (example) - 100 million new URLs per month (about 40 writes per second), 10 billion redirects per month (about 4,000 per second average, higher at peak). - Each record about 500 bytes: roughly 50 GB per month, 3 TB over five years. 3. API design POST /api/urls { "longUrl": "https://...", "customAlias": "sale", "expiresAt": "..." } → 201 { "shortUrl": "https://sho.rt/aZ3k9Q" } GET /{code} → 301 or 302 redirect with Location header - 301 (permanent) lets browsers cache the redirect, reducing load but hiding repeat clicks from analytics; 302 (temporary) sends every click to the server. Choose based on analytics needs. 4. Generating short codes - Base62 encoding (a–z, A–Z, 0–9): 7 characters give 62^7, about 3.5 trillion combinations. - Option A — counter plus Base62: a unique numeric ID (database sequence, or a distributed ID generator like Snowflake) converted to Base62. No collisions; but sequential codes are predictable, so add obfuscation (for example a reversible shuffle) if guessability matters. - Option B — hash: take a hash of the long URL (MD5 or SHA-256) and use the first 7 Base62 characters; handle collisions by retrying with a salt. Same URL can map to the same code. - Option C — pre-generated key service: a service generates unused random codes in advance and hands them out in batches, avoiding collisions and coordination at write time. 5. Storage - A key-value access pattern: code → long URL plus metadata (created date, expiry, owner). - Good fits: DynamoDB, Cassandra, Cosmos DB, or a relational database (PostgreSQL) with an index on the code, partitioned or sharded by code as data grows. - Unique constraint on the code; store custom aliases in the same keyspace. 6. Read path (redirects) - Client → CDN or edge (can cache popular redirects) → load balancer → stateless redirect service → Redis cache (code → URL) → database on cache miss. - Hot links are served almost entirely from cache; use cache-aside with TTL and LRU eviction. 7. Analytics - Do not slow down redirects: publish a click event (code, timestamp, referrer, country, user agent) to Kafka or a queue asynchronously, then aggregate into an analytics store (ClickHouse, BigQuery) for dashboards. 8. Scaling and reliability - Stateless services behind load balancers with autoscaling; database replication across zones; multi-region deployment with geo-DNS for global latency. - Rate limiting on URL creation to prevent abuse; malware and phishing checks on submitted URLs (safe browsing APIs); expiry clean-up jobs. 9. Trade-offs to discuss - 301 vs 302 for analytics. - Predictable sequential IDs vs random codes. - Consistency: a newly created link must be readable immediately (write to the database, then populate the cache). Interview tip: drive the conversation — clarify requirements, estimate scale, propose a simple design, then evolve it for scale and discuss trade-offs.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics