Caching
Storing frequently accessed data in a temporary storage layer to reduce database queries and improve response times.
Detailed Explanation
Caching is one of the most effective performance optimization techniques. It works by storing the result of an expensive operation (database query, API call, computation) in fast storage (memory, SSD) and serving subsequent requests from the cache instead of recomputing.
Caching operates at multiple levels: browser caching (HTTP headers tell the browser to cache files), CDN caching (static content cached at edge servers), application caching (Redis/Memcached for computed results), and database caching (query result cache). Each level reduces latency and load at the level below it.
Why It Matters
Caching can improve performance by 10-100x and reduce database load by 90%+. It is the single most impactful performance optimization for most applications.
Real-World Example
A news website caches its homepage for 5 minutes. Instead of querying the database for the latest 50 articles on every request, it serves the cached version. Database load drops by 99%, and page load time drops from 2 seconds to 50 milliseconds.
When to Use
For any data that is read frequently but changes infrequently. Common targets: homepage content, user profiles, product catalogs, API responses, and computed results.
Advantages
- Dramatically reduces response times
- Reduces database load and costs
- Improves application scalability
- Multiple levels can be combined
- Relatively simple to implement
Disadvantages
- Cache invalidation is notoriously difficult
- Stale data can confuse users
- Adds complexity to the data flow
- Memory costs for large caches
- Cache stampedes can overwhelm databases
Related Terms
Frequently Asked Questions
What is cache invalidation?
Cache invalidation is determining when cached data is stale and should be refreshed. Strategies include TTL (time-to-live), event-based invalidation (invalidate when data changes), and write-through (update cache when writing to database).
What is the difference between cache and buffer?
A cache stores frequently accessed data for fast retrieval. A buffer temporarily stores data during transfer between two processes with different speeds. Caches improve read performance; buffers smooth out speed differences.
What is a cache stampede?
A cache stampede occurs when many requests simultaneously try to rebuild an expired cache entry, overwhelming the database. Solutions include locks/mutexes, early expiration, and stale-while-revalidate patterns.
What is Redis?
Redis is an in-memory data store used as a cache, database, and message broker. It supports data structures like strings, hashes, lists, sets, and sorted sets. It is the most popular caching solution for web applications.
Should I cache at the application or database level?
Application-level caching (Redis) gives you more control and flexibility. Database caching (query cache, materialized views) is simpler but less flexible. Most applications use application-level caching for maximum control.