Detailed Component Design
- DB: since the data is well structured we can use SQL db(e.g. postgres). It also grants consistency of our data.
- to grant horizontal scalability we should enable replication and sharding
- To garanty low latency response we are going to use key-value cache(Redis) before lookup for the DB. In case of cache miss service will fallback to DB. extracted value will be populated in cache(with TTL=7days). Eviction model is LRU.
- for scalabilty we can introduce multiple app servers that will partition incoming requests
- we are also going to use load balancer in front of our api getway
- To garanty fault tolerance we also will have redundant LB, app servers and DB to hands outages.
- App servers should contains following services: a. url generation b. redirection c. analytics
- In case if colision happend for generated url: i.e. generated url coinsides with another previously generated url we could 1. fallback to some reserved urls to workaround it 2. continue incremental url generation until uniq one will be generated
- to garanty low latency redirection there are some techniques could be used, we could have message queue(kafka) in front of redirect service that will contain all redirect requests, and redirect service will have multiple users that will subscribed on this queue.
- To speed up redirection we could also use CDN allowing faster url resolve and redirect based on user geographical location
Availability will be acheived because of used redis cache. This sharded cache grants low latency during query processing. Sharded DB also impacts availability. in case of network partition data still will be available.
Horizontal scalability could be achieved by adding new DB shards, new instances of app servers and redis cache. Usage of the load balancer will grand horizontal scalability.
To prevent surge of redirection requests and DDoS attackes we will limit amount of queries per user.
Collision resolving strategies: when newly generated url already exists we don't populate it into DB we continue generation until wont produce uniq shorted url, we can do it in following way, generate ssh-256 hash, and calculate base32(hash), if case of collision just hash++ and repeat until base32(hash) is unique