My Solution for Designing a Simple URL Shortening Service: A TinyURL Approach with Score: 6/10
by kritsnam
System requirements
Functional:
- The Service should return shorten URL
- The Shorten URL's cn't be deleted.
- Shorten URL can be a combination of characters and numbers.
- If given a shorten URL then it should redirect to the original URL
Non-Functional:
- Scalable
- Reliable
- Fault Tolerant
Capacity estimation
- Write Operations = 100million URL's generated per day
- Write Operatiosn per second = 1000/(24*3600) = 1160
- Read Operartions Per second = Considerig 10:1 read to write ratio then read operations = 1160*10 = 11,600
- Average URL length = 100 bytes
- 1 day storage = 100million * 100 bytes = 10^10 bytes = 10GB
- Let's assume that the serivce will run for 10 years = 10GB*10years*365days = 36.5TB
Total sorage needed is 36.5TB
API design
For Url shorteining service we primarly need two API
- URL Shortening - Client will do a POST request with the original LONG URL to a sample endpoint that might looks like "POST api/v1/data/shorten".
- URL Redirecting - Client will do a get request with the shorten URl to get the Oiginal URL "GET api/v1/data/redirect"
Database design
For the high level design, mapping of
High-level design
- As there are more read than writes, mapping is stored im cache to increase performace.
- Load Balancer is introduced to handle and distrubute the load evenly to web servers.
- IF the short URL already in cache then return it, if not then get the short URL from database and store the mapping in cache for future references.
Request flows
- Long URL is the input.
- System Checks if Long URL exist in the database.
- If it exisit the it means then the URL has already been queried before. The system responds back with the short url.
- If not then New ID is generated for the long Url.
- Apporiate Algorithm is written to convert the Long Url to short URL.
Detailed component design
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Trade offs/Tech choices
While URl redirecting we can have 2 Status codes. response cde 301 and 302. with response code 301 the browser will permanently store the response in the cache so any subsequent request for the same URL will niot be sent to the shortening service. For 302 response code the browser "temporarily stores the result" hemce each reque4st is redirected to long URL increasing the latency.
Failure scenarios/bottlenecks
Try to discuss as many failure scenarios/bottlenecks as possible.
Future improvements
- We can introduce rate limiters to manage exessive load and Dos and DDos attack.
- Database scaling or sharding is another technique that we can implement for efficient querying and resource management.