0
0
HLDsystem_design~3 mins

Why Write-through and write-back caching in HLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a simple shelf can transform your data speed and safety!

The Scenario

Imagine you have a huge library of books, and every time someone wants a book, you run to the main storage room to get it. If you want to update a book's information, you must go all the way to the storage room to change it. This takes a lot of time and effort.

The Problem

Going back and forth to the storage room for every single request is slow and tiring. If many people want books or updates at the same time, it causes long waits and mistakes can happen if updates are missed or delayed.

The Solution

Write-through and write-back caching act like a small, quick-access shelf near you. Write-through means every update is immediately sent to the main storage, keeping things safe but a bit slower. Write-back means updates stay on the small shelf for a while and are sent later, making things faster but needing careful tracking.

Before vs After
Before
updateMainStorage(bookId, newInfo)
getFromMainStorage(bookId)
After
cache.write(bookId, newInfo)  # write-back or write-through
cache.read(bookId)
What It Enables

It enables fast access and updates to data while balancing speed and safety, making systems efficient and reliable.

Real Life Example

When you edit a document on your computer, sometimes changes save instantly to the cloud (write-through), or they save after a few moments when you're offline (write-back), ensuring your work is safe and quick to access.

Key Takeaways

Manual data access is slow and error-prone.

Write-through caching updates main storage immediately for safety.

Write-back caching delays updates for speed but needs careful management.