Detailed explanation of data structure in Redis
In actual development, Redis
will be used frequently, so how should we correctly choose the data type during use? Which data types are suitable in which scenarios. And in interviews, interviewers often ask questions about Redis data structure:
- Why is Redis fast?
- Why does the query operation slow down?
- Redis Hash rehash process
- Why use a hash table as the index of Redis
When We have analyzed and understood the Redis
data structure, which can help us correctly choose the data type to use and improve system performance when using Redis
. [Related recommendations: Redis video tutorial]
Redis
Underlying data structure
Redis
Yes A memorykey-valuekey-value
database, and the key-value pair data is stored in memory, so Redis
memory-based data operations , which has high efficiency and fast speed;
Among them, Key
is the String
type, Redis
supports the value
type Including String
, List
, Hash
, Set
, Sorted Set
, BitMap
wait. Redis
The reason why it can be widely applied to many business scenarios is based on its diverse types of value
.
The data type of Value
of Redis
is based on the object system customized for Redis
redisObject
Implemented,
typedef struct redisObject{ //类型 unsigned type:4; //编码 unsigned encoding:4; //指向底层实现数据结构的指针 void *ptr; ….. }
redisObject
In addition to recording actual data, additional memory space is also required to record metadata information such as data length, space usage, etc., which contains 8 bytes of metadata and a 8-byte pointer, the pointer points to the actual data location of the specific data type:
Among them, the pointer points to the location based on The underlying data structure of Redis
stores the location of data. The underlying data structure of Redis
is: SDS
, implemented by doubly linked lists, jump tables, hash tables, compressed lists, and integer sets. .
So how is the underlying data structure of Redis implemented?
Redis underlying data structure implementation
Let’s take a look at Redis
relatively simpleSDS
, two-way Linked list, set of integers.
SDS
, doubly linked list and integer set
SDS
, use the len
field to record The number of bytes used reduces the complexity of obtaining the string length to O(1), and SDS
is lazy releasing space, you free
free the space , the system records the data and can use it directly when you want to use it next time. No need to apply for new space.
Integer collection, allocate a space with consecutive addresses in the memory, and the data elements will be stored next to each other, without the need for additional pointers to bring space overhead. Its characteristics areThe memory is compact and saves memory space, the query complexity is O(1) and the efficiency is high, and the complexity of other operations is O(N);
Doubly linked list , it can be a non-contiguous and non-sequential space in the memory, and the sequence between the elements is connected in series through the additional pointer overhead of the front-end/back-end pointer.
Its characteristics are that the complexity of inserting/updating data in the section is O(1), high efficiency, and the query complexity is O(N);
Hash
ha Hash table
Hash table is actually similar to an array. Each element of the array is called a hash bucket. Each hash bucket stores key-value pair data, and the hash bucket The elements in use the dictEntry
structure,
Therefore, the hash bucket element does not save the key-value pair itself, but points to A pointer to a specific value, so there will be additional space overhead when saving each key-value pair, at least 24 bytes will be added, especially Value
is String
key-value pairs, each key-value pair requires an additional 24 bytes of space. When the saved data is small and the additional overhead is larger than the data, in order to save space, consider changing the data structure.
Let’s take a look at the full picture of the global hash table:
Although the hash table operation is very fast, Redis
After the data becomes larger, A potential risk will arise: Hash table conflict problem and rehash
overhead problem, Can this explain why the hash table operation slows down?
When writing more data into the hash table, hash conflicts are an inevitable problem. The way Redis solves hash conflicts is Chained Hash , multiple elements in the same hash bucket are stored in a linked list, and they are connected with pointers in turn, as shown in the figure:
When there are more and more hash conflicts, this will cause some hash conflict chains to be too long, which will lead to a long time-consuming search for elements on this chain and reduced efficiency.
In order to solve the problem of too long chains caused by hash conflicts, perform rehash
operation to increase the number of existing hash buckets and disperse the single Number of bucket elements. So how is the rehash
process performed?
Rehash
To make the rehash
operation more efficient, two global hash tables are used: Hash table 1 and hash table 2, as follows:
- Allocate larger space to hash table 2,
- Remap the data in hash table 1 and copy it to the hash in Table 2;
- Release the space of hash table 1
However, due to the large data size of tables 1 and 2 during remapping and copying, if you put hash table 1 in one go After all the data has been migrated, the Redis
thread will be blocked and unable to serve other requests.
In order to avoid this problem and ensure that Redi
s can handle client requests normally, Redis
adopts progressive rehash
.
Every time a request is processed, all entries at the index position are copied from hash table 1 to hash table 2, and the overhead of a large number of copies at one time is allocated to the process of processing multiple requests. , avoiding time-consuming operations and ensuring fast access to data.
After understanding the relevant knowledge points of Hash
Hash tables, let’s take a look at the uncommon compression lists and skip tables.
Compressed list and skip table
Compressed list, based on the array, the compressed list has three fields in the header: zlbytes, zltail and zllen, respectively represents the length of the list, the offset of the end of the list and the number of entries in the list; the compressed list also has a zlend at the end of the table, indicating the end of the list.
Advantages: The memory is compact and saves memory space. A space with continuous addresses is allocated in the memory, and the data elements will be stored next to each other without the need for additional pointers. To reduce space overhead; searching and locating the first element and the last element can be directly located through the length of the three header fields, and the complexity is O(1).
Jump list, based on the linked list, adds a multi-level index, and achieves rapid positioning of data through several jumps in the index position, as shown in the following figure:
For example, query 33
Features: When the amount of data is large, the search complexity of the skip table is O(logN).
To sum up, we can know the time complexity of the underlying data structure:
Data structure type | Time complexity |
---|---|
Hash table | O(1) |
Integer array | O(N) |
Doubly linked list | O(N) |
Compressed list | O( N) |
Jump list | O(logN) |
Redis
The custom object system type is the Value
data type of Redis
. The data type of Redis
is based on the underlying If the data structure is implemented, what are the data types?
Redis data type
String
、List
、Hash
、Sorted Set
, Set
are relatively common types, and their corresponding relationship with the underlying data structure is as follows:
Data type | Data structure | |
---|---|---|
String | SDS (Simple Dynamic String) | |
List | Doubly linked list Compressed list |
|
Compressed list | ||
Compressed List | ||
Hash Table |
Based on List | Based on Strems | |
---|---|---|
Message order preservation | Use LPUSH/RPOP
|
Use XADD/XREAD
|
Use | BRPOP
| Use XREAD block
|
Producers implement global unique IDs by themselves | Streams automatically generates globally unique IDs | |
Use | BRPOPLPUSH
| Use PENDING List to automatically retain messages
|
The total amount of messages is small | The total amount of messages is large and data needs to be read in the form of consumer groups |
Location-based LBS service is implemented using the specific GEO data type of
Redis.
GEO can record geographical location information in the form of longitude and latitude, and is widely used in LBS is in service. For example: how taxi-hailing software provides services based on location.
Summary
Redis is so fast because of its memory-based data manipulation and use of
HashHash As an index, a table is highly efficient and fast, and thanks to the diversification of its underlying data, it can be applied to many scenarios. Choosing the appropriate data type in different scenarios can improve its query performance.
Programming Video! !
The above is the detailed content of Detailed explanation of data structure in Redis. For more information, please follow other related articles on the PHP Chinese website!

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment