Redis performance benefits
First of all, Redis is also a cache database developed in C language, but different from traditional databases, Redis data is stored in memory, that is, it is an in-memory database, so the reading and writing speed is very fast, so Redis is widely used in the cache direction. Official test 50 concurrent execution 100000 requests, read speed 110000 times /s write speed 81000 times /s, high concurrency excellent solution.
Redis solves those problems
- Cached data is stored in memory, similar to the time complexity of HashMap lookup and operation, which is O (1).
- Supports rich data types, including String, Hash, list, set, and zset
- Transactions are supported, and operations are atomic, meaning that changes to data are either all or none
- Rich features, can be used for caching, messages, counting, by key set expiration time, will be automatically deleted after expiration
- Single thread, preventing multithreaded race problems
Why is Redis so fast
- Completely memory based, the vast majority of requests are pure memory operations, very fast data is stored in memory based on Hashmap, lookup and operation time is o(1)
- The use of single thread, avoid unnecessary context switch and competition conditions, there is no multi-process or multi-threading caused by the switch and additional CPU consumption, do not have to consider the problem of various locks, there is no lock release lock, not because of deadlock performance consumption.
- The data structure is simple and the operation of the data is also simple. The data structure in Redis is specially designed.
- Use the multiplexing IO model – multiple network connections multiplexing a single thread
Here, “multiplexing” refers to multiple network connections, and “multiplexing” refers to the multiplexing of the same thread. Multiplexing technology can make a single thread efficiently process multiple connection requests (minimize the time consumption of network IO), and Redis operates data in memory at a very fast speed. In other words, in-memory operations will not be a bottleneck affecting Redis performance, which is mainly due to the high throughput of Redis.
Why is Redis single threaded
This question first needs to be clear why we use multi-threading, multi-threading is to take advantage of the multi-core CPU power of the computer to improve concurrency, but the official single-threading test data has shown that CPU is not the bottleneck of Redis, the most likely bottleneck may be the size of the machine memory and network bandwidth. Since Redis is a memory-based operation, CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of the machine’s memory or network bandwidth. Since single-threading is easy to implement and the CPU is not a bottleneck, it makes sense to go with a single-threaded solution (there is a lot of trouble with multi-threading after all!). .