We will use HTTP/REST for this. This is the proposed API:
POST /v1/url
body:
{
"longUrl": "https://www.example.com"
}
We will use HTTP/REST for this. This is the proposed API for redirect:
GET /v2/url/{id}
The database will be a key/value store to store tinyUrl ids to longUrl mappings. I will use DynamoDB for this which is eventually consistent by default but supports strong consistency if we need that later. DynamoDB supports single digit millisecond queries via the primary key, I can also use the primary key autogeneration feature to ensure Here is my proposed schema:
tinyUrlId (primary key) | longUrl
TinyUrl service will be our main service. We will scale this horizontally behind a load balancer, since our system is stateless we can utilize round robin load balancing to ensure even traffic distribution. We include an LRU cache for storing recently requested tinyUrl mappings to speed up our reads. The write functionality will write our long url to url mappings table and take advantage of DynamoDB's autogenerated key feature to create tiny urls. The service will return the tiny url to the client after writes. The read functionality will take a tinyUrl and the service will read from our cache to see if the mapping is there currently, in the event of a cache miss it will check our DynamoDB table and return a 301 redirect with the long url redirection.
We should have backups for our URL Mappings table as this is a single point of failure, AWS provides this functionality.
Possibly look into having a separate service for handling reads vs writes. Use of a queue would ensure writes get delivered.