Read Path: Cache-Aside Pattern
What you'll learn: How Pastebin retrieves paste content efficiently by checking Redis cache first, then falling back to slower storage only when necessary.
The Two-Tier Lookup Strategy
When a user requests a paste, your system shouldn't always hit the database or object storage—that's slow and expensive at scale. The cache-aside pattern (also called lazy loading) puts a fast Redis cache in front of your storage layer.
Here's how it works:
- Check cache first: When a read request arrives, query Redis with the paste ID
- Cache hit: If found, return the content immediately (fast path)
- Cache miss: If not found, fetch from database/object storage (slow path)
- Populate cache: After fetching from storage, write the content to Redis for next time
- Return to user: Send the content back
The Library Analogy
Think of Redis as your desk and object storage as a warehouse across town. When you need a document:
- First, check your desk (Redis)—instant access
- Not there? Drive to the warehouse (object storage)—slower
- Bring a copy back to your desk for next time
Why Cache-Aside for Pastebin?
Pastebin is read-heavy. Popular pastes get accessed thousands of times. By caching them in Redis:
- First read: ~100ms (database + object storage fetch)
- Subsequent reads: ~1ms (Redis in-memory)
The application controls cache population—storage never talks to cache directly. On cache misses, you explicitly load and set the data. This gives you flexibility: you decide what to cache, when to invalidate, and what TTL to use.
Key Takeaway:
The cache-aside pattern reduces Pastebin's read latency by 100x for popular pastes—the application checks Redis first, falls back to storage on misses, and proactively populates the cache for future requests.