Delving into Distributed Cache: A Comprehensive Guide

Praveen Kumar JV
3 min readNov 23, 2023

--

Introduction

In the fast-paced realm of web applications, performance is paramount. To address the performance bottlenecks that arise from frequent data access, distributed caching has emerged as a powerful solution. This article delves into the intricacies of distributed caching, exploring its concepts, implementation strategies, and functional requirements.

What is a Distributed Cache?

At its core, distributed caching is a caching mechanism that utilizes multiple cache servers, strategically dispersed across a distributed system. This approach contrasts with traditional single-server caching, offering enhanced scalability, fault tolerance, and performance.

Key Concepts in Distributed Cache

  1. Cache Hit: When a client requests data, the application server attempts to retrieve it from the cache. If the data is present, it’s a cache hit, resulting in faster response times.
  2. Cache Miss: If the requested data is not found in the cache, it’s a cache miss. In this scenario, the request is routed to the database, potentially introducing latency.
  3. Cache Invalidation: To maintain data consistency, stale cache entries need to be identified and removed. This process is known as cache invalidation.

Why Distributed Cache?

  1. Avoiding Single Points of Failure (SPOFs): Distributed caching eliminates the reliance on a single cache server, preventing performance degradation or data loss in case of server failures.
  2. Scalability: As data volumes and user traffic increase, distributed caching can seamlessly scale by adding more cache servers, ensuring optimal performance.
  3. Reduced Latency: By storing frequently accessed data in the cache, distributed caching reduces the load on databases, leading to faster response times and improved user experience.

Layers of Distributed Cache

  1. Web Cache: Accelerates the retrieval of static web content and manages user sessions.
  2. Application Cache: Provides local caching and key-value data storage within the application layer.
  3. Database Cache: Reduces data retrieval latency and I/O from the database, improving application performance.
  4. Knowledge Base Cache: Enhances query performance by caching frequently accessed knowledge base data.

Cache Writing Policies

  1. Write-Through Cache: Data is written to both the cache and database simultaneously, ensuring strong consistency but potentially introducing write latency.
  2. Write-Back Cache: Data is initially written to the cache, and updates to the database occur asynchronously. While offering lower latency, this approach may lead to stale data reads.
  3. Write-Around Cache: Data is written directly to the database, and cache updates are triggered on subsequent read requests. This policy prioritizes write latency but may result in cache misses.

Cache Eviction Policies

As cache memory is limited, eviction policies determine which data to retain and which to remove. Common eviction policies include:

  1. Least Recently Used (LRU): Evicts the least recently accessed data.
  2. Most Recently Used (MRU): Evicts the most recently accessed data, assuming it’s more likely to be accessed again soon.
  3. Least Frequently Used (LFU): Evicts the data with the lowest frequency of access.
  4. Most Frequently Used (MFU): Evicts the data with the highest frequency of access, assuming it’s less likely to be accessed again soon.

Cache Invalidation Approaches

  1. Active Expiration: Continuously monitors the Time-to-Live (TTL) of cache entries and evicts those that have expired.
  2. Passive Expiration: Checks the TTL of a cache entry only when it’s accessed, reducing computational overhead but potentially leading to stale data reads.

Storage Mechanisms in Distributed Cache

  1. Hashing Functions: Used to determine the cache server responsible for storing a particular data item. Consistent hashing ensures even data distribution across servers.
  2. Data Structures: Linked lists are commonly used for their efficiency in adding, removing, and accessing data.
  3. Bloom Filters: Probabilistic data structures that efficiently determine if a data item exists in the cache, reducing the need for full data scans.

Data Sharding in Distributed Cache

  1. Dedicated Cache Servers: Separates cache servers from application and web servers, allowing independent scaling of each component.
  2. Colocated Cache: Embeds cache functionality within the application or web servers, simplifying deployment but limiting scalability.

Cache Client

Resides in application servers to determine the appropriate cache server for data storage and retrieval. It ensures consistent results regardless of client interactions.

Exploring Distributed Cache System Design

In the next article, we will delve into the intricacies of designing a distributed cache system, exploring strategies for achieving high availability, scalability, and performance. We will discuss various design considerations, including cache invalidation mechanisms, data partitioning, and cache consistency models. Join us as we embark on this journey to design a robust and efficient distributed cache system.

--

--