Requirements
Functional Requirements:
- Create a short URL for a given long URL.
- Return the long URL associated with a given short URL.
Non-Functional Requirements:
- horizontal scalability
- low latency less than 1s p99
- high availability
10k links each 2 qps
give or take 20k QPS
heavy read possibly 1000 to 1
scalable to 1 billion urls 100 million dau
storage req 1 billion * 100 byte = 100 GB
create url
POST /create
header { user auth token jwt }
req
{ originalURL: "" }
res
{ shortURLKey: "" }
retrieve
GET /
res -> redirect to the original URL
for creating new short url
client hit api gateway -> authenticate user and check rate limit to avoid ddos
once passed api gateway call shortener app
shortener app
- validate valid url
- hash the url and get the short key (eg first 6 char char 0:6)
- insert in db
- if duplicate and original url is the same return the existing short key
- if duplicate but original url is different rehash e.g char 1:7) and try reinsert
- also update cache
for retrieval
client hit api gateway
api gateway hit the retriever app
each app instance lookup cache if key exists if yes return directly
else hit the db query by the key
for scalability
each app is horizontally scalable
cache -> e.g. redis cluster, add more node for horizontal scalability + add read replica
for db -> add read replica if needed (if sql relational) else if use nosql eg. dynamo db can increase read capacity unit
Database Design
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
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.