# 缓存驱逐

## 缓存驱逐概述

由于Alluxio使用的存储空间是有限的，缓存驱逐功能通过多种策略驱逐旧数据，以确保有足够的存储空间缓存新数据。

Alluxio将通过两种不同的方式驱逐其缓存：

* 写入时驱逐
* 后台异步驱逐

## 写入时驱逐

写入时驱逐是在Alluxio中写入page时同步检查并清除缓存的数据。 当Alluxio即将写入一个page且总缓存将超过存储容量时，将触发驱逐。

### 缓存驱逐器

Alluxio提供以下五种缓存驱逐器来驱逐缓存数据：

* `LRUCacheEvictor`（默认）：LRU缓存驱逐策略
* `FIFOCacheEvictor`：FIFO缓存驱逐策略
* `LFUCacheEvictor`：LFU缓存驱逐策略。page根据对数计数按桶顺序排序。桶内的page按LRU顺序排序。
* `NondeterministicLRUCacheEvictor`：具有不确定性缓存驱逐策略的LRU。均匀驱逐LRU尾部的元素。
* `TwoChoiceRandomEvictor`：两选随机客户端缓存驱逐策略。它选择两个随机page ID并驱逐使用最少的一个。

worker缓存和client缓存有不同的属性来定义它们各自的驱逐器。 例如，以下在`alluxio-site.properties`中的配置为worker缓存和client缓存设置`LRUCacheEvictor`。

```properties
alluxio.worker.page.store.evictor.class=alluxio.client.file.cache.evictor.LRUCacheEvictor
alluxio.user.client.cache.evictor.class=alluxio.client.file.cache.evictor.LRUCacheEvictor
```

## 后台异步驱逐

Alluxio 支持针对缓存空间设置不同的约束条件，这些约束条件将触发驱逐操作：

* 设置页面可以占用的总大小的限制，即页面存储的容量；
* 设置页面存储中页面总数的限制。

在每次写入page操作时，Alluxio 会检查是否达到了其中的约束条件。如果违反了约束条件，则会进行同步驱逐，为新page的写入腾出空间。 然而，在写入时进行同步驱逐会显著降低性能。后台的异步驱逐旨在预先异步驱逐缓存数据，以避免在写入操作期间驱逐缓存数据。

### 基于缓存容量的异步驱逐

要启用后台异步驱逐功能，请在`alluxio-site.properties`中添加以下配置：

```properties
alluxio.user.client.cache.async.eviction.enabled=true
alluxio.user.client.cache.async.eviction.check.interval=1min
alluxio.user.client.cache.async.eviction.high.water.mark=0.9
alluxio.user.client.cache.async.eviction.low.water.mark=0.8
```

通过设置上述配置，Alluxio将创建一个后台线程，检查page缓存空间是否达到高水位线阈值。 一旦触发此条件，它将驱逐缓存的page直到达到低水位线阈值。 后台线程将周期性进行检查。

### 基于Page数量上限的异步驱逐

Alluxio 会在后台线程中异步驱逐超过数量限制的page，该线程会定期扫描、检查过多的page。当page总数超过`highWatermark * maxPageNumberLimit`时， 会触发驱逐操作，直到page总数降到`lowWatermark * maxPageNumberLimit`以下。

要启用基于page数量限制的异步驱逐功能，将`alluxio.worker.page.store.max.page.number.limit.enabled`设置为`true`。 最大page数量的限制可以通过`alluxio.worker.page.store.max.page.number`来指定。

```properties
alluxio.user.client.cache.async.eviction.enabled=true
alluxio.user.client.cache.async.eviction.check.interval=1min
alluxio.user.client.cache.async.eviction.low.water.mark=0.6
alluxio.user.client.cache.async.eviction.high.water.mark=0.8

alluxio.worker.page.store.max.page.number.limit.enabled=true
alluxio.worker.page.store.max.page.number=100000
```

### 动态更新配置的REST API

Alluxio提供以下REST API，用户可以动态设置和获取异步驱逐配置：

* 启用异步驱逐并更新相关参数

```shell
curl --location --request POST 'localhost:28080/v1/cache?cmd=enableCacheAsyncEviction&chacheEvictionCheckInterval=30&highWaterMark=0.8&lowWaterMark=0.5'
```

* 禁用异步驱逐

```shell
curl --location --request POST 'localhost:28080/v1/cache?cmd=disableCacheAsyncEviction'
```

* 获取当前异步驱逐参数

```shell
curl --location 'localhost:28080/v1/cache?cmd=getPageCacheAsyncEvictionManagerInfo'
```


---

# 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://documentation.alluxio.io/ee-ai-cn/ai-3.3/feature/cache-evicting.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.
