> For the complete documentation index, see [llms.txt](https://aaronice.gitbook.io/system-design/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aaronice.gitbook.io/system-design/distributed-systems/caching.md).

# Caching

## Cache Invalidation

### **Write-through cache:**

> Under this scheme **data** is **written** into the **cache** and the corresponding **database** at the **same** **time**.
>
> Every **write** **operation** must be **done** **twice** **before** **returning** **success** to the **client**

#### Pros:

* (**Data Consistency**) The cached data allows for **fast retrieval**, and since the **same** **data** gets written in the **permanent** **storage**, we will have complete **data consistency** between **cache** and **storage**.
* (**Fault** **Tolerance**) This scheme ensures that nothing will get lost in case of a crash, power failure, or other system disruptions. It **minimizes** the **risk** of **data** **loss**.

#### Cons:

* (**Higher Latency**) higher **latency** for **write** operations

### **Write-around cache:**

> This technique is similar to write through cache, but data is written directly to permanent storage, bypassing the cache.

#### Pros:

* This can reduce the cache being flooded with write operations that will not subsequently be re-read,&#x20;

#### Cons:

* but has the disadvantage that a read request for recently written data will create a “cache miss” and must be read from slower back-end storage and experience higher latency.

### **Write-back cache:**

> Under this scheme, **data** is **written** to **cache** **alone**, and **completion** is **immediately** **confirmed** to the **client**.
>
> The **write** to the **permanent** **storage** is done **after** **specified** **intervals** or under **certain** **conditions**.

#### Pros:

* (**Low Latency & High Throughput**) This results in **low** **latency** and **high** **throughput** for **write-intensive** **applications**,&#x20;

#### Cons:

* (**Data Loss**) however, this speed comes with the **risk of data loss** in case of a crash or other adverse event because the **only copy** of the **written** **data** is in the **cache**.

Cache Invalidation Schemes Comparison

|             | Write-through cache | Write-around cache | Write-back cache |
| ----------- | ------------------- | ------------------ | ---------------- |
| Description |                     |                    |                  |
|             |                     |                    |                  |
| Pros        |                     |                    |                  |
| Cons        |                     |                    |                  |

## Cache Eviction Policies

| Cache Eviction Policies     | Description                                                                                                                       |
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| First In First Out (FIFO)   | The cache evicts the first block accessed first without any regard to how often or how many times it was accessed before.         |
| Last In First Out (LIFO)    | The cache evicts the block accessed most recently first without any regard to how often or how many times it was accessed before. |
| Least Recently Used (LRU)   | Discards the least recently used items first.                                                                                     |
| Most Recently Used (MRU)    | Discards, in contrast to LRU, the most recently used items first.                                                                 |
| Least Frequently Used (LFU) | Counts how often an item is needed. Those that are used least often are discarded first.                                          |
| Random Replacement (RR)     | Randomly selects a candidate item and discards it to make space when necessary.                                                   |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://aaronice.gitbook.io/system-design/distributed-systems/caching.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
