Solution for Designing a Simple URL Shortening Service: A TinyURL Approach
by kaleidoscope4544
Requirements
Functional Requirements:
1.Shorten URL: Given Long URL , generate short URL. Here user can also provide custom URL.
2.Redirect URL: Given short URL, redirect to long URL
3.Link Expiration: Set TTL for URL and on expiring Link should return 404.
Non-Functional Requirements:
1.Highly available: Redirect path must be available all times.
2.Lower Latency: Redirect path must complete in 10ms.
3.Horizontal Scalable: System must scale to billions of stored URLS.
4.Durability: Once related, URL mapping never be lost.
Capacity Estimation:
Traffic: 200 shortURLs/sec
Storage:
- Short code (7 bytes)
- Long URL (approximately 100 bytes average)
- User ID (20 bytes)
- Created timestamp (8 bytes)
- Expiration timestamp (8 bytes)
143 Bytes per entry.
Daily: 200*250*86400->4.4GB /day
Bandwidth:
Write bandwidth: 200 requests/sec times 256 bytes = roughly 50 KB/sec
Read bandwidth: 20,000 requests/sec times 256 bytes = roughly 5 MB/sec
Cache Sizing:
Cache atleast 20% of top daily active URLs.
API Design
1.Create Short URL
POST /api/v1/urls
Request Body:{
"longURL":"Destination URL to shorten",
"customAlias":"optional",
"expiresAt":"ISO Timestamp"
}
Response:{
"shortCode":"Custom Short Code",
"shortURL":"full short URL",
"longURL":"Echo long URL",
"createdAt":"Timestamp of creation",
"expires at": "Expiration Timestamp"
}
RateLimiting: 100URLs/hr/API key for free account other higher for Paid.
Returns 429 in case rate limit exceeds.
2.Redirect:
GET /shortCode
Return 302(Found) response with Location header set to original long URL.
Returns 404(Not Found) :in case not found with JSON body error explaining.
For expired Links, add msg for expiry.
High-l Design
The architecture separates into two distinct paths, each optimized for its unique requirements.
Read (Redirect) path: Client click --> CDN --> API Gateway --> Mapping Service --> Redis Cache --> DynamoDB. Optimized for sub-10ms latency with multiple caching layers. The CDN handles the hottest URLs at edge locations. Redis handles the warm set in the data center. DynamoDB is the durable fallback for cache misses.
Write (Shorten) path: Client request --> API Gateway --> Shortening Service --> Message Queue --> Queue Worker --> DynamoDB. Optimized for reliability and database protection. The Shortening Service generates a guaranteed-unique code from its pre-allocated ID range and enqueues the write. The message queue decouples the client response from the durable write, smoothing traffic spikes.
Detailed Component Design
Components
API Gateway: TLS termination, DDoS protection, request routing, and rate limiting for write requests. All external traffic enters through the gateway, providing a single enforcement point for authentication and throttling.
Shortening Service: Generates short codes from a pre-allocated ID range, validates inputs, and enqueues write requests. Each instance owns an exclusive range of IDs, so codes are guaranteed unique at generation time. Stateless and horizontally scalable behind a load balancer.
Mapping Service: Handles redirect lookups. Checks Redis first, falls through to DynamoDB on cache miss. Also stateless. This is the most latency-sensitive component.
Message Queue (SQS or Kafka): Buffers write requests between the Shortening Service and the database. If 2,000 URL creation requests arrive in one second, the queue absorbs the spike and the Queue Worker drains them at a steady 200 per second that the database can comfortably handle.
Queue Worker: Consumes messages from the queue and writes URL mappings to DynamoDB. Since codes are generated from exclusive ID ranges, collisions cannot occur, and every write succeeds on the first attempt. Rate-limited to match database write capacity.
Redis Cache: Stores the warm set of URL mappings (recently and frequently accessed). LRU eviction policy ensures the cache holds the most useful entries. Cluster mode for horizontal scaling across multiple shards.
CDN (CloudFront): Caches redirect responses at edge locations worldwide. For a viral URL, the CDN serves millions of redirects from a nearby edge node without any request reaching the backend. This is the single most impactful component for read performance.
DynamoDB: The durable source of truth for all URL mappings. Handles cache misses from Redis, supports conditional writes for custom alias conflict detection, and provides automatic TTL-based expiration.