Redis is short and concise, and the Swiss army knife in the system. I have studied its source code and gained a lot.
1: network framework
2: exquisite implementation of various data structures
3: Persistence
4: Copy
Network Framework
The redis network framework is quite simple. redis implements an event library by itself. The overall framework is: single process based on events. Generally, we think that the current servers are multi-process or multi-thread, and the performance of a single process is not good. But redis is just different. It is a single process and has good performance. I personally think there are the following reasons:
1. The memory structure of redis is very complicated. It is inevitable to lock Multiple Threads. Locking is a huge overhead, and this is avoided by a single process.
2. The characteristics of data processed by redis. All redis data is in the memory, and each piece of data is usually very small. In this way, it takes very little time to access or search for data, so serial processing on the server can also work.
Exquisite implementation of various data structures
This is redis's most distinctive feature. redis is not a key-value, but it also implements other data structures such as list, hash, set, and sort set. It is not difficult to implement these data structures. It is rare to use the least memory space to implement these structures. For example, the string "123456" needs 6 bytes (not counted as 0) for storage, and the number 123456 only requires 4 bytes. redis will store the string "123456" as an integer; in a hash table, if there are few items in the hash table, there is no need to store the hash table. Because of the large overhead of a hash table, there are very few items in the hash table, in this case, the sequential table is saved quickly. This technique is widely used in redis, with the aim of minimizing memory usage.
Persistence
Http://redis.io/topics/persistence
There are two methods for persistence: RDB and aof (append of file)
RDB writes the image of the database to the disk at every time. This advantage is that the server loads data fast and the transmission efficiency is high. The disadvantage is that it is willing to lose data and time-consuming.
Aof is recorded when the database is modified.
Copy
Http://redis.io/topics/replication