1. POST /shorten
Purpose: Create a short URL for a given long URL.
Request:
json
Copy
Edit
POST /shorten
Content-Type: application/json
{
"long_url": "https://www.example.com/some/very/long/path",
"custom_alias": "myalias", // optional
"expiry": "2025-12-31T23:59:59Z" // optional
}
Response:
json
Copy
Edit
HTTP 201 Created
{
"short_url": "https://short.ly/myalias"
}
Notes:
If custom_alias is provided, check uniqueness.
Validate the URL format.
2. GET /{short_code}
Purpose: Redirect to the original long URL.
Request:
bash
Copy
Edit
GET /abc123
Response:
http
Copy
Edit
HTTP 301 Moved Permanently
Location: https://www.example.com/some/very/long/path
3. GET /analytics/{short_code}
Purpose: Get usage data for a short URL (for authenticated users only, if applicable).
Request:
sql
Copy
Edit
GET /analytics/abc123
Authorization: Bearer
Response:
json
Copy
Edit
{
"short_code": "abc123",
"long_url": "https://www.example.com/...",
"created_at": "2025-05-10T12:00:00Z",
"clicks": 2547,
"expiry": "2025-12-31T23:59:59Z"
}
Optional APIs (for full product):
DELETE /{short_code}: Delete a link (if owned by user).
PUT /{short_code}: Update expiry or destination.
GET /health: Health check for monitoring tools.
Authentication (optional):
If you support user accounts, protect APIs with OAuth or JWT.
Anonymous users may still create links, but won't get analytics/editing.
We can use a simple table like:
ShortCode LongURL CreatedAt Expiry ClickCount
Options:
SQL: Easy to query, index on ShortCode.
NoSQL (e.g., DynamoDB, Cassandra): Better horizontal scaling.
Client → API Gateway → URL Shortening Service → Database
↓
Short URL Returned
Step-by-step:
Client sends a POST request to /shorten with the long URL.
API Gateway routes the request to the URL Shortening Service.
URL Shortening Service:
Validates the URL.
Generates a unique short code (e.g., via base62-encoded ID or hash).
Stores {short_code → long_url} in the database.
Optionally caches the mapping in Redis.
Returns the full short URL to the client (e.g., https://short.ly/abc123).
{short_code → long_url}.short.ly/abc123.abc123.Preferred: Base62-encoded ID from Redis/DB or Snowflake — short, scalable, and easy to decode for debugging.
Preferred:
Preferred: Redis — it supports TTLs for short links and can be used for atomic counters too.
Preferred: Async with Kafka or SQS, with a consumer writing to an analytics DB.
Cause: DB crash, connection pool exhaustion.
Mitigation: