15 diagrams why redis is so fast

Time:2021-7-3

You must have dealt with redis at work. Redis   Why fast? You must know that, at least for the interview. A lot of people know that   Redis   Fast just because it’s memory based is ambiguous for other reasons.

Let’s take a look with Xiao Bian today

15 diagrams why redis is so fast

  • Mind map-

Implementation based on memory

This point was mentioned at the beginning. Let’s talk about it briefly.

Redis is a database based on memory, which inevitably has to be compared with disk database. For disk database, it is necessary to read data into memory, which is limited by disk I / O.

For the memory database, the data itself exists in the memory, there is no overhead in this regard.

Efficient data structure

There are many data types in redis, and the underlying layer of each data type is supported by one or more data structures. Because of these data structures, the speed of redis in storage and reading is not hindered. What’s special about these data structures

15 diagrams why redis is so fast

1. Simple dynamic string

This noun may be unfamiliar to you  SDS  I’ll know for sure. This is used to handle strings. Anyone who knows C language knows that it has string processing methods. Redis is implemented in C language, so why do we need to build wheels repeatedly? We can see from the following points:

(1) String length processing

15 diagrams why redis is so fast

This graph is the storage method of string in C language. To get the length of “redis”, you need to traverse from the beginning until you encounter ‘0’.

15 diagrams why redis is so fast

How to operate in redis? Use a len field to record the length of the current string. To get the length, just get the len field. You see, the gap is self-evident. The time complexity of the former is O (n), and O (1) in redis   You can get it. It’s faster.

(2) Memory reallocation

When it comes to modifying strings in C language, memory will be reallocated. The more frequent the changes, the more frequent the memory allocation. Memory allocation will consume performance, so performance degradation is inevitable.

Redis involves frequent string modification operations, so this memory allocation method is obviously not suitable. therefore   SDS   Two optimization strategies are implemented

  • Space pre allocation

In addition to the necessary space, the unused space will also be allocated when modifying and expanding the SDS.

The specific allocation rule is as follows: if the length of len is less than 1m after SDS modification, the unused space with the same length as len will be allocated additionally. If the modified length is greater than 1m, 1m usage space will be allocated.

  • Inert space release

Of course, if there is space allocation, there will be space release.

SDS shortens, does not reclaim the extra memory space, but uses the free field to record the extra space. If there is a subsequent change operation, the space recorded in free is directly used to reduce the memory allocation.

(3) Binary security

As you know, redis can store all kinds of data types, so binary data is no exception. But binary data is not a regular string format, and may contain some special characters, such as’ 0 ‘.

As we mentioned earlier, a string in C encounters a  ‘ 0’   It’s going to end. That’s it  ‘ 0’   After that, the data can’t be read. But in SDS, the end of a string is determined by the length of len.

Look, the problem of binary security is solved.

2. Double ended list

Lists are more often used as queues or stacks. Queue and stack have the characteristics of one FIFO and one FIFO. The double ended list supports these features very well.

15 diagrams why redis is so fast

-Double ended list  –

(1) Front and back nodes

15 diagrams why redis is so fast

Each node in the linked list has two pointers. Prev refers to the forward node and next to the back node. In this way, the time complexity is 0   O(1)   Before and after the node can be obtained.

(2) Head and tail nodes

15 diagrams why redis is so fast

You may notice that there are two parameters in the head node, head and tail, pointing to the head node and tail node respectively. This design can reduce the processing time complexity of two terminal nodes   O(1)  , Perfect for queues and stacks. At the same time, the linked list can be iterated from both ends.

(3) Length of linked list

There is also a parameter len in the header node, similar to the SDS mentioned above, which is used to record the length of the linked list. Therefore, when getting the length of the linked list, you don’t need to traverse the whole linked list, just get the len value directly. The time complexity is very low   O(1)。

You see, these features reduce the time cost of using list.

3. Compressed list

We are familiar with double ended list. I don’t know if you have noticed a problem: if a small data, such as a byte, is stored in a linked list node. Then the corresponding will save the header node, before and after the pointer and other additional data.

In this way, space is wasted, and memory fragmentation is easily caused by repeated application and release. In this way, the efficiency of memory usage is too low.

