# 代理

代理服务器是位于客户机和后端服务器之间的一个中间硬件软件。它接收来自客户机的请求，并将它们转发给原始服务器。通常，代理被用来过滤请求或记录请求，或者有时转换请求(通过添加、删除头、加密、解密或压缩)。代理服务器的另一个优点是它的缓存可以服务大量的请求。如果多个客户端访问一个特定的资源，代理服务器可以缓存它并服务于所有客户端，而不需要访问远程服务器。

![img\_2.png](/files/sjWEsaxhPZKE4ZArmpj0)

代理在协调来自多个服务器的请求时也非常有用，可以用于从系统范围的角度优化请求流量。例如，我们可以将相同(或类似)的数据访问请求折叠成一个请求，然后将单个结果返回给用户;这种方案称为折叠转发。

假设有一个跨多个节点对相同数据的请求，而该数据块不在缓存中。如果这些请求通过代理路由，那么可以将所有请求合并为一个请求，这意味着我们只需要从磁盘读取一次所需的数据。

另一种使用代理的好方法是将对存储(连续地在磁盘上)中空间上接近的数据的请求折叠起来。这种策略将减少请求延迟。例如，假设一些服务器请求文件的部分:part1, part2, part3，等等。我们可以将代理设置为能够识别单个请求的空间位置的方式，从而将它们分解为单个请求并读取完整的文件，这将极大地减少从数据源读取的次数。当我们跨tb数据进行随机访问时，这种方案在请求时间上有很大的不同。代理在高负载的情况下特别有用，或者当我们的缓存有限时，因为代理可以将多个请求批处理成一个。


---

# 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://vagrant.gitbook.io/grokking-system-design/dai-li.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.
