Discover more from System Design Newsletter
Get the powerful template to approach system design for FREE on newsletter sign-up:
This post outlines the most common caching patterns. If you want to learn more, scroll to the bottom and find the references.
Consider sharing this post with someone who wants to study system design.
A cache is a temporary storage that provides quick access to frequently used data. And it’s usually stored in memory for low latency. But available memory is limited. So it’s important to update the cache the right way.
I will teach you the popular caching patterns in this post.
A cache hit occurs when queried data is available in the cache. And a cache miss occurs when queried data isn’t available in the cache.
Caching Patterns
5 popular caching patterns are:
1. Cache Aside
Here is the workflow:
Read data from the cache
Read data from the database on a cache miss
Write data to the cache
And return data
It’s also called lazy loading. Because only the queried data gets cached.
Also the cache doesn't interact with the database. But the app does.
Advantages
Easy implementation
Finer control over cache population. Because only frequently accessed data is cached
Disadvantages
Degraded latency. Because there are many trips
Potential data inconsistency
Use Cases
General purpose caching
Read-heavy workloads
2. Write Through
Here is the workflow:
Write data to the cache
And the cache writes data synchronously to the database
The app doesn't interact with the database. But the cache does.
Advantages
Data consistency
Low read latency. Because the cache contains fresh data
Disadvantages
High write latency. Because both cache and database need to be updated
Most data in the cache is never read
Use Cases
A low number of writes expected
Data freshness is important
3. Read Through
Here is the workflow:
Read data from the cache
Read data from the database on a cache miss
Write data to the cache
And return data
The app doesn't interact with the database. But the cache does.
And this is what makes it different from the cache aside pattern.
Advantages
Low read latency for frequently accessed data
Improved read scalability
Disadvantages
Potential data inconsistency. Because the cache might contain stale data
High latency on a cache miss
Use Cases
Read-heavy workloads
A high cache miss rate is acceptable
4. Write Back
Here is the workflow:
Write data to the cache
And the cache writes data asynchronously to the database
It’s also called the write-behind pattern.
Advantages
Low write latency
Improved performance. Because writes to the database get batched
Disadvantages
Increased risk of data loss. Because the cache might fail before writing to the database
Complex implementation
Use Cases
Write-heavy workloads
Data durability is not important
5. Write Around
Here is the workflow:
Write data to the database
Read data from the cache
Read data from the database on a cache miss
And write data to the cache
Advantages
Reduced risk of data loss
Reduced cache pollution. Because cache stores only frequently accessed data
Disadvantages
High read latency
High cache miss rate
Use Cases
No data update expected
A low number of reads expected
Redis and Memcached are popular cache implementations. And a combination of caching patterns is used in real-world systems.
Consider subscribing to get simplified case studies delivered straight to your inbox:
Thank you for supporting this newsletter. Consider sharing this post with your friends and get rewards. Y’all are the best.
And don’t forget to hit the Like button if you enjoyed this post ❤️
References
https://www.prisma.io/dataguide/managing-databases/introduction-database-caching
https://www.slideshare.net/tmatyashovsky/from-cache-to-in-memory-data-grid-introduction-to-hazelcast
https://www.alachisoft.com/resources/articles/readthru-writethru-writebehind.html
This is a great idea for an article but unfortunately the descriptions are not easy to read. For example, the workflow for Read-through is WORD FOR WORD identical to Cache Aside. The words are insufficient to explain what's going on, so is the diagram. Readers have to read both the words and the diagram to stitch together hat's going on. I would encourage rewriting these to explain in more detail.
Interesting! I've worked only with the Cache aside implementation, as it's the easier to maintain.