Cache Writing Policies

Imagine you’re running a large e-commerce platform where millions of users are constantly checking product prices and inventory. Every time a user requests information, your system needs to fetch it from the main database. Database queries are slow, and with millions of requests, your system becomes frustratingly sluggish. This is where caching comes in. By storing frequently accessed data in a faster, in-memory storage (cache), you can serve requests much faster. Instead of hitting the database every time, your system first checks the cache. If the information is not found in the cache, then it queries the database. While reading from a cache is straightforward, managing data updates in a cache can be challenging. This is where cache writing policies become important.

Let's start with the simplest writing policy: Write-Through. In this approach, when data needs to be updated, we write it to both the cache and the main database simultaneously. Only when both writes are complete does the system confirm the update. This policy ensures that the cache and database stay perfectly synchronized, making it ideal for systems where data consistency is critical, like financial applications or inventory management. But Write-Through has a significant drawback. Every write operation must wait for both the cache and database updates to complete, which can make write operations slower.

To address the performance limitation of Write-Through, some systems use Write-Back (also called Write-Behind). With Write-Back, data is initially written only to the cache, and the system immediately confirms the update. The modified data is written to the database later, usually in batches. This makes write operations much faster since they don't have to wait for the slower database write. Write-Back is perfect for systems that need high write performance, like real-time analytics or gaming applications. But there's a risk. If the system crashes before cached data is written to the database, you could lose updates.

Write-Around is another policy that takes a different approach. When updating data, Write-Around bypasses the cache completely and writes directly to the database. This policy works well when the updated data isn't likely to be read again soon, like log entries. The downside is that if the same data is needed again quickly, it must be fetched from the database, causing a cache miss. But once we hit the database, we’ll store this data in the cache so that we don’t have another cache miss.