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
Schema
short_url: string
long_ur: string
userId: uuid
created_at: timestamp
expires_at: timestamp