> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/shu-ju-ku/redis/2.4-dan-xian-cheng.md).

# 2.4单线程

## 单线程架构

redis在一个瞬间只会执行一条命令

## redis使用单线程为什么还这么快？

1. 纯内存，一条命令在内存中的响应时间大概是100ns
2. 非阻塞IO
3. 避免线程切换和竞态消耗

## 使用redis的注意事项

1. 一次只执行一条命令
2. 拒绝长（慢）命令，包括keys、flushall、flushdb、slow lua script、mutil/exec、operate big value（collection）
3. 其实不是单线程

   fysnc file descriptor或close file descriptor是另外启动线程来执行的


---

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

```
GET https://moluo.gitbook.io/notes/bian-cheng-xue-xi/shu-ju-ku/redis/2.4-dan-xian-cheng.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.
