What is Cache?

Cache is a mechanism used to temporarily store frequently accessed data in a fast storage layer, so future requests for that data are served faster. By reducing the load on the primary data source (e.g., database or web service), caching improves system performance and scalability, particularly for read-heavy applications.

cache
Benefits of Caching (Pros)

Drawbacks of Caching (Cons)

Cache Strategies

Cache Strategy Theory Pros Cons
Cache Aside Also known as Lazy Loading, the application checks the cache first. If data is not in the cache (cache miss), the application retrieves it from the database, places it in the cache, and then returns it to the user.
  • Simple to implement.
  • Only caches requested data, reducing memory usage.
  • Risk of stale data since database updates don't immediately update the cache.
  • Cache misses lead to slower initial responses.
Read-Through Cache The cache sits in front of the database. When a cache miss occurs, the cache fetches the data from the database and updates itself before returning the data to the application.
  • Application transparency, as read logic is handled by the cache.
  • Fewer cache misses because the cache automatically loads data when missing.
  • More complex to implement than Cache Aside.
  • Similar stale data issues if updates aren’t synchronized.
Write-Through Cache Data is written to both the cache and the database simultaneously. Every update first modifies the cache, which writes to the database immediately after.
  • Ensures consistency between cache and database.
  • No risk of stale data as updates happen in real-time.
  • Higher write latency, as both cache and database must be updated.
  • More complexity and overhead due to duplicate writes.
Write-Behind (Write-Back) Cache Data is first written to the cache, and the write to the database happens asynchronously (after a delay).
  • Improved write performance as the database doesn't need immediate updating.
  • Supports batch writes to the database, reducing total write operations.
  • Risk of data loss if the cache fails before the database is updated.
  • More complex logic to manage the asynchronous writes and queues.

Eviction in Cache

Eviction refers to the process of removing data from the cache when it reaches its capacity. When new data needs to be added to a full cache, the cache decides which existing data should be evicted based on the chosen eviction policy.

Eviction Policies
Policy Theory Pros Cons
Least Recently Used (LRU) LRU removes the least recently accessed data from the cache when new data is added. Effective for workloads where recently accessed data is more likely to be accessed again. May not perform well in cases where older data is more valuable than recently accessed data.
Least Frequently Used (LFU) LFU removes the data that is accessed the least frequently over time. Effective for workloads where some data is consistently more important than others, based on frequency of access. Can lead to stale data if frequently accessed data is no longer needed but remains in the cache.

What is a Distributed Cache?

A distributed cache is a cache that is spread across multiple servers or nodes, allowing data to be cached in a decentralized way. This type of cache is designed to scale horizontally, enabling high availability, fault tolerance, and improved performance across large systems.

Unlike a local cache that is stored on a single server, a distributed cache allows caching across multiple servers, which is crucial for applications serving millions of users or handling significant traffic. Distributed caching ensures data is accessible even when individual servers fail, making it ideal for cloud-native applications and large-scale systems.

lb

Why Use a Distributed Cache?


How to Design a Distributed Cache?

Designing a distributed cache involves several key considerations to ensure it is scalable, performant, and fault-tolerant. Below are the essential design steps:

1. Data Partitioning (Sharding)

To distribute data across multiple nodes, we partition (or shard) the data. A consistent hashing algorithm is often used to determine which node stores a specific piece of data. This ensures that data is distributed evenly across all nodes and can easily be retrieved.


2. Cache Replication

Replication ensures that multiple copies of the cached data are stored on different nodes. This improves fault tolerance and availability, as data can still be accessed if a node fails. There are two common replication methods:


3. Consistency Models

In a distributed cache, ensuring data consistency across nodes is critical. There are different consistency models that can be employed:


4. Cache Eviction Policies

In a distributed environment, cache size is still finite, so eviction policies decide which data should be removed when space is needed for new data. Common eviction policies include:


5. Handling Cache Invalidation

Cache invalidation ensures that stale data is removed or updated to maintain data integrity. In a distributed cache, this is more challenging due to the number of nodes involved. There are several strategies for handling cache invalidation:


6. Data Serialization

To efficiently store and transfer data across nodes in a distributed cache, it is serialized (converted to a format that can be transmitted or stored). Common serialization formats include:


7. Network Latency and Data Locality

In a distributed cache, minimizing network latency is essential to ensuring fast data retrieval. You can design the system to ensure that the cache nodes are geographically distributed and closer to users (data locality) to reduce latency.


Popular Distributed Cache Solutions in Cloud Environments

  1. Amazon ElastiCache (AWS)
  2. Azure Cache for Redis (Azure)
  3. Google Cloud Memorystore (GCP)
  4. Redis
  5. Memcached

Interview Questions and Answers

Q1: What are the differences between a distributed cache and a CDN (Content Delivery Network)?

A distributed cache and a CDN both aim to improve data access performance, but they serve different use cases:

Distributed cache:
CDN: