Detailed Component Design
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
The design can be implemented internally as a 2 separate micro service. 1 micro-service generates the shortURL while the other one reads the short URL to return long URL
- The workload is read heavy so we can optimize the latency for the read path by adding dedicated read servers which only handle the read traffic.
- To further optimize the read latency a layer of cache is added and the cache can be MRU cache to ensure the most recently frequently accessed values are in the cache and only cache miss is sent to the DB. In case of cache miss the request is then served from data stored in the database.
- Further we can have a higher number of read servers compared to the generated data servers and the system overall can be uncoupled.
- The database will be a key value store since there is no need to maintain data in multiple different tables or even database joins.
- Data in the DBs is replicated to ensure high reliability and since we are ok with eventual consistency of upto 10 secs to be replicated everywhere , we can use consensus protocol to replicate and return data and gossip protocol to ensure the data stays up to data across different copies of data . This provides high reliability.
- DB also leverages consistent hashing to ensure the easy scalability of database layer and also sharding the data for easy partitioning along with easy to manage data re-distribution or handling hot shard problem
- Data base can be exposed via DB API to ensure we don't tie the business logic to internal database platform/implementation logic
- The generate Short URL API has to generate a unique URL very quickly and ensure the uniqueness is maintained in a distributed hash table across all the distributed database shards. But distributed data storage and URL generation creates a challenge for uniqueness of the result.
- To ensure the uniqueness of the generated short URL we can use hash function with more sophisticated collision resolution approaches
- Use a centralized unique key generator service which generates monotonically increasing integers and prepend or append it to the generated hash for the shortURL to ensure uniqueness
- Or allocate range for the hash function to each app server to generate the URL within that range
- Use loadbalancer to handle the performance and scalability/latency requirements for handling the request distibution along with rate Limiting and API quotas in place to avoid the issues like noisy neighbors /resource abuse or even configurable quotas for the premium customers
- Rate throttling Algo like leaky bucket algorithms can be implemented to handle the traffic burst effectively
- In case of generator service outage , we need to have a fall back mechanism like a local cache of previously generated ids or a secondary generator identifiers that can take over in case of failure
- In case of network partition , we can use a consensus Algo like Paxos or Raft to ensure that only 1 node can generate an ID at a time and each app server should query the centralized Id service before a new URL is created.