Database Caching Strategies
Tags: cache, systemdesign, redis, backend
Published: 2024-03-22 12:43:08

We often face high latencies while fetching data from Database and are unable to meet SLA. Caching is one of the solutions to implement after DB query and table optimisations. 

A **Cache** is used to store the data so that it can be delivered faster to the client as compared to persistent systems like database or disk.

Popular caching tools are:
1. [Redis](https://redis.io/)
2. [Memcached](https://memcached.org/)

There are multiple ways in which data can be written into and read from the cache. Let us explore the most prominently used methods or policies.


## Cache Aside
Cache Aside or Lazy Loading is one of the cache write policies or strategies.

- In cache aside method, the application is responsible for storing data into cache.
- When the client sends request to the application, it looks for data in the cache.
- If the data is found in the cache it is called as _cache-hit_. The data is fetched from cache and returned to client.
- However, if the data is not found in the cache, also called as _cache-miss_, the application queries for data in the database, writes the data into cache and sends the response to the client.
- Since we store data in cache only when it is necessary, this strategy is also called as Lazy-Loading.



![Cache Aside - Write Strategy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh6koyxc7792iu568usl.png)


### Advantages:
- _Cost effective_ because, only the frequently accessed data is stored into cache.

### Disadvantages:
- The response time can be slow when there is cache miss, because, it involves many i/o operations to fetch data from DB and store it in cache.


> 
In case the system can tolerate an initial delay and the same data is to be fetched repeatedly, then this mechanism works best.

> 

__________________________________________________



## Write through 

Write through cache is a simple to implement caching mechanism.

* Here the newly arrived data is written into cache and as well as persisted into the disk or a database. Atomicity is maintained.

There are two ways to implement it:
1] The application writes data to cache and database simultaneously

![Write through cache](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mz9ls68gioad30eo5d0v.png)

2] The application writes data to cache and then the cache writes the data into the database.

![Write through cache 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y577t78bpgorm1dqudns.png)


##### Advantages:
- Faster response times.
- Data integrity because of atomic nature of write operation. 
- Lower latency for subsequent reads.

##### Disadvantages:
- Cache pollution: Since every time the data is filled into cache, it can get filled with less frequently read data and more cache eviction which could introduce some latency.

- Not suitable for write intensive scenarios as the write operations are slower compared to other methods because data needs to be written in cache as well as persistent storage everytime.

> 
In case the system cannot tolerate an initial delay and the writes are infrequent, then this mechanism works best. 

Also if the number of records to be cached is also fixed, then this approach can provide the best of the results.
> 

Thank you :)