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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
