Different types of caching strategies
Caching strategies are essential for improving performance and reducing latency in systems. They can be categorized based on the level of caching (L1, L2, etc.) and how data is managed and accessed.
Caching Levels
1. L1 Cache (In-Process Cache)
Location: Directly within the application or process.
Usage: Caches data in memory (RAM) of the running application.
Performance: Extremely fast as it avoids network overhead and resides in the same memory space as the application.
Scope: Limited to a single application instance or process.
Example:
Java: Use of in-memory caching frameworks like
Ehcache
orCaffeine
.Python: Use of
LRU (Least Recently Used)
caches via libraries likefunctools.lru_cache
.
Use Case:
Frequently accessed configuration data or session-level data.
API response caching in a microservice.
Limitations:
Not shared across multiple instances of the application.
Can cause cache inconsistency if multiple instances are running.
2. L2 Cache (Distributed Cache)
Location: Shared across multiple application instances or processes, typically implemented using external systems.
Usage: Caches data in a distributed caching system like Redis, Memcached, or Hazelcast.
Performance: Slightly slower than L1 due to network access, but still much faster than querying the primary database.
Scope: Global across the application cluster or multiple services.
Example:
Using Redis to store session data or common lookups shared across services.
Caching aggregated results for analytics in a distributed application.
Use Case:
Scenarios where multiple instances need access to the same cached data.
Load balancing across microservices that share state via a common cache.
Limitations:
Requires network overhead.
Needs proper cache invalidation strategies to avoid serving stale data.
Caching Strategies
1. Write-Through Cache
Definition: Data is written to the cache and the underlying database simultaneously.
Advantages:
Ensures data consistency between the cache and database.
Cached data is always up-to-date.
Disadvantages:
Write operations are slower because they need to update both cache and database.
Use Case: Scenarios requiring high consistency, such as e-commerce inventory updates.
2. Write-Back Cache (Lazy Write)
Definition: Data is written to the cache first and written back to the database asynchronously.
Advantages:
Faster writes since updates are deferred to the database.
Reduces database write load.
Disadvantages:
Risk of data loss if the cache fails before writing to the database.
Use Case: Use cases where eventual consistency is acceptable, such as logging systems.
3. Read-Through Cache
Definition: The application retrieves data directly from the cache. If the data is not available (cache miss), the cache fetches it from the database and then returns it.
Advantages:
Simplifies application logic by abstracting cache management.
Ensures the cache is automatically populated on misses.
Disadvantages:
Cache misses can lead to slightly higher latencies.
Use Case: Applications with frequent reads, such as user profile systems.
4. Cache Aside (Lazy Loading)
Definition: The application checks the cache first. If the data is not available (cache miss), it fetches data from the database, updates the cache, and then returns the data.
Advantages:
Gives applications complete control over the cache.
Optimized for read-heavy workloads with sporadic writes.
Disadvantages:
Cache misses require manual data loading.
Use Case: When the cache needs to hold only frequently accessed or hot data.
5. Write-Around Cache
Definition: Data is written directly to the database, skipping the cache. Reads still go through the cache, so cached data must be invalidated or repopulated when required.
Advantages:
Reduces cache write load, making it suitable for infrequent reads.
Disadvantages:
Can lead to more cache misses.
Use Case: Scenarios where most written data is not frequently read.
6. Global Distributed Cache
Definition: A cache shared across multiple regions or data centers to serve users globally.
Advantages:
Reduces latency for geographically dispersed users.
Ensures global consistency if implemented properly.
Disadvantages:
Complexity in maintaining consistency across regions.
Use Case: Applications with a global audience, like video streaming or social media platforms.
Eviction Policies
To handle limited cache size, various eviction policies are used:
LRU (Least Recently Used): Evicts the least recently accessed item.
LFU (Least Frequently Used): Evicts the least frequently accessed item.
FIFO (First In, First Out): Evicts the oldest item in the cache.
Random Eviction: Removes a random entry to make space.
TTL (Time-to-Live): Items expire after a specified duration.
Choosing a Cache Strategy
Read-Heavy Systems: Use Read-Through or Cache Aside with an L1 and L2 cache.
Write-Heavy Systems: Use Write-Through or Write-Around with careful consistency mechanisms.
Globally Distributed Systems: Use Global Distributed Cache with replication and eventual consistency.
source:-wikipedia