Redis String description

Introduction of Redis

Redis(Remote Dictionary Server) is a high performance open source non-relational cache database, Redis written in C language, support a variety of types of data structures, such as string, hash, list, set, ordered set and range query, bitmaps, Hyperloglogs and Geospatial index radius query. Redis is built with replication, LUA scripting, LRU driver events, transactions and different levels of disk persistence, and provides high availability through Redis Sentry and automatic partitioning.

Redis has become a must-have skill for interviews. I plan to update at least one Redis article every week to learn about Redis. Let’s start with the basic Redis data types


String Basic commands

You create a string of the form set key value, which is the simplest form, and you can add some other parameters, which we’ll talk about later

Look at the string, just get the key

Append the string with append key Content

We see that it has been successfully appended to the original string

Setting the range of a string can be done using Setrange,

The important thing to note here is that no matter how long we want to set the string length, it will be successful, and if it is larger than the original string length, it will be automatically expanded

We can use the getrange command to get the substring at the corresponding position

Here, the starting range can be set to negative to indicate the penultimate value

In a Redis string, if all characters in the string are integers, you can simply increment and decr the “string”. The commands for the increment and decr operation are incr and decr. As shown in the figure below, we assign 996 to test, and the increment and decr operation will succeed

Not if it’s a decimal

So what if I want to increase by 100 every time? Execute incR 100 times? Of course not so much trouble, let’s use increment to demonstrate

With the incrby command, we can set a step after it, and decrby is the same, so you can try it out for yourself.

But what if I just want to increment the decimal type? We can just use the incrbyfloat command

The thing to notice here is that decrByFloat doesn’t exist

What if I want to set multiple strings at once? Of course, the mset command is used, which is equivalent to merging multiple sets together

With batch Settings, what about batch reads? As some readers may already have guessed, yes, the bulk read command is mget

In addition to basic additions and queries, Redis strings have many advanced features

To set the expiration time for key-value pairs, we can set the expiration time for an existing key through the EXPIRE command

Here I set a key-value pair via set, and then set an expiration time of three seconds for the key KKK via expire. The result can be queried within three seconds, but not after three seconds

There is also a command called setex (set and expire). The basic format is setex key second value, after which the key will be deleted.

It can be seen that I set the expiration time for KKK for three seconds. Within three seconds after setting, the corresponding value can be queried, and after three seconds, it can not be checked.

We can also use psetex to do the same thing as setex, except that setex is measured in seconds and PSEtex in milliseconds.

In Redis, the setnx command can be used to set the lock. If the setting is successful, the command cannot be executed again. Setnx (set if not exist) will succeed only when the key does not exist.

As we can see, the first time we execute this command returns 1(success), and the second time returns 0 (failure), because the second time we execute this command, the key already exists, so it must fail.

If I want to reset the key value after I have set the key value through setnx, what should I do? Remember the expiration time we just talked about? We can set an expiration time for it through expire. Of course, there is another way

We can do this by adding other command line arguments to the set operation. Ex indicates the expiration time, 5 indicates the expiration time, and nx indicates the expiration time if the key does not exist. Both ex and nx can be used separately

Some other commands that are not commonly used

The bitcount command is used to count the number of 1s in the binary representation of characters in a string

The binary representation of the string redis is 01110010 01100101 01100100 01101001 01110011. There are 20 ones in total, which can be counted by bitcount

The getbit command obtains the bit value of the offset. The binary representation of a is 01100001

Setbit sets the bit value at the offset, and changes the bit value to the original value

Stralgo, which does some special algorithms for strings, temporarily supports getting the longest common subsequence, and notice that subsequences are not substrings, substrings are continuous in the original string and subsequences don’t have to be continuous

The strlen command gets the string length


String underlying structure

The most common data type in Redis is String, and most programmers only use the String type in Redis. We said that Redis is written in C, but the String in Redis is not written in C. Why is this? Let’s go into the source code to look at the String type, first look at the schematic of the String type structure

The String type in Redis is implemented depending on a custom structure named SDS, SDS contains free(current available space size), LEN (current stored String length), buF [] (stored String content), the following is the source code of SDS

In fact, in the source code SDS structure is divided into five subdivision types. The reason there are five is so that strings of different lengths can use headers of different sizes. We can see that the len and alloc(free) types in different SDS are different, and they also correspond to different lengths. The flags field in the structure records the header type. By assigning different lengths to strings of different lengths to maximize space savings, we can see the ultimate pursuit of performance by the Redis team.


What are the benefits of using SDS?

  1. Reduce the number of memory allocations

We know that Redis is a kind of directly using the memory database, the data stored in the memory, when we are on a string type additional, two situations may occur: (1) the current remaining space (free) is big enough to accommodate additional content, we don’t need to allocate memory space, so that we can reduce the number of memory allocation. ② The current free space is not enough to accommodate the additional content, we need to re-apply memory space for it.

  1. Lazy free memory space

When we cut the string, Redis will put truncation part empty, only keep the rest, and not the immediate release of truncation part of memory space, the advantage is the next time again for the string additional content, if the current remaining space to accommodate additional content, don’t need to go to apply for space, to avoid the frequent memory applications. Unused space can be deleted periodically or lazily by Redis.

  1. Preventing buffer overflows

In C, if we do not know the position of ‘\0’ when concatenating an array of strings, we can cause a buffer overflow problem, but in Redis, we control the length of len, which avoids this problem

  1. Binary security

In C, the end of a string is determined by determining whether the current character is ‘\0’. In SDS, the end of a string is not considered as long as the traversal length does not reach len, even if ‘\0’ is encountered. Based on this, we can use Redis to cache binary forms such as images, audio, and compressed files.

Dig deep into the Redis string

The above SDS is only the structure of the string content stored in the string type. The strings in Redis can be stored in two ways, namely, EMbSTR and RAW. When the string length is very short, Redis uses EMbSTR to store the string, while when the string length exceeds 44, it needs to store the string in RAW. Below is a schematic of their complete string structure

All Redis objects have an object header structure that records the type, encoding, LRU,refcount, and PTR of the data.

Embstr is stored by placing the RedisObject header and SDS structure in contiguous spatial locations in memory, using the MALloc method to allocate once, whereas raw requires two mallocs to allocate space for the object header and SDS respectively.

To accommodate a full EMbSTR object, Jemalloc allocates at least 32 bytes, or 64 bytes if the string is longer, and embSTR has fixed fields that use 19 bytes and null-terminated strings. It takes up one byte, so embstr can only store strings of 44 bytes. If more than 44 bytes are stored, Redis uses RAW for storage.


String Expansion Policy

If the string size is smaller than 1MB, the system doubles the string size. If the string length is larger than 1MB, a maximum of 1MB is allocated each time to avoid space waste


Refer to the link

Mp.weixin.qq.com/s/vXBFscXqD…

Github.com/AobingJava/…

Blog.csdn.net/codejas/art…

Redis Deep Adventure


Welcome to scan the code to follow my wechat public number. I am Xinghai, and I look forward to your attention