Course contentsShow
← System Design
Lesson 1523 of 1,91937. Real-World System: URL ShortenerFree lesson

Caching Layer Architecture

Introduce Redis or Memcached in front of the database to cache hot short_code → long_url mappings and reduce DB load.

Caching Layer Architecture

What You'll Learn

You'll discover how to introduce a caching layer (Redis or Memcached) in front of your database to dramatically reduce read load for hot URL mappings.


The Problem

Your URL shortener's database handles lookups for every redirect request. Even with indexes on short_code, the database becomes a bottleneck when serving millions of redirects per second. Popular URLs create "hot spots"—the same short codes queried repeatedly.


The Caching Layer Solution

A caching layer sits between your application servers and the database, storing frequently-accessed short_code → long_url mappings in memory.

How It Works

  1. Cache-Aside Pattern: When a redirect request arrives:

    • Check Redis/Memcached first for the short_code
    • Cache hit: Return long_url immediately (microsecond latency)
    • Cache miss: Query database, store result in cache, return long_url
  2. Popular URLs Stay Hot: Viral links remain in cache, never touching the database after initial load

  3. Time-To-Live (TTL): Set expiration (e.g., 24 hours) so cache doesn't grow unbounded and respects link expiration policies


Redis vs Memcached

  • Redis: Richer data structures, persistence options, replication support
  • Memcached: Simpler, pure in-memory key-value store, slightly faster for basic operations

Both excel at sub-millisecond lookups—perfect for read-heavy URL shortener workloads.


The Impact

With 80%+ cache hit rates (common for viral content), you offload 4 out of 5 database reads. A database handling 50K queries/second now handles just 10K, with the caching layer absorbing the rest at fraction of the cost.

Key Takeaway: A caching layer like Redis or Memcached transforms database-bound read performance by keeping hot URL mappings in memory, reducing database load by 80%+ for typical URL shortener traffic patterns.