So, the compressed list came on!

15 diagrams why redis is so fast

It is specially coded to improve the efficiency of memory usage. All operations are performed by pointer and decoded offset.

And the compressed list memory is continuously allocated, traversal speed is very fast.

4. Dictionary

Redis is a K-V database. All key values are stored in dictionaries.

You should not be unfamiliar with the dictionary used in daily learning. If you want to find a word, you can directly locate it through a word, which is very fast. The dictionary mentioned here is the same in principle, and the corresponding value can be obtained directly through a key.

A dictionary is also called a hash table, which has nothing to say. Hash table features we are very clear, can be used in the   O(1)   The associated values are extracted and inserted within the time complexity.

5. Jump table

As a unique data structure in redis, jump table adds multi-level index on the basis of linked list to improve the search efficiency.

15 diagrams why redis is so fast

This is a simple schematic of the jump list, each layer has an ordered list, the bottom list contains all the elements. In this way, the jump table can support the   O(logN)   Find the corresponding node in the time complexity of.

The following is the real storage structure of the jump table. Like other data structures, the corresponding information is recorded in the header node, which reduces some unnecessary system overhead.

15 diagrams why redis is so fast

Reasonable data coding

For each data type, the underlying support may be a variety of data structures, when to use which data structure, which involves the problem of coding transformation.

Let’s take a look at how different data types are coded and transformed

String: the code of int type is used to store numbers, and raw code is used to store non numbers;

List: if the length of string and the number of elements are less than a certain range, use ziplist encoding. If any condition is not met, it will be converted to LinkedList encoding;

Hash: the length of the key and value string in the key value pair saved by hash object is less than a certain value and key value pair;

Set: use intset encoding when the number of saved elements is integer or less than a certain range, and hashtable encoding when any condition is not satisfied;

Zset: ziplist encoding is used when the number of elements and the length of members in Zset object are less than a certain value, and skiplist encoding is used when any condition is not satisfied.

Appropriate thread model

Another reason why redis is fast is that it uses an appropriate thread model

15 diagrams why redis is so fast

1. I / O multiplexing model

  • I/O : Network I / O
  • multiple: multiple TCP connections
  • multiplexing: share a thread or process

In the production environment, multiple clients usually connect to redis, and then send commands to the redis server. Finally, the server processes these requests and returns the results.

15 diagrams why redis is so fast

In response to a large number of requests, it is used in redis   I / O multiplexers listen to multiple sockets at the same time, push these events to a queue, and then execute them one by one. Finally, the result is returned to the client.

2. Avoid context switching

You must have heard that redis is single threaded. So why is single threaded redis fast?

Because multithreading needs to switch the context of CPU in the process of execution, this operation is time-consuming. Redis is based on memory. For memory, no context switching is the most efficient. Read and write many times on a CPU, for memory is the best solution.

3. Single thread model

By the way, why   Redis is single threaded.

Redis uses the reactor single threaded model, which you may not be familiar with. It doesn’t matter. I just need to know something about it.

15 diagrams why redis is so fast

In this picture, after receiving the user’s request, all the requests are pushed to a queue and then sent to the file event dispatcher, which works in a single thread mode. Redis works based on it, so redis is single threaded.

summary

Implementation based on memory

  • Data are stored in memory, reduce some unnecessary I / O operation, the operation speed is very fast.

Efficient data structure

  • A variety of underlying data structures support different data types and redis stores different data;
  • The design of different data structures minimizes the time complexity of data storage.

    Reasonable data coding

  • Different encoding formats are adapted according to the length of string and the number of elements.

Appropriate thread model

  • I / O multiplexing model monitors client connections at the same time;
  • Single thread does not need context switch in the process of execution, which reduces the time consumption.

A few words of nagging

If you feel that the article has a little help for you, you are welcome to share it with your friends and praise Xiaobian. This is the driving force for Xiaobian to stick to it. Thank you. See you next time!

=

After three things ❤️

========

If you think this content is quite helpful to you, I’d like to invite you to do me three small favors:
Like, forward, have your “praise and comment”, is my creation power.
Pay attention to the official account of “Java didi” and share original knowledge without any time.
At the same time, we can look forward to the following articles