Caching is a core concept in modern system design. In most systems, databases store large volumes of data on disk, which makes complex queries and frequent access relatively slow. A cache mitigates this problem by keeping frequently accessed data in high-speed memory.
When requested data is found in the cache (cache hit), responses are returned almost instantly. When it is not (cache miss), the system must fall back to the primary database, incurring higher latency.
โก Why Use an External Cache? #
Although most databases maintain internal memory buffers, an external cache often provides better control, performance, and scalability. Common scenarios include:
-
Result Caching
Store the output of expensive computations or aggregation queries. -
Temporal Hotspots
Cache data that is heavily accessed for a short time window (for example, newly created content). -
Write Buffering
Absorb bursts of write traffic when immediate durability is not required.
Most cache systems are implemented as in-memory databases. Because data lives primarily in RAM and may not be persisted to disk, cached data is considered volatile and can be evicted or lost at any time.
โ๏ธ Write Strategies #
Write strategy determines how data flows between the application, cache, and database, directly affecting consistency and latency.
Write-Through #
The application writes data to the database first. Once the write succeeds, the same data is written to the cache before returning success.
- Pros: Strong consistency; cache always reflects database state
- Cons: Higher write latency due to double writes
Write-Back (Write-Behind) #
The application writes data to the cache and returns success immediately. The database is updated asynchronously.
- Pros: Very low write latency and high throughput
- Cons: Risk of data loss if the cache fails before persistence
Write-Around #
Writes bypass the cache and go directly to the database. The cache is populated only on read misses.
- Pros: Avoids cache pollution from rarely read data
- Cons: First read is always a cache miss
๐งน Eviction Strategies #
Because cache memory is limited, eviction policies determine which data is removed when space is needed.
Time-to-Live (TTL) #
Each entry has an expiration time. Once it expires, it is automatically removed.
- Ideal for time-sensitive or rapidly changing data
Least Recently Used (LRU) #
Entries that have not been accessed recently are evicted first.
- Effective when recent access predicts future access
Least Frequently Used (LFU) #
Entries with the lowest access count are removed.
- Useful when access frequency matters more than recency
๐ Scalability and Reliability #
Production-grade cache systems go far beyond simple key-value storage and typically support:
-
Sharding
Distributing data across multiple nodes to increase capacity and throughput. -
Replication
Maintaining multiple copies of data to improve availability and fault tolerance.
These techniques allow caches to scale horizontally while remaining resilient to node failures.
โ Summary #
The purpose of caching is not to store all data, but to maximize the cache hit rate. By carefully choosing write strategies, eviction policies, and scaling mechanisms, a well-designed cache can dramatically reduce latency, protect the primary database, and improve overall system performance.