lets assume there are k = 1M queries / day
in a second, there would be 1000000/24*60*60 QPS
Lets just assume for every 5 reads there is a write
R/ W ratio = 5
DB usage
Egress/bandwidth
Define the APIs expected from the system. This is your chance to analyze and define the read and write paths so that you can come up with the high-level design...
Request body {
url: string
}
Response;
The HLD for URL shortener has two main flows one for creating and the other for getting the long URL. The first service is the URL shortner service when Client adds any long url it is stored into a DB ( consists of Short -> long url mapping)
The other service is when the User puts in the short URL to get the long key from a cache which is short lived, if not found in cache search in the same DB
Add a load balancer to handle the requests being directed to each of these services, also the URL shortner service has the logic for an ID generation strategy
The database can simply be sql with two columns in it 1 is the short url and 2 is the long url.
The data model would look like
Table url-shortener
Columns
id
short-url
long-url
created_date
expires_at
the short-url is the primary key and possibly a SQL index can be created on short-url as it helps the process of read queries which from the Read Write ratio are already very high.
New Url creation requires strong consistency to avoid duplicates, redirect reads can tolerate eventual consistency - a brief phantom read that returns a 404 that can be retried.
Read replicas for read traffic, sharding by short-key hash when the DB outgrows a single node
Ways for ID generation