User should be able to generate a shortened url for a long url
When clicking on a shortened url users are redirected to the long url
Should scale for a large number of users accessing the shortened url
Service should be generally available
For 1 million urls a month or ~10 million a year
30 days * 3600 seconds ~ 100000 seconds which is about 10 urls a second, so low traffic
Lets say long url about 100 bytes, and shorted url is 8 bytes plus a unique id of 8 bytes so say 200 bytes per mapping
For a year 10 mil * 200 bytes = 2 billion bytes ~ 2 gb a year which can fit in memory
Define what APIs are expected from the system...
POST /shorten -> return shortened url (string)
{
longURL: string
}
GET shortURL -> 3XX redirect to long url
{
}
mapping
shorturl
long url
API gateway to expose endpoints. LB for possible scaling in the future. Shorten service will handle shortening and redirection. Database to store the short to long mapping
When user requests short url, the shortener service will generate a new shortened id (hash) and store the mapping in the db. When accessing the shortened service will translate the url and convert it back and redirect the user.
The database can be a postgres database. But for fast access it can be a redis instance. This can be backed by another database or ocassionally snappshoted to S3. This allows for relability if the service goes down.
For simplicity shortener service handles both shortening and redirecting but could be split (possible bottleneck)
If using redis + databse or blob storage then there might have to deal with synching sources and handling brining up services if they go down.
For very high shortening traffic, it maybe worth scaling the shortener service. Traffic will be distributed by LB.
It's also worth separating the shortener service into 2 services. One to handle the shortening and another to redirect the user. This is because there will be more traffic trying to redirect rather creating new urls.
If there are hash collisions then we could append a random id and rehash until no collision
Handling url expriration