Skip to main content

Command Palette

Search for a command to run...

Cache Writing Policies

Updated
2 min readView as Markdown

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.

image.png

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

  • High write latency as database needs to be updated on each write request.
  • Database has updated data.
  • Low read latency since cache has updated data.
  • Is fault tolerant, if the cache goes down the updates are still present in the database.

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.

image.png

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.

image.png

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 tolerant. Since the data is written to database.

More from this blog

Coding with a Glass of Milk

17 posts