Caching Patterns : Write Around
Introduction
Unlike write-through caching, write-around caching bypasses the cache when writing data, going directly to the persistent storage. This can ensure that only the most read-frequent data is stored in the cache, optimizing its use for read operations.
Mechanics of the pattern
Write-around is a methodology applied to data writing processes. It can be effectively paired with lazy loading, which serves to populate the cache with data that has been recently accessed. The provided diagram illustrates how write-around is implemented during the data writing stage. Additionally, for read operations, lazy loading is integrated, serving to populate the cache.
Sequence diagram

Steps
Here is a breakdown of the steps involved in the process :
- The User initiates a write request through the Client Application.
- The Client Application writes the data directly to the DataStore, bypassing the Cache.
- The DataStore acknowledges the write operation back to the Client Application.
- The Client Application then confirms the write success to the User.
- When the User makes a read request, the Client Application first checks the Cache.
- If the data is not in the Cache (cache miss), the Client Application requests the data from the DataStore.
- The DataStore provides the data to the Client Application, which then stores the data in the Cache for future access.
- Finally, the Client Application returns the data to the User.
Implementation
Here’s a simple Node.js application that follows the write-around caching pattern. in-memory structures simlate the data store and the cache
const express = require('express');
const app = express();
app.use(express.json());
// Simulated Data Store and Cache
let dataStore = {};
let cache = {};
// Endpoint to write data directly to the data store (bypassing the cache)
app.post('/write', (req, res) => {
const { key, value } = req.body;
// Write directly to the data store
dataStore[key] = value;
// Do not write to cache - this is the essence of write-around caching
res.send('Data written to data store.');
});
// Endpoint to read data; if it's not in the cache, retrieve from the data store and then cache it
app.get('/read/:key', (req, res) => {
const key = req.params.key;
// Check if data is in cache
if (cache[key]) {
return res.json({ value: cache[key] });
}
// If not in cache, check the data store
const value = dataStore[key];
if (value !== undefined) {
// Add to cache for future access
cache[key] = value;
return res.json({ value });
}
res.status(404).send('Data not found.');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
Conclusion
The write-around caching pattern is an efficient data management strategy that is particularly useful in scenarios where it’s essential to preserve cache space for the most frequently accessed data and ensure that the cache is not overwhelmed by write operations that do not contribute to read performance.