How to design a URL shortener service

· Category: System Design

Short answer

Use a hash function or base62 encoding to generate short keys, store mappings in a database, and redirect via HTTP 301. Plan for high read volume and collision handling. For caching strategies, see how caching improves system performance. For delivery speed, see how cdns speed up content delivery.

Steps

  1. Choose a key generation strategy: hash + base62 or pre-generated keys
  2. Store short-to-long URL mappings in a key-value or relational database
  3. Build a redirect API that returns HTTP 301 or 302
  4. Add caching for hot URLs to reduce database load
  5. Monitor usage and handle collisions or expiration policies

Tips

  • Use separate read replicas if read volume is much higher than writes
  • Cache heavily accessed URLs in Redis or Memcached
  • For database schema considerations, review sql vs nosql for system design