SXStudio

System Design Reading Notes 7: Design a URL Shortener

This is my reading notes for Chapter 8 in book “System Design Interview – An insider’s guide (Vol. 1)”.

Overview
Chapter 8 from System Design Interview: An Insider’s Guide explains how to design a URL shortening service like TinyURL or Bitly. The goal is to build a scalable, reliable, and low-latency system capable of handling massive traffic. The design involves solving distributed system challenges such as database scaling, hashing, and caching.

DESIGN A URL SHORTENER

Key Concepts & Design Steps

1. Understanding Requirements

The primary functions of a URL shortener are:

Key requirements include:


2. Back-of-the-Envelope Calculations

For 100 million requests per day, the system needs to handle approximately:

Storage estimation: About 36.5 TB would be needed over 10 years to store URL mappings.


System Components

3. API Design

POST API (Shorten URL)
This API accepts a long URL and generates a short one.

POST /shorten
Request: { "longURL": "https://example.com/some-long-url" }
Response: { "shortURL": "https://tinyurl.com/abc123" }

GET API (Redirect)
The GET API maps the short URL to the long URL and redirects the user.

GET /abc123
Response: Redirects to "https://example.com/some-long-url"

4. High-Level Architecture


5. Unique ID Generation


6. Collision Handling


7. Caching


8. Data Model

Table: URL_Mappings 
--------------------------------- 
ID | ShortURL | LongURL | CreatedAt
 ---------------------------------
 1 | abc123 | https://example.com/long-url | 2024-08-18

9. Rate Limiting

To prevent abuse, limit the number of URL shortening requests per user/IP.


System Scalability and Optimization

10. Horizontal Scaling

Scale horizontally by adding more servers to handle increased traffic.


11. Database Replication

Use master-slave replication to distribute read operations and ensure high availability.


12. Fault Tolerance

Ensure resilience by replicating data across multiple data centers to protect against outages.


13. Analytics and Tracking

Track URL usage with analytics.


14. Content Delivery Network (CDN)

Use a CDN to serve static assets quickly to end users globally.


Conclusion

Chapter 8 of System Design Interview: An Insider’s Guide outlines how to design a scalable URL shortening service, a common system design question. The design process covers everything from API structure and ID generation to scaling, caching, and fault tolerance, making it a great practice problem for system design interviews.

Exit mobile version