13: Caching

Caching is a technique used in computing to temporarily store and manage copies of frequently accessed or computationally expensive data to speed up subsequent access and reduce the need to fetch or compute the same data repeatedly. The stored copies are referred to as "cache," and the process of storing and retrieving these copies is known as caching.
Key Concepts in Caching:
Cache:
- A storage area where copies of frequently accessed data are kept. This can be at various levels, such as a memory cache within a CPU, a web browser cache, or a distributed caching system in a network.
Cache Hit:
- Occurs when the requested data is found in the cache. It results in faster access times since the data doesn't need to be fetched from the source.
Cache Miss:
- Occurs when the requested data is not found in the cache. In this case, the system needs to retrieve the data from the source and store it in the cache for future use.
Eviction:
- The process of removing or replacing existing data from the cache to make room for new data. Various eviction policies determine which data is selected for removal.
Benefits of Caching:
Improved Performance:
- Caching reduces the time and resources required to access frequently used data, leading to faster response times and improved overall system performance.
Reduced Latency:
- By serving data from a cache closer to the requester, latency is minimized, especially in scenarios where the original data source is distant or has high access times.
Lower Load on Resources:
- Caching reduces the load on servers and databases by serving precomputed or stored results, minimizing the need for redundant computations or data retrievals.
Bandwidth Savings:
- Caching can reduce the need for repeated data transfer over networks, saving bandwidth and improving network efficiency.
Common Caching Scenarios:
Web Browser Caching:
- Browsers store copies of web pages, images, and other resources locally to speed up subsequent visits to the same website.
Content Delivery Networks (CDNs):
- CDNs cache and distribute content across geographically distributed servers, reducing the latency and load on origin servers.
Database Query Caching:
- Database systems can cache the results of frequently executed queries to reduce the computational load and speed up data retrieval.
In-Memory Caching:
- Some systems use an in-memory cache to store frequently accessed data directly in RAM, providing rapid access without the need for disk I/O.
Object Caching:
- In programming, caching can be used to store the results of expensive function calls or complex computations to avoid redundant calculations.
Caching Patterns / Strategies:
Caching patterns are strategies or approaches to implementing caching in software systems. Each pattern has its own set of advantages and disadvantages, and the choice of a caching pattern depends on the specific requirements and characteristics of the application. Here are some common caching patterns along with their pros and cons:
1. Cache-Aside (Lazy-Loading) Pattern:

Applications explicitly manage the cache. Data is fetched from the cache if available; otherwise, it is loaded from the data source and stored in the cache.
Pros:
Flexibility: Developers have control over what is cached and when.
Reduced Overhead: Only requested data is cached.
Cons:
Complexity: Developers need to manage cache logic explicitly.
Stale Data: Possibility of serving stale data if not properly managed.
2. Write-Through and Write-Behind Caching Pattern:
Write-Through: Data is written to both the cache and the underlying data store simultaneously.

Write-Behind: Data is initially written to the cache, and the write to the underlying data store is deferred.

Pros:
Consistency: Ensures that data in the cache and data store are consistent.
Reduced Latency: Write-Behind can improve write performance by batching writes.
Cons:
Increased Write Latency: Write-Through may introduce higher write latency due to dual writes.
Complexity: Write-Behind introduces complexity in handling delayed writes.
3. Read-Through and Refresh-Ahead Caching Pattern:
Read-Through: If data is not in the cache, it is loaded from the data store and stored in the cache.

Refresh-Ahead: Proactively refreshes or preloads data into the cache before it expires.
Pros:
Read Efficiency: Improves read efficiency by loading data into the cache on demand.
Proactive Refresh: Refresh-Ahead reduces the likelihood of serving stale data.
Cons:
Increased Latency: Read-Through introduces latency for cache misses.
Resource Intensive: Refresh-Ahead may consume resources when preloading large datasets.
4. Cache Invalidation Pattern:
Invalidation: Cache entries are explicitly invalidated (removed) when the corresponding data in the data store changes.
Pros:
Consistency: Ensures that only valid data is served.
Resource Efficiency: Reduces the risk of serving stale data.
Cons:
Complexity: Requires explicit mechanisms for tracking and managing invalidations.
Increased Write Overhead: Additional operations for invalidating cache entries.
5. Local Caching and Distributed Caching Pattern:
Local Caching: Each instance of an application maintains its local cache.
Distributed Caching: Caches are shared across multiple instances of an application, providing a global cache.
Pros:
Local Caching: Low-latency access to frequently used data.
Distributed Caching: Improved scalability and global data consistency.
Cons:
Local Caching: Limited to the cache of a specific instance.
Distributed Caching: Potential network overhead and complexity.
6. Near Cache (Side Cache) Pattern:
A cache is placed near the data source, reducing the need to traverse the network for frequently accessed data.
Pros:
Low Latency: Provides low-latency access to data located nearby.
Reduced Network Overhead: Reduces the need to fetch data over the network.
Cons:
Limited Distribution: This may not be suitable for widely distributed systems.
Increased Memory Usage: Near caches may duplicate data already stored in a central cache.
7. Time-To-Live (TTL) Caching Pattern:
Assigns a time limit to cached data, and entries expire after a predefined period.
Pros:
Resource Management: Ensures that outdated data is eventually removed, freeing up resources.
Simplified Cache Management: Automatic eviction of stale data based on TTL.
Cons:
Potential Stale Data: Data may be served until its TTL expires.
Increased Eviction Overhead: Frequent cache evictions may introduce overhead.
Considerations:
Cache Invalidation:
- Ensuring that cached data remains accurate by updating or invalidating the cache when the original data changes.
Cache Size and Replacement Policies:
- Determining the size of the cache and the strategy for selecting data to be evicted when the cache is full.
Consistency and Freshness:
- Balancing the trade-off between serving stale data from the cache and ensuring that the data is always up-to-date.
Caching is a fundamental optimization technique employed in various computing systems to achieve faster response times, lower latency, and more efficient use of resources.


