COST ESTIMATION:
Assume: 200 WPS => 200 * 86400 / day
read : write = 100:1
therefore number of redirects : 20000/sec
Storage:
short_code : 7chars => 7bytes
long_url: about 100bytes
user_id: 20bytes
created timestamp: 8bytes
estimated timestamp: 8bytes
total 1 record = 7 + 100 + 20 + 8 + 8.= 143 bytes
== 256 bytes (when metadata, indexes are involved)
therefore daily storage => 256 * 200 * 86400/day = approx 4.4 GB/day
Bandwidth:
Write : 50KB/sec => negligible
Read (Redirect) 5MB/s (due to large payload of long_url)
Caching:
Storing last 30 days of fetched mappings => 80 to 90% cache hits
req payload:
long_url: string
expires_at: timestamp
auth : Api key in header
Response: 201 Created
short_code: string,
long_url string (for validation)
createdAt: timestamp
expiresAt: timestamp
authentication: using API key in sent in headers
rate limit: 100 req/s per user for free tier, piad subscriptions can have higher.
return 429 with a Retry-after header if too many requests.
Validation: The server should validate that the long_url is a well formed url with a supported scheme (HTTPS, HTTP) reject javascript:. ftp: data: to prevent abuse.
Check if url is reachable using the HEAD request, although this will increase latency for create, but is bearable.
2.GET /api/v1/url
params: short_url = short_url
success response: 302 Found
response header: Location: long_url
auto redirection without authenticaton
If the short_url has been expired or doesnot exist, return 404.
For expired link, send a message field with response stating that the link has expired.
Other headers to be included: Cache-Control and X-Robots-Tag to tell search engines whether to cache the short_url or original url.
301: Moved Permanentely, Browser stores the mapping in cache. There for high reads, bit no analytics or stale date validation for this matter
The user will send a post request to generate a short_url for a given long_url. The user can optionally send a namespace , in order to prefix the short_url with the organisation name.
The request flows through the Load Balancer and the API Gateway, which handle TLS/SSL termination, and authentication respectively.
The request finally reaches the Shortening Service, where it uses a hash based algorithm to generate hash of the long_url and take first 7 bytes from the hash in order to generate a short_id.
This short_id will be unique across shards and will be stored asynchronously in the KV store using a message Queue and queue worker.
2.GET request path
The user when requests for the original url to be redirected using the short_url, the request first goes to CDN, which will cache the most frequently used urls. If it is a hit then the request doesnot flow into the Backend , and is directly returned with the appropriate url.
The request reaches the Mapping Service, where it checks in the cache for the original_url, given the short_id, if it is a hit, then it is returned and stored in CDN.
If it is a miss, then a DB call is made, and the cache is updated. (Read Through Policy)
Caching Strategy:
Problems that might occur:
Schema
short_url: string
long_ur: string
userId: uuid
created_at: timestamp
expires_at: timestamp