List functional requirements for the system (Ask the chat bot for hints if stuck.)...
User input the long URL and system need to return short url.
Whenever user enter short url. Redirect the user to original long url without any intermediate response
URL eviction policy is 1 year
We do not want user authentication
unique character should be 10 characters long
List non-functional requirements for the system...
Response time should be less then 300ms.
high availability
High scalability
Estimate the scale of the system you are going to design...
1000 request/sec is what we are expecting
Given the response time of 300ms.
1 request will take 300ms. then in 1 sec we can complete 10/3 ~= 3.33 request
means 3 requests will get complete by 1 thread in 1 sec.
so 1k request will need 1000/3 ~= 333 threads
Since our system is I/O bound. we can consider it will reduce wait time by 20%. i.e. 240ms for 1 request.
so, 1 sec it will complete 1000/240 ~= 4 request.
means 1k request will need 240 threads.
if 1 instance has 60 core. so we need 4 instance of application.
Database: We need to have total 240 db connection.
we are considering this as high read system.
database capacity:
databases consist of key value
key would short URL and value could object consist of long URL, create time, expiry time.
key (50 char) (50 bytes)
expiry time (8 byte)
create time (8 byte)
long URL (100 char) (100bytes)
total 1 item size is around 166 bytes
we round of to 200 bytes.
or we can say for 200 req/sec. it will be around
200*200*60*60*24*365*5 ~= 6TB considering data growth and some buffer we consider 10TB.
Define what APIs are expected from the system...
create short url
Post:
baseurl/shortining?queryParameter
input: longUrl
output: shortUrl
return short url with status code 200
Get actual url
Get:
baseurl/redirect/uniqueId
input=None
output= redirecturl
return status code 301 or 302 with redirect url
Defining the system data model early on will clarify how data will flow among different components of the system. Also, you could draw an ER diagram using the diagramming tool to enhance your design...
we will use NoSQL database. Because we do not have high ACID characteristic requirements.
we use database which nosql database in key-value pair
document store : ShortURL
document will contain:
longActualUrl
ShortUrl- primary key
createAtTime
expiry time
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
Client makes request.
If redirect request. Web address is resolved by DNS and the request is routed to regional CDN node.
If data is found it will return immediately. Otherwise, forward request to API gateway.
API gateways is responsible for load-balancing and routing to respective microservice.
Microservice checks cache if it found return else. query database. Add data to cache. And return data.
For create short URL: User submit long URL. Https direct URL to API gateway. API gateway direct to create URL shortener service.
Create some random key to append as path to shortURL. Update database. response 200 status code.
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Users submit the long URL. Https request goes to regional CDN. If found return the redirect url. Else forward to API gateway. API gateway forward request to redirect service for redirect to original. For creating short URL api gateway forward request to create url shortener service.
if create url shortener generate short url and update database with new entry.
redirect url read from cache if not found query database. we update cache with data from database. we return data to api gateway. update the CDN and return to user.
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...
API Gateway: Will be responsible for routing as well as load balancing. We have service discovery where each instance on start register itself with service discovery.
API gateway will query service discovery to find the active instance ip address and port.
Each service periodically send heartbeat to service discovery and API gateway will will query and cache data of active srevice from service discovery.
Redirect Service: Query the cache if found return. Else get from database update the cache and return.
Cache we are using key-value store cache. Cache eviction policy can be Least recently used.
create URL shortener service: will create entry into the database with corresponding short and long URL
Database: We have selected NoSQL database. SQL database is not selected because we do not have high ACID requirement.
We can use database with key-value store.
Explain any trade offs you have made and why you made certain tech choices...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?
URL customization. User authentication.