How to design a URL shortener service
· Category: System Design
Short answer
A URL shortener maps long URLs to compact codes that redirect users to the original destination efficiently and reliably.
Steps
- Accept a long URL and generate a unique short code using base62 encoding of a sequential ID or hash.
- Store the mapping in a fast key-value store with persistence.
- Return the short URL to the user.
- On redirect lookup, cache popular mappings in memory to reduce database load.
- Track analytics such as click counts and geographic distribution.
Tips
- Use a distributed counter or snowflake IDs to avoid collisions.
- Reserve certain codes to prevent offensive or misleading short URLs.
- Set TTLs for unused mappings to reclaim storage.
- Implement rate limiting to prevent abuse.
Common issues
- Hash collisions requiring additional checks or rehashing.
- Database hot spots from sequential key generation.
- Cache misses on new or rare URLs causing latency.
- Malicious URLs being shortened and distributed.
Example
# Consistent hashing for service discovery
import hashlib
def get_node(key, nodes):
hash_val = int(hashlib.md5(key.encode()).hexdigest(), 16)
return nodes[hash_val % len(nodes)]
node = get_node('user-123', ['node-a', 'node-b', 'node-c'])
This snippet implements consistent hashing to distribute keys across nodes, a foundational technique in scalable distributed systems.