Create Short Link:
POST /api/v1/shorten
Request: { "long_url": "https://example.com", "expire_at": "optional_timestamp" }
Return: { "short_url": "https://short.ly" }.
Redirection:
GET /{short_code}
Action: The server looks up the code and returns an HTTP 301 (Permanent Redirect).
Load Balancer: Distributes traffic to multiple web servers to prevent any single server from being overwhelmed.
Web/Application Servers: Handle the logic for generating short codes and managing redirects.
Database & Cache:
Database (NoSQL/SQL): Stores the mapping (Short Code → Long URL). NoSQL like Cassandra or
DynamoDB is often preferred for its massive scalability.
Cache (Redis): Stores the most popular links in memory. Since 20% of links usually get 80% of the traffic, caching these "hot" links makes redirection nearly instant
Shortening Algorithm (Base62 Encoding):
To get a 7-character code, we use characters [0-9, a-z, A-Z] (62 total).
provides ~3.5 trillion unique combinations, which is plenty for years of use.
Unique ID Generation:
Instead of hashing (which can cause collisions), the system uses a Distributed ID Generator (like Twitter Snowflake) to give every new link a unique number.
Example: ID 200921 converts to Base62 code zn9edcu.
Database Schema:
short_id (Primary Key, String)
original_url (String)
created_at (Timestamp)
expiration_at (Timestamp)