POST/ shorten
Request: { longUrl }
Response: { shortUrl }
GET/{shortId}
The system uses a stateless, horizontally architecture optimized for low-latency URL redirection.
Client requests are routed through a Load Balancer to multiple Stateless API Servers. The API server handle URL creation and redirection logic. For read requests, the API server first check the Redis cache to retrieve the long URL. If the cache does not contain the mapping, the API server queries the NoSQL database and updates the cache.
For write request, the API server generates a unique short ID using the ID generator , stores the mapping in the NoSQL database, and updates the cache .
stateless API server
Responsibilites
=> Handle incoming requests for URL shortening and redirection
=>communicate with cache, database, and id generator
=>ensure statelessness to support horizontal scaling
Why stateless?
easy horizontal scaling
fault tolerance
ID generator
Purpose
Generate unique short IDs for URLs in a distributed environment
Design choice
=> use snowflake style ID generation or atomic counter + Base62 encoding
Working
=> API server requests a new ID
=> ID generator returns a globally unique numeric ID
=> ID is encoded using Base62 to generate short URL
Benefits
=> prevents ID collisions
=> avoid database auto- increment bottleneck
=> supports high write throughput
Cache and Database layer
cache(Redis)
=> stores short URL -> long URL mapping
=> Read through cache strategy
=> TTL- based expiration with LRU eviction
=> Reduce database load and improve read latency
Database(Nosql)
=> persistent key value storage for URL mappings
=> schema: short_url_id -> long_url
=> Horizontally scalable via sharding
=> Replicated for high availability
Failure Handling
Cache miss -> fallback to database
cache failure-> direct database access