Cache Types
I'm currently reading this book about System Design Interviews. One of the suggested article was about cache types. Following are the cache types and their properties.
- Cache-aside:
-
how it works:
- we have application, database and cache
- application communicates directly with cache and with database
- first it asks the cache about data and if it's a miss, then it asks the database and updates the caches
- in case of a hit, application returns the data
- the writes are executed on a database directly
-
pros:
- great for read-heavy workloads
- data stored in cache can be different from the data in the database (we can fetch multiple data from the database, combine it and store the result in the cache under request ID)
- cache-failure resilient — if the cache goes down, we can still serve the data from the database
-
cons:
- not great for write-heavy workloads
- writes go to the database, so we can have inconsistency between database and cache, and it may require additional mechanisms like TTL
- Read-through:
-
how it works:
- cache sits between application and a database
- application communicates only with the cache
-
pros:
- good for read-heavy workloads
-
cons:
- data needs to be the same as in the database in oppose to the cache-aside approach
- first request for particular data always results in cache miss
- often techniques like warming or pre-heating is required (simulating requests to fill the cache)
- Write-through:
-
how it works:
- again cache sits between application and the database, but this time we write to cache and then the writes go immediately to the database
- not so useful on its own
-
pros:
- good for write-heavy workloads
-
cons:
- needs to be paired with for example read-through to be useful and get rid of inconsistencies
- Write-around:
-
how it works:
- data is written directly to the database and only read is cached
-
pros:
- good for cases where data is written only once and read frequently (should be paired with read-through)
-
cons:
- needs to be paired with other techniques to be useful
- Write-back:
-
how it works:
- cache sits between application and the database
- writes go to the cache and, after some delay, go to the database
-
pros:
- awesome for write-heavy workloads
-
cons:
- there is a risk of loosing data in case the cache goes down
Thanks for reading! Read other posts?