Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
POST /api/url/shorten Request: { "long_url": "https://verylongurl.com/..." } Response: { "short_url": "tinyurl.com/abc123", "expires_at": "..." } Optional: { "custom_alias": "my-link", "ttl_days": 30 }.
redirect -
GET /{short_key} → 301 Moved Permanently Location: https://verylongurl.com/...
delete
DELETE /api/url/{short_key} Request Header: Authorization: Bearer <token> Response: 204 No Content
get url info
GET /api/url/{short_key}/info Response: { "long_url": "...", "created_at": "...", "clicks": 12345 }
Describe the overall system architecture. Identify the main components needed to solve the problem end-to-end. Use the diagramming tool to create a block diagram.
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
-- Core Table: URL MappingsCREATE TABLE url_mappings ( id BIGINT PRIMARY KEY, -- sequential ID from ID generator short_key VARCHAR(10) UNIQUE NOT NULL, -- "abc123" (base62) long_url TEXT NOT NULL, -- original long URL user_id BIGINT, -- optional: who created it created_at TIMESTAMP NOT NULL DEFAULT NOW(), expires_at TIMESTAMP, -- NULL = never expires click_count BIGINT DEFAULT 0 -- denormalized for speed);-- Indexes for fast lookupsCREATE INDEX idx_short_key ON url_mappings(short_key);CREATE INDEX idx_user_id ON url_mappings(user_id);CREATE INDEX idx_created_at ON url_mappings(created_at);
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
Algorithm: Base62 Encoding
Collision handling:
UNIQUE INDEX on short_keyHard to guess? Base62 encoded sequential IDs are predictable. For better security: hash the sequential ID (e.g., take last 7 chars of MD5(ID + secret_salt)).
High availability: Each app server can pre-fetch a batch of IDs (e.g., 10,000) from the ID generator — if the generator goes down, servers still have a local buffer.
| Aspect | Design |
| Cache type | Redis (in-memory, sub-ms reads) |
| Key | short_key |
| Value | long_url |
| TTL | 24 hours (LRU eviction if full) |
| Write-through | On URL create, immediately cache it |
Cache miss path:
Client → Redirect Service → Cache Miss → Query DB → Return redirect → Write to Cache
Viral link protection:
Cache invalidation:
DEL short_key in Redis + purge CDNShard by short_key hash:
shard_id = hash(short_key) % N
| Benefit | Why |
| No scatter | One shard hit per redirect — no cross-shard queries |
| Even spread | Hash distributes uniformly |
| Hotspot mitigation | Redis cache absorbs hot keys before they hit shar |