Why would you care about the implementation of a read-through vs a write-through cache?
Choosing between read-vs-write can be an important optimization where speed is critical.
How does a read-through cache work? Can you walk through the process?
A read-through cache is populated on-demand.
Basically when a request is made, the cache would:
- Check for the requested item
- If missed, it would query primary storage, store in the cache, and serve
- If hit, it would serve directly
What are the pros of a read-through cache?
It’s transparent (since the application only interacts with the cache), highly performant on hits (served directly from the cache), and reduces primary storage pressure (frequent reads don’t hit primary storage).
What’s the single con of a read-through cache?
Misses result in a latency hit as the data must be read from primary storage.
How does a write-through cache work? Can you walk through the process?
In a write-through cache, all write operations are written to the cache AND primary storage.
When a write request is made:
- Data is written to the cache
- Data is written to the primary storage
- All reads are made from the cache
What are the three primary pros of using write-through cache?
A write-through cache provides:
- Consistency guarantees as the cache and primary storage are always in sync
- No data is lost during crashes as both stores have the same data
- Simplfied writes as it’s written to the cache and primary storage
What are the two cons of a write-through cache?
Writes can have higher latency as they must be written to two places instead of one, and since every write hits primary storage it can increase pressure on it.
What type of systems would favor a read-through cache?
Most read-heavy systems would prefer a read-through, especially if data isn’t frequently updated.
What systems would prefer a write-through cache?
Systems that favor consistency or data integrity would favor a write-through cache, even at the cost of increased write latency.
What are two good reasons to use a read-through cache?
Ready-heavy with infrequent writes and eventually consistent with reads.
What are two good reason to use a write-through cache?
Systems where data consistency is paramount and updates must be immediately propagated.