Given a long url, it should be converted to a short url.
Given a short url, it should redirect to the long url.
A short url may contain an expiry timestamp after which the url should be inaccessible.
Short url visited count needs to be stored for analytics. This need not be real time.
Latency of redirection should be low.
Short urls should be unique (No 2 urls should map to same long url).
System should be available 99.9% of the time.
System should scale to support 100M DAU.
System should be able to handle 1B lifetime urls.
1 kb for each record which would need 1kb * 1B = 1 TB storage for database.
10M writes per month.
100M reads per month.
4 RPS for write and 40 RPS for read.
POST /shorten
{
"url" : "
"expiry" : "
}
GET /redirect/{short_code}
302 redirection
DynamoDB
{
"short_code" : varchar(8),
"url" : varchar(100),
"expiry" : int,
"created_at" : DateTime
}
Write Service:
Takes a long URL via POST and returns a short url.
Counter Service:
Returns a range of counts (Ex: 0-1000) when requested.
Counter Redis:
Redis store to store the counter.
Read Service:
Given a short url with short code via GET, long url is redirected.
Hot URLs Redis:
Frequently accessed short codes with their urls are cached here.
Database:
Database to store short codes with their long urls and other metadata.
Analytics Service:
Used to track analytics such as click count for short url, when GET is hit.
Analytics Redis:
Store to store analytics information temporarily, which is flushed every X duration and written to the database.
Expiry Flush Service:
Service which deletes all expired urls in the database, every X duration.
Write Service:
Write services accepts a long url. It requests a range of counts from Counter Service, which is used to create short codes with Base62. If range is exhausted, new range is requested.
Read Service:
Frequently accessed urls are cached and read from Redis. If not present, it is read from database.
Analytics Service:
Url click count is stored in Redis. It is later flushed out and written to actual database.
Availability may be sacrificed for latency, since we are relying on eventual consistency for replicas.
As counter grows, so will length of the short code.
System may go down if clicks of short url go crazy high.