What are write-through and write-behind caching?

Caching is a crucial strategy to improve the speed and performance of databases and applications, storing frequently used information in an easy-to-access location in memory. But with multiple caching strategies available, which is the best one for your situation?

Below, we’ll discuss the definitions of write-through caching and write-behind caching, as well as how write-through caching and write-behind caching work.

What is write-through caching?

Write-through caching is a caching strategy in which the cache and database are updated almost simultaneously. When we want to update the information in the cache, we first update the cache itself, and then propagate the same update to the underlying database. Changes to the cache that have not yet been sent to the database are referred to as “dirty.”

The use of write-through caching helps guarantee that your data is consistent between the cache and the database. Write-through caching is best when you expect to perform update operations relatively infrequently. If you perform updates too many times while using write-through caching, then this reduces the benefits of having a cache in the first place, since you will need to access the database anyway.

Write-through caching is especially helpful when you want to guard against system failures, such as power outages or crashes. If the system goes down while using write-through caching, it’s very likely that the data in the cache and the database will be identical once the system recovers, since updates are made almost instantaneously.

What is write-behind caching?

Write-behind caching is a caching strategy in which the cache is updated first, and then the database is updated after a set period of time. 

The use of write-behind caching is more convenient when you expect to have a write-heavy workload, i.e. you expect to perform many cache updates. Write-behind caching improves system performance because the user does not (usually) have to wait for changes to be made to the database.

As long as the time limit is not too long, write-behind caching may still deliver acceptable protection from system failures. However, the risk of data loss is greater when using write-behind caching. If the system goes down, any changes to the cache data may not have yet been sent to the database.

How do write-through and write-behind caching work?

In general, write-through caching is easier to implement than write-behind caching for several reasons.

First, in order to successfully implement write-behind caching, all parts of the system must first make changes to the cache before changing the database, as well as check the cache before accessing the database. Otherwise, the system could miss the “dirty” records stored in the cache that have not yet been sent to the database.

Second, to improve performance, write-behind caching uses a technique known as conflation, in which changes to the cache are consolidated in order to limit the number of transactions needed to update the database. For example, if a value is changed from 1 to 2 in the cache, and then later from 2 to 3, the database will only be updated to change the value from 1 to 3.

Write-through and write-behind caching in Redis

Redis is an open-source, in-memory data structure store that is frequently used to build NoSQL key-value databases, caches, and message brokers. However, one drawback of Redis is that it isn’t automatically compatible with programming languages such as Java out of the box. To lower the Redis learning curve, many Java developers install a third-party Redis Java client such as Redisson.

Redisson implements many different Java objects, collections, and constructs, making it easy for Java developers to get started using Redis. The good news is that Redisson includes functionality for write-through and write-behind caching in Redis by using the RMap interface.

Below is an example of how to use write-behind caching in Redis with Redisson:

MapOptions<K, V> options = MapOptions.<K, V>defaults()

As the name suggests, the writeBehindDelay() method defines the number of milliseconds to delay a write or delete operation to the database. The writeBehindBatchSize() method defines the size of the batches that will be sent to the database; each element in the batch corresponds to a single write or delete operation.

For more information on using write-through and write-behind caching in Redis with Redisson, check out the Redisson wiki on GitHub.

Similar terms