Pay attention to the public account, communicate together, wechat search: sneak forward
High performance of Redis
- Pure memory access, all data is in memory, all operations are memory-level operations, and memory response time is nanosecond. Therefore, the CPU of redis process has no disk I/O latency and memory read/write performance problems. CPU is not the bottleneck of Redis (memory size and network I/O are the bottleneck of Redis, namely the network transfer delay between client and server).
- Using single thread model, single thread implementation is simple. Avoids the overhead of multiple threads with frequent context switches and synchronization mechanisms such as locking
- Simple and efficient basic data structures: dynamic string (SDS), linked list, dictionary, skip linked list, integer set and compressed list. Redis then builds on this foundation to implement objects that users can manipulate: strings, lists, hashes, collections, ordered collections, etc
- A network event handler for the REACTOR schema. It uses I/O multiplexing to monitor multiple sockets simultaneously, which is an efficient I/O model. Take a look at the IO+ REACTOR model for Linux high-performance networks
Why single threads
- Single thread is used to avoid unnecessary context switching and race conditions. There are no cpu-consuming switches caused by multithreading
- There are no locks to worry about, no locking and unlocking operations, and no performance costs due to possible deadlocks
- Simple and maintainable, multithreaded mode will make the writing of the program more complex and troublesome, single thread implementation is easy to implement
Redis is a single-threaded system?
- We often refer to REDIS Single threaded as its Network event processing model REACTOR is single threaded
- If a client request deletes millions of keys, this command can cause a block. In Redis 4.0, multithreading is chosen to implement this type of non-blocking command
- In Redis6.0, multi-threading was officially introduced to handle client I/O read and write responses, while redis command events are still handled in the main thread
- If it is for the entire Redis system, there are always other threads handling asynchronous tasks, such as AOF, RDB synchronization
Redis I/O model
- Redis is fast not only because it is a single-threaded pure memory system, but also because it uses the Reactor model to process external requests using I/O multiplexing, reducing network connection and read/write latency. In this way, it can concurrently process a large number of client requests in network I/O operations, achieving high throughput and high concurrency
Redis multithreaded version of the I/O model 6.0
Corrections are welcome
Refer to the article
- Redis In-depth Series – Threaded IO model 1
- Redis multithreaded network model full disclosure