Low redirect latency: The target redirect latency is P99 < 50ms, the database operation could take a lot of time, current idea is to cache the frequently used TinyURL.
High availability: The server needs to have at lease 2 pods, if the main pod is down, the sub pod can support.
Horizontal scalability: It could be over 10,000 clients requesting server at the same, the server should be deployed on multiple pods, and api call can by dynamically distributed.
Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
Two APIs are needed:
Request body:
{
"url":
}
Response body:
{
"short_url":
}
Response: 302 Found
Location: https://tiny-url.com
Only one table is needed, the entity design can be:
{
id:
short_url:
original_url:
updatedAt:
updatedBy?:
}
The primary key will be id and short_url, we should add index for both short_url and original_url. When user tries to update the original url, it'll be convinent to find and update.
Consistency:
Writes (create): Strong consistency to let user always see the most recent write.
Read (read): Eventual consistency to give quick response.
Partition:
Partition key: short_url
Number of shards: 3 shards
Re-sharding: Managed DBs can handle re-sharding automatically.
ID Generation Service
Functionalities:
Redirect Service
Functionalities:
Cache Layer
Create a cache service, using LRU eviction policy, to cache the latest 100 requests. If the cache missed, find the record in DB and cache it.
Add read replicas per shard.
Rate Limiter
Use Token Bucket to prevent abuse.