Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
POST /url/create
input -> original url
output -> shortened url
GET /url
param -> short url
output -> long url
Load Balancer :- DIstributes incoming traffic to API Gateway.
API Gateway :- All the API information and there redirection logic to services
Read Service :- Uses Cache and DB to redirect to the original url. This will have GET api logic
Write Service :- Generates ID and shorten the url and store it in the DB. This will have the POST api logic.
The short_code is generated using Base62 encoding ensuring no two urls have same short code.
Cache :- Cache layer on top of DB
Database:- DynamoDB . Stores the mapping between short url and original url, supporting fast lookups for redirect.
URL Mapping Table
Need dynamodb for key value pair lookup where key is short_code and value is long_url.
Write consistency:- Strong consistency
Read consitency:- Eventual consistency (default).
ID Generation - ID are created using Base62 ensuring no two long urls have same id.
short_code - It will be generated randomly + unique constraint.
Caching strategy - For hot urls/ viral link, we will use Redis hot cache. TTL will be based on expires_at value. On miss, fall back to dynamodb and refresh the cache with fetched mapping.
At scale, the hash of short_code (partition_key) routes exactly one shard.
If cache goes down, then all the traffic will be redirected to DB till the cache comes back up.
If DB goes down (dynamoDB has three replicas), all the write service operation will be halted. If this is a regular scenario, then we can use SQS/ queues to store the request and process it once db/service comes back up.