Introduction

Efficient write operations are crucial for maintaining data integrity and performance in applications. Write-through caching is a technique where write operations are simultaneously written to the cache and the backing store. This ensures consistency between the cache and the persistent storage.

Mechanics of the pattern

Sequence diagram

Lazy loading Sequence diagram

Steps

Here is a breakdown of the steps involved in write through :

  1. Write Request: A client sends a write request to the cache layer, requesting that new or updated data be stored.
  2. Writing to Cache: Upon receiving the write request, the cache layer immediately writes the data to the cache.
  3. Write to Backing Store: Simultaneously or immediately after writing to the cache, the cache layer forwards the write request to the backing data store to maintain data consistency between the cache and the store.
  4. Data Store Acknowledgment: The data store, upon successfully writing the data, sends an acknowledgment back to the cache layer confirming that the write operation has been completed successfully.
  5. Cache Acknowledgment to Client: After receiving the write acknowledgment from the data store, the cache layer then sends an acknowledgment to the client. This acknowledgment signifies that the write operation is fully complete, as the data is now stored both in the cache and the data store.

Implementation

In this example, the cache server abstracts the interaction with the data store and the cache from the client application. The client application only needs to send a request to the cache server to write the new data.

const axios = require('axios');

// Data to be written through the cache
const postData = {
  id: '123',
  value: 'Example data to cache and store'
};

// Function to send a POST request to the cache server
function writeToCache(data) {
  axios.post('http://localhost:3000/cache', data)
    .then(response => {
      console.log('Response from cache server:', response.data);
    })
    .catch(error => {
      console.error('Error communicating with cache server:', error.message);
    });
}

// Use the function to write data through the cache
writeToCache(postData);

Caching servers that support the pattern

Some caching servers support out of the box the write through pattern. This is the case for DAX:

  • Amazon DynamoDB Accelerator (DAX) is an in-memory caching service for DynamoDB
  • Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS)

The write through pattern is supported as shown in the following diagram : Write Through DAX DynamoDB

Adding a caching layer

Elasticache for Redis for instance is an in memory data store service provided by AWS. However it does not support write through out of the box. A way to implement this pattern in this case would be to add a caching layer between the client application from one side and the elasticache service and the data store from the other side. Let’s take the example of RDS, a relational database service provided by AWS to represent this pattern :

Write Through Elasticache RDS

In this case, the client application only interacts with the caching layer. This caching layer abstracts the communication with RDS and Elasticache from the client. When a write request is sent to the caching layer, it sends a write request to the elasticache for Redis and to the RDS data store. Upon acknowledgment from both it sends an acknowledgment to the client.

Conclusion

In summary, the write-through caching pattern is a robust strategy for data management that ensures consistency and reliability. By writing data simultaneously to the cache and the underlying storage, it provides a safeguard against data loss and ensures that read operations always retrieve the most current data. This pattern is particularly beneficial for applications where data integrity is paramount.

Updated: