# Caching Conundrum: Is There Truly Just One Path to API Efficiency?

Caching involves storing duplicates of frequently accessed data at various points along the request-response path.

When a consumer seeks a resource like a YouTube video, the request traverses one or more caches (local cache, proxy cache, or reverse proxy) towards the service hosting the resource.

If any cache along the path has a recent copy, it fulfills the request; otherwise, the request proceeds to the origin server.

This process defines two key states:

* ***Cache Hit***: Data requested is found in the cache memory, accelerating delivery to the processor.
    
* ***Cache Miss***: The requested data is absent in the cache, causing delays as the program fetches it from higher cache levels or the main memory.
    

**But is there a single way to implement caching?**

---

## **Cache-Aside Pattern (Lazy Loading)**

The cache-aside pattern is one of the most commonly used caching approaches.

In this pattern, the application:

* Initially, it requests data from the cache.
    
* If the data is not in the cache, the application fetches it from the primary data store and updates the cache before providing the data back to the caller.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744702444917/a12a16d4-0f2a-4169-9166-fef5f895e18d.png align="center")

Here's an example using Redis in Python:

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80841769-ba3c-4fcc-847b-2e72be733c13_1480x894.png align="center")

#### **Advantages**

* Easy implementation
    
* Low read latency for frequently accessed data
    

#### **Disadvantages**

* High read latency on a cache miss
    
* Potential data inconsistency: Data in cache is not updated
    

#### **Use Cases**

* Read-heavy workloads
    
* Data inconsistency is acceptable
    

---

## **Read-Through Pattern**

In the read-through pattern, the cache retrieves data from the primary data store in case of a cache miss.

In this approach, the client only interacts with the cache, eliminating the need to explicitly handle cache misses.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744702691494/8fc160fe-beb6-443a-944e-026db51ebf7d.png align="center")

To implement the read-through pattern using Redis, you can use a custom cache implementation that fetches the data from the primary data store when necessary:

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d77d64c-e4c1-4982-85ec-873eb9e2439a_1160x1080.png align="center")

#### **Advantages**

* Easy use: only interact with cache
    
* Low read latency for frequently accessed data
    

#### **Disadvantages**

* High read latency on a cache miss
    
* Potential data inconsistency: Data in the cache is not updated
    

#### **Use Cases**

* Read-heavy workloads
    
* Data inconsistency is acceptable
    

---

## **Write-Through Pattern**

With this pattern, the application writes data to both the cache and the primary data store, ensuring that the cache consistently reflects the most recent data.

This pattern minimizes the likelihood of stale data within the cache.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744702753843/8dc1bb46-db1b-46cd-afc9-5ceaf5aca222.png align="center")

Here's an example of how you can implement the write-through pattern with Redis in Python:

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4bdce39d-cc70-4b39-aa19-9deaade979a4_1058x744.png align="center")

#### **Advantages**

* Data consistency: cache always contains fresh data
    
* Low read latency
    

#### **Disadvantages**

* Most data in the cache is never read
    
* High write latency: both cache and database need to be updated
    

#### **Use Cases**

* A low number of writes expected
    
* Data freshness is important
    

---

## **Write-Behind Pattern**

The write-behind pattern is an optimization of the write-through pattern.

In this pattern, the application writes data to the cache and asynchronously updates the primary data store.

This improves the application's write performance, as it does not need to wait for the primary data store to acknowledge the write operation.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744702801855/eabcbe85-bdd6-46b3-aca1-c4f22c200922.png align="center")

To implement the write-behind pattern, you can use a message queue or a background worker to handle the asynchronous updates:

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d92316c-eff5-441f-a6ed-94201950af1b_1312x1004.png align="center")

#### **Advantages**

* Easy use: only interact with cache
    
* Low write latency: cache updated asynchronously
    

#### **Disadvantages**

* Complex implementation
    
* Increased risk of data loss: cache might fail before writing to the database
    

#### **Use Cases**

* Write-heavy workloads
    
* Loss of data is not critical
    

---

## **Write-Around Pattern**

In this pattern, when the user requests to store the data, the application:

* First, it writes data to the primary data store.
    
* Then when data is requested, the application initially checks the cache.
    
* In case of a cache miss, the application fetches it from the primary data store and updates the cache before providing the data back to the caller.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1744702851807/8933486a-7f85-4fc7-ba53-40d52311f2f4.png align="center")

Here's an example of how you can implement the write-around pattern with Redis in Python:

![](https://substackcdn.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccc25270-8eee-45ce-bfc6-ccaa6f1ea111_1110x968.png align="left")

#### **Advantages**

* Reduced risk of data loss
    
* Reduced cache pollution: cache only stores frequently accessed data
    

#### **Disadvantages**

* High read latency on cache miss
    
* Potential data inconsistency: Data in cache is not updated
    

#### **Use Cases**

* (Re)Read- and Write-heavy workloads
    
* A high cache miss rate is acceptable
    

***<mark>In production environments, data storage and retrieval techniques vary according to the application requirements, and often the combination of two or more is used.</mark>***
