Caching

Pasted image 20230902110723.png

There are a few ways to update our cache:

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

Caching strategies

Pasted image 20230906130004.png

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
  • 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
  • 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