Distributed Caching Introduction
What is Distributed Cache?
In high-concurrency distributed systems, caching is an essential component. Without caching to accelerate the system and prevent a large volume of requests from directly hitting the underlying infrastructure, it becomes extremely difficult for the system to withstand high concurrency shocks; therefore, designing cache mechanisms in distributed systems is a critical aspect.
Distributed Cache Solutions
- Redis
- Memcache
Problems Caused by Distributed Caching
Distributed caching introduces three major issues: Cache Penetration, Cache Breakdown, and Cache Avalanche.
Cache Penetration
Definition: Refers to the scenario where data does not exist in both the cache and the database. This leads to high query pressure, as every request results in a non-existent lookup, rendering caching ineffective for such cases.
- Root Cause: Data absence (the key is missing from storage).
- Solutions:
- Bloom Filters: Utilize bloom filters to determine data existence; if the filter indicates the data does not exist, return immediately without querying the database or cache.
- Interface Validation: Implement checks at the application layer before attempting a lookup.
Cache Breakdown (Cache Hit)
Definition: Refers to the scenario where data exists in the database but has expired from the cache. When users attempt to retrieve this data, they miss it in the cache and query the database directly, causing an instantaneous spike in database load and potentially overwhelming it due to excessive pressure.
- Root Cause: Data is absent in the cache (due to expiration) but present in the database.
- Solutions:
- Set hot data with a permanent expiry time (never expire).
Cache Avalanche
Definition: Refers to the scenario where large batches of cached data expire simultaneously, combined with an excessive volume of queries, resulting in overwhelming pressure on the backend database.
- Root Cause: Mass expiration of cache entries at nearly the same moment.
- Solutions:
- Set random expiry times for cache data to prevent a synchronized mass-expiration event.
- Configure hot data with permanent expiry settings (never expire).
Common Cache Eviction Strategies
- FIFO (First In, First Out)
- LFU (Least Frequently Used)
- LRU (Least Recently Used)