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

  1. Accept a long URL and generate a unique short code using base62 encoding of a sequential ID or hash.
  2. Store the mapping in a fast key-value store with persistence.
  3. Return the short URL to the user.
  4. On redirect lookup, cache popular mappings in memory to reduce database load.
  5. 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.