Why Caching Matters
Database query: 50-100ms. Cache lookup: 1-5ms. That's a 10-50x improvement with a single optimization.
Caching is the performance multiplier that makes modern internet-scale applications possible.
The Speed Gap
The further you go from the CPU, the slower things get. Caching keeps frequently accessed data in faster storage tiers.
The Caching Pyramid
Each layer catches requests before they reach slower layers below.
Real-World Impact
Amazon found that every 100ms of latency cost them 1% in sales. At their scale, that's billions of dollars. Caching is not optional - it's essential.
Twitter caches pre-computed timelines for users. Without this, generating a timeline would require querying thousands of accounts in real-time - impossible at their scale.
What Can Be Cached?
Database Query Results
User profiles, product listings, configuration data
Computed Results
Recommendations, search results, analytics aggregations
Static Assets
Images, CSS, JavaScript, fonts
API Responses
External API calls, microservice responses
The Cache Hit Ratio
The most important metric in caching:
Cache Hit Ratio = Cache Hits / (Cache Hits + Cache Misses)
Basic Cache Implementation
class SimpleCache:
def __init__(self):
self.cache = {}
self.hits = 0
self.misses = 0
def get(self, key):
if key in self.cache:
self.hits += 1
return self.cache[key]
self.misses += 1
return None
def set(self, key, value):
self.cache[key] = value
def hit_ratio(self):
total = self.hits + self.misses
return self.hits / total if total > 0 else 0
When NOT to Cache
Caching isn't always the answer:
Avoid Caching When:
- Data changes too frequently - Real-time stock prices, live scores
- Data is rarely accessed - Long-tail content with no repetition
- Consistency is critical - Bank balances, inventory counts
- Cache is larger than the data - No benefit, just overhead
Key Takeaways
- Caching = Speed - 10-50x faster than database queries
- Multiple layers - Browser, CDN, application, database
- Hit ratio matters - Aim for 90%+ for production systems
- Not a silver bullet - Some data shouldn't be cached
Next up: Caching Patterns - Learn cache-aside, write-through, and write-behind strategies.