Technology Stack
Responsibilities
Technology: PostgreSQL
Responsibilities
Schema Example
urls
-----
id
checksum
original_url
created_at
click_count
Indexes
UNIQUE INDEX(checksum)
INDEX(created_at)
Since retrieval is primarily by short code, a unique B-tree index is sufficient.
Technology: Redis
Responsibilities
Cache Flow
Request
↓
Redis
↓ (miss)
PostgreSQL
↓
Redis Update
For an MVP, an in-memory cache could be enough. Redis becomes valuable once multiple API instances are deployed.
Technology: Nginx
Responsibilities
Flow
Client
↓
Nginx
↓
FastAPI
Nginx
↓
-----------------------
↓ ↓ ↓
App-1 App-2 App-3
↓
Redis
↓
PostgreSQL
Strategies:
Current MVP:
App
↓
PostgreSQL
Future:
Primary DB
↓
Read Replica
Possible improvements:
Strategies:
Recommended tools:
Metrics:
Nginx provides:
Additional recommendations:
myapp.com/openai)Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
For URL shortening, we can use FastAPI REST APIs to expose endpoints that allow users to create shortened URLs, redirect users, and monitor service health.
The API service will handle:
Endpoint
POST base_url/v1/shortenUrl
Request Payload
{
"url": "http://example.com/aaaaaaaaaaa"
}
Processing Flow
User URL
|
v
Generate checksum
|
v
Check PostgreSQL
|
+------ Exists ------> Return existing shortened URL
|
+------ New ---------> Store URL mapping
|
v
Return shortened URL
{
"statusCode": 20001,
"data": {
"shortenedUrl": "https://base_url/a8f3d2"
},
"errorDetails": []
}
Where:
statusCode → Application-level response code.data → Contains generated shortened URL.errorDetails → Contains error information if request fails.Example error response:
{
"statusCode": 50001,
"data": null,
"errorDetails": [
{
"errorCode": "INVALID_URL",
"details": "Provided URL format is invalid"
}
]
}
Endpoint
GET base_url/{hash_id}
Where:
hash_id is the checksum generated for the original URL.Example:
Request:
https://base_url/a8f3d2
Flow:
Client
|
v
Redis Cache
|
Cache Miss
|
PostgreSQL Lookup using checksum
|
Retrieve original URL
|
301/302 Redirect
Response:
HTTP 302 Redirect
Location:
https://example.com/aaaaaaaaaaa
Endpoint
GET base_url/health
Purpose:
Example response:
{
"status": "UP"
}
Rate limiting can be implemented at two levels:
Using Nginx:
Client
|
v
Nginx
|
v
FastAPI
Advantages:
Example:
100 requests/minute/IP
FastAPI middleware-based rate limiting can be used for:
Example:
POST /shortenUrl
50 requests/minute/user
Since checksum is used:
URL A
|
v
checksum = abc123
URL B
|
v
checksum = abc123
Before storing:
GET /v1/analytics/{hash_id}
Provides:
DELETE /v1/url/{hash_id}
For URL expiration/removal.
The URL shortening system consists of multiple components responsible for accepting URL creation requests, storing URL mappings, serving redirects with low latency, and maintaining reliability and scalability.
The main components are:
Request
|
v
Redis
|
Cache Miss
|
PostgreSQL
|
Update Redis
Tools:
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...
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.