· caching

Different Types of Read and Write Cache Patterns

The are different types of read and write cache patterns based on the behaviour of the cache. Following is a brief list of such patterns

Read Cache Patterns

Following are the different types of read cache patterns.

Read Through Cache

For a read request, if the data is not in the cache, the data will be read from the disk and written to the cache before returning data to the caller. The advantage of this pattern is consecutive read requests for the same data will always hit the cache. This cache pattern is best suited for scenarious where most frequently accessed items are kept in the cache.

Refresh Ahead Cache

In this caching scenario, the cache will asynchronosly refresh the cache values before they expire. The advantage of this cache pattern is, the cache will be always fresh and the reader will never experience delay in reading items in cache. To avoid constant refreshing of infrequently accessed data, the cache might keep track of the number of the read requests for each items

Write Cache Patterns

Following are the different types of write cache patterns.

Write Through Cache

When a write request comes, the data will be written to cache and disk before sending a success response. The advantage of this pattern is, successful writes will be always persisted to data store and read requests immedietely follows the write request will never result in a cache miss. The disadvantage of this cache pattern would be, increased response times for write operations

Write Behind Cache

When a write request comes, the data will be written to cache and success response will be returned. Then the cache will asynchronously write the data into the persistent data store. The advantage of this pattern is fast response times for write operations and read after write will never cause a cache miss. The disadvantage of this cache pattern is data in the cache is volatile and can be lost before it is written to persistent storage, if the caching layer fails.

Write Around Cache

When a write request comes, the data will be directly written to disk and success response will be returned. The advantage of this pattern is better response times for write operations than “Write Through” pattern and guaranteed data persistence. This cache pattern is also suited for scenarios, where only the most frequently accessed items are kept in the cache. A disadvantage of this pattern would be immediete read after a write will cause a cache miss.