Course contentsShow
← Software Engineering
Lesson 1384 of 1,93341. Scalability and Distributed SystemsFree lesson

Caching to Reduce Load

Apply caching strategies at multiple layers to reduce database queries and improve response times under load.

Caching to Reduce Load

What you'll learn: How to strategically place caches at multiple layers to dramatically reduce database queries and speed up response times under heavy traffic.

The Core Problem

Databases are powerful but slow. Every query involves disk I/O, network latency, and computation. When your application serves thousands of requests per second, repeatedly fetching the same data from the database becomes a massive bottleneck. Your database struggles, response times spike, and users suffer.

What is Caching?

Caching stores frequently accessed data in fast, temporary storage (usually in-memory) so you can retrieve it without hitting the database. Think of it like keeping your frequently used kitchen utensils on the counter rather than walking to the basement storage every time you need them.

Multi-Layer Caching Strategy

Effective caching happens at multiple levels:

Client-side caching: Browsers cache static assets and API responses using HTTP headers. Users get instant page loads.

Application-layer caching: Your application stores computed results or query responses in memory (like Redis or Memcached). The database query that took 100ms now takes 1ms.

Database caching: The database itself caches query results and indexes. While helpful, this isn't under your direct control and doesn't prevent the database from receiving requests.

Cache Invalidation

The hardest part of caching is keeping data fresh. When underlying data changes, you must either invalidate the cache (force a refresh) or set appropriate expiration times (TTL - time-to-live). Stale data can confuse users, but over-invalidating defeats the purpose.

Common Caching Patterns

Cache-aside: Application checks cache first, fetches from database on miss, then stores in cache.

Write-through: Write to cache and database simultaneously, keeping them synchronized.

Time-based expiration: Cache entries automatically expire after a set duration, forcing fresh reads.

Key Takeaway: Caching at multiple layers—browser, application, and database—dramatically reduces load by serving frequently accessed data from fast memory instead of slow disk, but requires careful invalidation strategies to maintain data freshness.