System Design: TinyURL
Learn to design a scalable URL shortening service like TinyURL by defining functional and non-functional requirements, estimating capacity for high traffic, architecting a resilient, efficient system, and evaluating the design against performance, reliability, and scalability goals.
Introduction
Advantages
The key advantages of a URL shortening service are:
Shortened URLs are convenient to use: they optimize link usage across different forms of devices due to their enhanced accessibility and non-breakability.
They are visually professional, engaging and facilitate a higher degree of sharing possibilities.
They are less error-prone while typing.
They require smaller storage space at the user’s end.
Disadvantages
The URL shortening service has some associated drawbacks as well. Some of them are as follows:
We lose the originality of our brand by using a short URL generated by a third-party service. Many different brands using the same service get short URLs containing the same domain.
Since we're using a third-party service for URL shortening, the possibility of it getting shut down and wiping all our shortened URLs will always be there.
Our business brand image depends on the trustworthiness of a URL shortening service. The wrong business move might negatively impact our business. The competition for acquiring the in-demand custom short URLs is immense, so it might be possible that the best custom URLs are already taken by the time we start generating short URLs.
Several building blocks can be considered for the system design of TinyURL. Recognize these building blocks based on the following requisite functionalities in the design and provide your answer in the AI widget given below:
It’s necessary to:
Store the shortened URLs.
Provide unique IDs for each URL.
Store frequent URL-related requests to serve them efficiently.
Limit the number of incoming requests.
To move from concept to creation, we must first lay a solid foundation by specifying the requirements of the service.
Requirements for URL Shortening Design
Let’s look at the functional and non-functional requirements for the service we’ll be designing:
Functional requirements
Short URL generation: Our service should be able to generate a unique shorter alias of the given URL.
Redirection: Given a short link, our system should be able to redirect the user to the original URL.
Custom short links: Users should be able to generate custom short links for their URLs using our system.
Deletion: Users should be able to delete a short link generated by our system, given the rights.
Update: Users should be able to update the long URL associated with the short link, given the proper rights.
Expiry time: There must be a default expiration time for the short links, but users should be able to set the expiration time based on their requirements.
Note: Even though our system doesn’t reuse expired short URLs, we still delete them after their five-year lifespan. Keeping them forever would cause the data store’s search index to grow endlessly, increasing query time and adding unwanted latency.
Non-functional requirements
Availability: Our system should be highly available, because even a fraction of a second of downtime would result in URL redirection failures. Since our system’s domain is in URLs, we don’t have the leverage of downtime, and our design must have fault-tolerance conditions instilled in it.
Scalability: Our system should be horizontally scalable with increasing demand.
Readability: The short links generated by our system should be easily readable, distinguishable, and typeable.
Latency: The system should perform at low latency to provide the user with a smooth experience.
Unpredictability: From a security standpoint, the short links generated by our system should be highly unpredictable. This ensures that the next-in-line short URL is not serially produced, eliminating the possibility of someone guessing all the short URLs that our system has ever produced or will produce.
Note: Predictable short URLs can be a security risk; attackers might spot patterns and guess private links.To prevent this, our system avoids sequential IDs. Instead, it generates random, unpredictable identifiers, ensuring each short URL is unique, secure, and impossible to guess.
With our requirements clearly defined, we can now quantify the scale of the system.
Resource estimation
It’s better to have realistic estimations at the start. For instance, we might need to change them in the future based on the design modifications. Let’s make some assumptions to complete our estimation.
Assumptions
We assume that the shortening: redirection request ratio is 1:100.
There are 200 million new URL shortening requests per month.
A URL shortening entry requires 500 Bytes of database storage.
Each entry will have a maximum of five years of expiry time, unless explicitly deleted.
There are 100 million Daily Active Users (DAU).
Storage estimation
Since entries are saved for a time period of 5 years and there are a total of 200 million entries per month, the total entries will be approximately 12 billion.
Since each entry is 500 Bytes, the total storage estimate would be 6 TB:
URL Shortening Service Storage Estimation Calculator
| URL shortening per month | 200 | Million |
| Expiration time | 5 | Years |
| URL object size | 500 | Bytes |
| Total number of requests | f12 | Billion |
| Total storage | f6 | TB |
Query rate estimation
Based on the estimations above, we can expect 20 billion redirection requests per month.
We can extend our calculations for Queries Per Second (QPS) for our system from this baseline. The number of seconds in one month, given the average number of days per month is 30.42:
Considering the calculation above, new URL shortening requests per second will be:
With a
Bandwidth estimation
Shortening requests: The expected arrival rate will be 76 new URLs per second. The total incoming data would be