# Caching strategies

Keywords: \[\[Cache]], \[\[Caching]]

## Cache Aside

1. If data exists in **cache**, **application** reads data from **cache**.
2. If not
   1. **Application** reads data from **database**
   2. **Application** updates data to **cache**.

## Read Through

1. If data exists in **cache**, **application** read data from **cache**.
2. If not
   1. **Cache** reads data from **database**
   2. **Cache** returns data to **application**

## Write Through

1. **Application** writes data to **cache**.
2. **Cache** writes data to **database** immediately.

## Write Back a.k.a Write Behind

1. **Application** writes data to **cache** constantly.
2. **Cache** writes data to **database** once in a while.

## Write Around

### With Cache Aside

1. **Application** writes data to **database**.
2. If data exists in **cache**, **application** reads data from **cache**.
3. If not
   1. **Application** reads data from **database**
   2. **Application** updates data to **cache**.

### With Read Through

1. **Application** writes data to **database** only.
2. If data exists in **cache**, **application** read data from **cache**.
3. If not
   1. **Cache** reads data from **database**
   2. **Cache** returns data to **application**


---

# Agent Instructions: 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:

```
GET https://gia-duong-duc-minh.gitbook.io/notes/computer/caching-strategies.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
