🔥 0
0
Lesson 1 of 6 12 min +150 XP

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

~1ns
L1 CPU Cache
~100ns
RAM Access
~1ms
Redis/Memcached
~50ms
Database Query

The further you go from the CPU, the slower things get. Caching keeps frequently accessed data in faster storage tiers.

The Caching Pyramid

Browser Cache
CDN Edge
Application Cache
Database Query Cache
Database Storage

Each layer catches requests before they reach slower layers below.

Real-World Impact

Amazon's Discovery

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's Timeline

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)
70%
Needs improvement
90%
Good
99%
Excellent

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.

🧠 Quick Quiz

Test your understanding of this lesson.

1

What is the primary benefit of caching?

2

Which layer of caching is closest to the user?

3

A cache hit ratio of 95% means what?