POST /api/v1/urls
Takes request body
longUrl: The url which needed to be shorten.
UserId: Used to attach urls to users for analytics.
CustomCode (optional): If user wants a custom short code.
TimeToLive: If user specifies the expiration time.
GET /shortCode
returns 302 (found)
with response containing
ShortCode: The shortened code.
ShortUrl: Url with short code which is ready to use.
LongUrl: Used of testing of consistency.
Short URL redirect:
flowchart TD
B[client] --> C{CDN}
C --> D[API Gateway]
D --> E[Service]
E --> F[Redis Cache]
F --> G[DynamoDB]
Short URL creation:
flowchart TD
B[client] --> D[API Gateway]
D --> E[Service]
E --> G[DynamoDB]
URL creation:
A random base62 of length 7 is created and service tries to conditional write it on Database.
It helps us to return existing shortCode if longUrl already exists or else a new entry is made.
This method helps us to eliminate race condition.
Random shortCode generation is used inorder to eliminate the birthday paradox arises with the truncation of SHA 256 hash to 7 characters.
Tradeoffs -> Increases latency to check for longUrl before write.
But it doesn't affect user experience since every body accepts creation takes time naturally.
URL redirect:
This component is key area. It handles most of the load because of 100:1 ration of redirects is to that of creation.
CDN (Hot cache) -- It stores the redirects of top 2% viral links which contributes a alot of load.
Redis (Warm cache) -- It stores values through cache -aside mechanism which helps to include only active shortCode pairs.
Horizontal scaling - We can increase number of instances by utilising short code sharding.