API or data flow:
High level design:
The API GW acts as a LB and perform auth, rate limitation could also be added here.
Write service:
Read service:
The URL write/read generator can also have 3:1 replicas for availability and throughput.
We could add logic to dynamically add more instances if all the shards of URL service are busy in terms of CPU.
DB sharding:
we could shard on the short key URL and scale horizantally with an LB before.
Cache sharding: In Redis , we use short key as the partition key.
Hot shard problem: We used replication so that the hot key would be written to multiple replicas and reads will be load balanced.
We could addition think of adding more clusters, if above is not enough.
Deep dive:
Writes:
10^6/10^5 = 10 URls per sec which is trivial. We don't really need to scale.
For DB: 1M * 365*10 = 3.65B URLS. If each DB entry is 200 bytes.
700 GB. This can easily fit in 1 server. We can definitely have multiple for replication.
Cache size: We only 100 bytes for each entry.
so 3.65B*100 = 365GB. This can fit into redis cache with replication so that reads are fast.
Reads: 1000:1 = 1000 URL/sec.
If each URL take 100 msec to redirect. We need 30K/50 = 300RPserver. We can horizantally scale to 3-4 read servers.