3 Strategies on Writing to Cache.

3 Strategies on Writing to Cache.

Talking about reading from Cache is common and most people are talking of it. Writing to Cache is the least talked about subject and one of the most important operations when dealing with Cache. Below are three strategies for writing to Cache.

  1. Write-through.

    In this strategy, data gets written to the cache and, at the same time, stores the data. This ensures immediate consistency between the cache and storage. This strategy should be used in scenarios when immediate data consistency is critical and a simple design is required.

    How it works:

    • An application writes data.

    • The cache writes to itself and sends the write request to the data store.

    • The data store acknowledges the writer.

    • The Cache forwards the acknowledgment to the application.

The downside for this is that the performance gets a hit due to synchronous write high latency for write operations.

  1. Write-behind.

    This strategy writes data to the Cache first and then writes to the underlying data store asynchronously. It improves the performance by decoupling the Cache and data store write operations. It is good for high-throughput, low-latency writes.

    How it works:

    • The application writes data to the Cache.

    • The Cache asynchronously updates the data store.

The downside to this is a risk of data loss due to the Cache failing before the async write completes which might cause inconsistency.

  1. Write-Around.

    The application writes directly to the data store in write write-around caching, bypassing the Cache directly. This is ideal for write-once, read-rarely scenarios, avoiding cache pollution. It is ideal for write-heavy workloads and avoids unnecessary Cache invalidation. Initial reads of the written data are slow because they load from the data store. Increasing read miss-rates.

    How it works:

    • The application writes data.

    • Write directly occurs to the data store, bypassing the Cache.

That is it for now. I appreciate your time and please leave me a comment.

Partial credits to @RaulJuncoV on twitter.