> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monad.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# 异步 I/O

*异步 I/O* 是一种输入/输出处理方式,允许 CPU 在通信进行的同时继续并发执行。

磁盘和网络的速度比 CPU 慢几个数量级。与其发起 I/O 操作后等待结果,CPU 可以在得知需要某数据时立即发起 I/O 操作,并继续执行不依赖该 I/O 操作结果的其他指令。

为便于说明,提供一些粗略的对比:

<div class="mintlify-table-wrapper">
  <table class="mintlify-table">
    <thead>
      <tr>
        <th>设备</th>
        <th>延迟</th>
        <th>带宽</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>CPU L3 缓存</td>
        <td>10 ns</td>
        <td>>400 GB/s</td>
      </tr>

      <tr>
        <td>内存</td>
        <td>100 ns</td>
        <td>100 GB/s</td>
      </tr>

      <tr>
        <td>磁盘 (NVMe SSD)</td>
        <td>400 us</td>
        <td>380 MB/s</td>
      </tr>

      <tr>
        <td>网络</td>
        <td>50 - 200 ms</td>
        <td>1 Gb/s (125 MB/s)</td>
      </tr>
    </tbody>
  </table>
</div>

(实际磁盘统计数据来自 fio 对大小为 2KB 的随机读取报告 - \~190k IOPS)

幸运的是,SSD 驱动器可以并发执行操作,因此 CPU 可以同时发起多个请求,继续执行,然后大约在同一时间接收多个操作的结果。

一些数据库(如 lmdb / mdbx)使用内存映射存储来读写磁盘。不幸的是,内存映射存储由内核实现 (mmap),并非异步,因此在等待操作完成时执行会被阻塞。

有关异步 I/O 的更多信息,请参阅[此处](https://en.wikipedia.org/wiki/Asynchronous_I/O)。
