×

Search anything:

Write through and write back

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Introduction

Write-through and write-back are two working methods of buffer memory. Whenever the processor wants to write a word, it checks to see if the address where it wants to write the data exists in the cache. If the address exists in the cache, it is a write hit. We can update the value in the cache and avoid expensive main memory accesses. But this will lead to inconsistent data (Inconsistent Data) problem. Since both cache and main memory contain different data, as in a multiprocessor system, this can cause problems for two or more devices that share main memory. This is where Write Through and Write Back come in.

Concept of write through

We can simply assume that write through is that when we implement write operations like insert a row to database, it will make the data durable in our device even if the machine is shut down. Write through will cost much more time and resource because we have to access storage every time, but it can make sure we won't lose data.

Concept of write back

Write back, however, is that we update data to storage in aperiod of time, which means we may lose our lastest data but get a higher performance. For instance, in a write-back cache, data is initially written only to the cache. The write to the main storage is deferred until the modified content in the cache is about to be replaced. This means the data will not be updated right now in the storage.

Advantages and Disadvantages

  1. write through:
  • Advantages: First, this approach ensures strong data integrity and consistency between the cache and the storage. There's no risk of losing data in the cache that hasn't been written to the storage. Second, It is simpler to implement and manage.
  • Disadvantages:Write-through can be slower for write operations since every write involves updating the main storage. So in the distributed system which requires low-latency will not use it. And in the case of solid-state drives, this could lead to more wear and tear due to frequent writes.
  1. write back
  • Advantages: This approach can offer better performance for write operations, as it reduces the number of write operations on the main storage.For storage types susceptible to wear (like SSDs), write-back reduces the total number of write operations, thereby potentially increasing the lifespan of the device.
  • Disadvantages:If the cache loses power or fails before the data is transferred to the main storage, data loss can occur and it could lead stale read.Also, managing a write-back cache is more complex, as it requires additional mechanisms to track when the cached data needs to be written back to the storage.

Difference:

type Operation method data consistency performance application
write through Data in cache and main memory (such as hard disk or RAM) are updated simultaneously higher data consistency Each write operation requires writing to main memory, which can lead to performance degradation, especially when main memory writes are much slower than cache Suitable for scenarios with high data consistency requirements and systems where main memory access speed is not a bottleneck
write back Updates to data in main memory are deferred until data in the cache needs to be replaced lower data consistency This generally provides higher performance since writes only affect the cache Suitable for applications that have high performance requirements and can tolerate temporary data inconsistencies, such as certain types of database operations

Example:

  • Critical Data Storage: In applications where data consistency and integrity are crucial, such as banking systems and transaction processing systems, the Write Through approach is more suitable. It ensures that data is saved to the primary storage as soon as it is written.

  • Applications with Infrequent Writes: In scenarios with infrequent write operations or smaller volumes of data, the performance overhead of Write Through is not significant, making it a suitable choice.

  • Systems Requiring Fast Recovery: In systems that need to recover quickly from failures, the Write Through approach can expedite the recovery process since the data in the primary storage is always up-to-date.

  • High-Performance Applications: For scenarios requiring high write performance, such as high-frequency trading, big data processing, and gaming servers, Write Back is preferable due to its higher performance.

  • Reducing Wear and Tear: In cases where storage media are prone to wear (like SSDs), Write Back can reduce the number of write operations to the storage medium, prolonging its lifespan.

  • Cache-Intensive Applications: For applications that heavily rely on caching and experience frequent changes in cache data (like certain database systems), Write Back can minimize access to the primary storage, thereby enhancing overall performance.

    For instance:Such as Oracle, MySQL, etc., usually use write strategies in their disk write operations to ensure data consistency and integrity. Some file systems, especially those used for critical data storage, may employ write policies to ensure data consistency and security. Such as Cassandra and Redis, etc., the write back strategy can be used in certain configurations, especially in scenarios that require high-performance write operations. Caching systems like Memcached may use a write-back strategy internally to improve the performance of cache operations.

Summary

Write-through is more reliable and simpler but can be slower and more taxing on the storage medium, while write-back offers better performance and reduced wear but comes with a higher risk of data loss and complexity. The choice between the two often depends on the specific requirements of the system, such as the need for performance versus the need for data integrity.

With this article at OpenGenus.org, you must have the complete idea of Write through and write back.

Write through and write back
Share this