System Design Interview Questions

Design a URL Shortener Service (Like TinyURL)

"How would you design a URL shortening service like TinyURL or bit.ly?"

Why Interviewers Ask This

URL shortener is one of the most classic system design interview questions. It appears frequently at FAANG, ByteDance, and other major tech companies because it covers key concepts: API design, database selection, hashing/encoding, caching, and horizontal scaling — all in a relatively simple domain that allows deep exploration.

How to Answer

  1. 1

    Clarify requirements: functional (shorten URL, redirect, custom aliases, analytics) and non-functional (high availability, low latency <100ms for redirects, read-heavy ~100:1 read/write ratio).

  2. 2

    Estimate scale: 100M new URLs/month = ~40 URLs/sec write, 4000 URLs/sec read. Storage: 500 bytes × 100M × 12 months × 5 years ≈ 300GB.

  3. 3

    Design the short URL encoding: Base62 encoding (a-z, A-Z, 0-9) with 7 characters gives 62^7 ≈ 3.5 trillion unique URLs. Use a counter/snowflake ID or hash (MD5/SHA256 + take first 7 chars of Base62).

  4. 4

    Choose database and schema: NoSQL (DynamoDB/Cassandra) for high write throughput, or SQL with sharding. Table: short_url (PK), original_url, created_at, user_id, expiry.

  5. 5

    Add caching and scaling: Redis/Memcached for hot URLs (80/20 rule), CDN for global distribution, load balancer, horizontal scaling of stateless app servers.

Sample Answer

I would design a URL shortener as follows. First, requirements: create short URLs from long ones, redirect short URLs to originals with <100ms latency, support optional custom aliases, handle 100M new URLs/month with a 100:1 read/write ratio. The API has two main endpoints: POST /api/shorten {url, custom_alias?} returns the short URL, and GET /{shortCode} returns 301/302 redirect. For generating short codes, I would use a distributed ID generator (Twitter Snowflake or a counter service) and convert the numeric ID to Base62. Seven characters of Base62 give 3.5 trillion unique codes, sufficient for decades. This avoids hash collisions entirely, unlike MD5-based approaches. The database stores mappings: short_code (primary key) → original_url, created_at, user_id, expiry_date. For this scale, I would use a NoSQL database like DynamoDB for its predictable performance and easy horizontal scaling, partitioned by short_code for even distribution. Since reads dominate, I would add a Redis cache layer in front of the database. With the 80/20 rule (20% of URLs get 80% of traffic), caching hot URLs dramatically reduces database load. Cache strategy: check Redis first, on miss read from DB and populate cache with a TTL. For the redirect endpoint, I would use 301 (permanent) for SEO-friendly links and 302 (temporary) when analytics tracking is needed (301s get cached by browsers, reducing analytics accuracy). Architecture: clients → CDN/load balancer → stateless application servers → Redis cache → DynamoDB. Application servers are horizontally scalable. The ID generation service can use a Zookeeper-coordinated range allocator (each server gets a block of IDs to avoid coordination overhead). For analytics, I would asynchronously log click events to Kafka, then process with Spark for real-time and batch analytics dashboards.

Pro Tips

  • Always start with requirement clarification and back-of-envelope estimation — it shows structured thinking.

  • Discuss the 301 vs 302 redirect tradeoff — it is a common follow-up that shows depth.

  • Mention the collision handling strategy for hash-based approaches (rehash or append counter).

  • Use HireMe AI during system design prep to get real-time feedback on your design choices.

Frequently Asked Questions

Why Base62 instead of Base64?

Base62 uses only alphanumeric characters (a-z, A-Z, 0-9), making URLs safe without encoding. Base64 includes + and / which need URL encoding, making them less clean.

How do you handle expired URLs?

Lazy deletion: check expiry on read and return 404 if expired. Background job periodically cleans up expired entries. This avoids the overhead of real-time deletion.

SQL or NoSQL for this system?

Either works. NoSQL (DynamoDB/Cassandra) is preferred for high write throughput and easy horizontal scaling. SQL works too with proper sharding, and offers stronger consistency if needed.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free