Functional requirements
1. Create a short URL from a given long URL (long → short).
2. Redirect a short URL to its long URL.
3. Short codes are randomly generated, not sequential, so they are
not enumerable/guessable.
4. Each URL has a fixed validity of 5 years.
5. No user information is embedded in the short URL
Non-functional requirements
1. Low redirect latency: p90 < 100ms, p99 < 200ms server-side
(excludes destination page load).
2. High availability: 99.999% (~5.3 min downtime/year)
3. Scalability: 100M reads/day → ~1,160 reads/s average, ~3,500/s at 3× peak
Out of scope
- Click analytics, custom aliases on URL, custom expiry, deletion.
Write Path
POST
/v1/createUser/
{user_id, pass} --> returns User instance
@RequiresAuthenticatedUser
PUT
/v1/createShortUrl/
{longUrl : "...." , idompotencyKey } --> accepts a long url and generates and returns a short url.
READ PATH
GET (Doesn't need authentication)
/v1/openUrl/
{shortURL : "...." } --> returns Long URL.
Components: User · API Gateway (×4, rate limiting) · UserAuth Validator (+ Bloom filter) · User Creation Service · URL Generation Service (×2) · SHA-256 Hash · Postgres · DynamoDB (3 replicas, quorum 3) · Redis Cache · CDN
1. Register user User →(1 Register User) API Gateway →(2) UserAuth Validator →(3 isValidUser) Postgres →(4) →(5 isValidUser Yes/No) API Gateway →(2 Register user) User Creation Service →(3) SHA-256 [Generate hash of password] →(4) back →(5 Insert User Details in Postgres) Postgres →(6 Success) back →(7 Success) API Gateway →(8) User
2. Create short URL User →(1 Create Short URL with Long URL) API Gateway →(2) UserAuth Validator →(3 isValidUser) Postgres →(4)/(5 Yes/No) → API Gateway →(6 Create Short URL) URL Generation Service →(7) Hashing Algorithm →(8) →(9 Insert Short URL to DDB) DynamoDB →(10 Success) →(11) Redis Cache [TTL 1 Month] →(12) →(13 Short URL) API Gateway →(14 Short URL) User
3. Open short URL (redirect) User →(21) CDN →(22 Open Short URL) API Gateway →(23 Open Short URL) URL Generation Service →(24) Redis Cache →(25) →(26 Cache miss → check DynamoDB) DynamoDB →(27) → (insert in Redis + return Long URL) →(28 Long URL for redirection) API Gateway →(29) CDN
Code generation note: User Id + Timestamp + given Long URL → SHA-256 → take first 10 chars; if already in DB, take 2–11; approach continues.
Other notes: TTL 5 years on DynamoDB (from expiry set at creation) · "TTL → 1 day" + "302 Redirection (Cache)" · API Gateway for rate limiting + Load Balancing on all three flows.
Basically, its a read heavy use-case. Hence, Key Value DB's like Dynamo DB can vend data in single digit milli second latency and with DAX it can go upto micro-seconds also, which fits well within P99 latencies. Secondly it automatically handles TTL which makes deletion of old records easy.
Dynamo Db can be partitioned based on Hash of the Short URL.
In the DB we can store,
Short URL as key with values as long URL, user_id (who created), creation_timestamp, expiry_timestamp. Dynamo Db TTL can be defined on expiry timestamp
In Short URL creation use-cases, we are not doing cross table join / cross table operations. Hence, No SQL Db's also fast write operations as well.
Redis Cache can help to achieve faster Reads, with single digit milli seconds and even DB reads can be minimized with Bloom Filters also here.
Currently, there is no use-case of List all user Links. If thats the case GSI can be used as Indexing strategy.
Since Dynamo Db is eventually consistent, we are using Chorum of 3 to have a consensus among reading. Secondly while writing on Dynamo Db, we should ensure that the writes are not updating the existing entry using attribute_not_exists.
For Same Short URL's it should not cause Dynamo Db hot partitioning. (Celebrity problem). Cache and CDN helps here. CDN can hold the result up-to TTL and avoid Hot partitioning on the DB and Stampede on Cache.
lets say if User Hits with wrong hash / invalid codes or Expired Codes, every time it wont be in cache and there is a chance that every request hits DB and results in DB stressed. hence, Bloom filters adds a Super validation layer to avoid that.
In case of Redis TTL lapsed and for legitimate Viral Codes, there is a chance that each missed code hits at-least once in the DB. As soon as its a Cache miss and found in DB, we basically update the cache to avoid future miss. To avoid multiple codes expiring at same time, we can add slight TTL jitter to Redis Cache.
In the write operations, Since we have a API gateway in place, that should limit the Write TPS / Bot attacks per Customer.
Api gateway is load balanced can even URL generation services are stateless services and can be horizontally scaled based on the need.
To avoid duplicate hash codes, for concurrent requests, we can change hashing to
distributed unique ID generator (43 bits mills + location (8 bits) + tie breaker (14 bits) base 62 which wont duplicate and results in 11 chars.