Cache Writing policies

There are three types of Cache writing policy that we're going to discuss today:
  • Write Through Caching
  • Write Back Caching
  • Write Around Caching

Write Through Caching

In this type of caching the data is written to cache first and then the database.



How it exactly works:
  • Data request comes to cache.
  • Cache updates its data.
  • Database updates its data.
  • Request is completed once both database and cache are updated.

Performance

Write Latency = Cache Update Time + Database Update Time
Read Latency = Cache Read Time

Properties
  • Data request comes to cache.
  • Cache updates its data.
  • Database updates its data.
  • Request is completed once both database and cache are updated.

Write Back Caching

In this type of caching the data is written to cache only and the request is completed. The updates are written back to the database asynchronously.



How it exactly works:
  • Data request comes to cache.
  • Cache updates its data.
  • Request is completed.
  • Database is updated asynchronously.

Performance


Write Latency = Cache Update Time
Read Latency = Cache Read Time

Properties
  • Low write latency as only cache needs to be updated.
  • Database can have stale data.
  • Low read latency since cache has updated data.
  • Takes a hit on fault tolerance. If the cache goes down then the updates which are not yet written to database are lost.

Write Around Caching

In this type of caching the data is written to the database and the request is completed. The cache key is deleted so the value is refereshed during the next read.



How it exactly works:
  • Data request comes to cache.
  • Database updates its data.
  • The cache key is deleted.
  • Request is completed.

Performance

Write Latency = Database Update Time
Read Latency = Cache Read Time
Read Latency = Cache Update Time + Database Read Time (Cache Miss)

Properties
  • Low write latency as database needs to be updated on each write request.
  • Database is always up to date.
  • High read latency since cache miss can happen if the key is only deleted.
  • Fault tolerance. Since the data is written to database.

Comments