System DesignMedium
System Design Question: Designing a URL Shortening Service like TinyURL Let's design a URL shortening service like TinyURL. Consider the following: Functional Requirements: Given a long URL, the service should generate a shorter and unique alias. When users access the short URL, they should be redirected to the original long URL. The system should be highly available and reliable. Analytics: Track the number of times a short URL has been accessed. Non-Functional Requirements: The system should have low latency for URL redirection. Shortened URLs should be relatively short in length. The system should be able to handle a large number of URL shortening and redirection requests. Scalability: The system should be scalable to handle a growing number of URLs and users. Capacity Estimation & Constraints: Assume 100 million URLs are shortened per day. Assume 1 billion redirections per day. Assume a read/write ratio of 10:1 (redirections vs. shortening). Short URL length should be as short as possible (e.g., 7 characters). High-Level Design: Discuss the components involved (e.g., web servers, application servers, database). Explain the URL shortening and redirection process. Propose a suitable database (e.g., SQL or NoSQL). Detailed Design: How to generate unique short URLs? Discuss approaches like hash functions, base-62 encoding, or UUIDs. What are the tradeoffs of each approach in terms of collision probability, URL length, and performance? Explain the database schema for storing the mappings between short and long URLs. Detail the caching strategy to improve redirection performance. Where should the cache be placed (e.g., client-side, CDN, in-memory cache)? What cache eviction policy should be used? Scaling and Availability: How to scale the system to handle the expected load? Discuss techniques like load balancing, database sharding, and replication. How to ensure high availability? Discuss strategies like redundancy, failover mechanisms, and monitoring. Rate Limiting: Implement rate limiting to prevent abuse (e.g., a user shortening too many URLs in a short period of time). Discuss different rate-limiting algorithms (e.g., token bucket, leaky bucket). Walk through the design process, addressing each of these points. Explain any tradeoffs you make in your design decisions. Show examples of how different technologies or architectural choices impact system performance and scalability.