Caching
There are a few ways to update our cache:
Cache eviction
- LRUCache can be used for cache eviction
Cache staling
- When no one used the cache and there is no eviction happens, cache can be stale.
- To deal with it we can set a TTL and then try to expire the item
- We can:
- Passively expire an item — when an item got accessed it checks for TTL to expire
- Actively expire item — have a thread to go through and query the expiration cache
Security
- Networking/Firewall
- Encryption
Caching strategies
Read
Cache Aside:
- Application will handle the cache miss, when there is a cache miss, application will read the db and update the data itself
- Pros:
- Fine-gain control of how to update the cache
- Cons:
- Need to handle cache eviction and expiration
Read Through:
- The cache itself will handle the cache miss, if there is a cache miss, the cache will fetch the db and update itself ==without going through application==.
- Pros:
- Simplicity
- Cons:
- Less control over the implementation
Write
- Write through: write into both database in the same time
- Pros:
- high resilient
- less cache miss
- no data stale
- Cons:
- double the storage
- Pros:
- Write around: write into database first and then the cache can update later (either through cache miss or something)
- Pros:
- no data stale
- less memory storage
- Cons:
- Higher chance of cache miss
- Pros:
- Write back: write to the cache first and let the cache asynchronously update our database
- Pros:
- less cache miss
- Cons:
- Single point of failure (cache)
- DB might have stale data
- Pros: