Redis shouldn't just be the first sight, there should be follow-up

   Redis must be known to everyone, and it must be used. I will not repeat them here.

So let's talk about the source of Redis, what it is mainly used for, what are its advantages, etc.

1. What is Redis?

    Source: It is said that there was a young man from Sicily, Italy in 2008. When creating a website, I found that some of the operations were frequent due to the frequent read and write operations. At the beginning, Mysql was considered, but when the concurrency was high, I discovered how to optimize The effect of the database is not obvious, because the database is stored on the disk (the bottleneck is also caused by the read and write speed of the disk), so I decided to abandon the database (Mysql) and implement a list structure database by myself, but this data is not It is not stored on the disk, but on the memory. We all know that the read and write speed of the memory is very different from that of the disk. Because IC has also increased the speed of the database get/set, we are rewriting this memory by using the C language Database, because the data will not be saved when the memory service is down, so data persistence is also added. In this way, Redis was born in 2009. Redis (Remote DIctionary Service: remote dictionary service)

2. The positioning and characteristics of Redis

   Under normal circumstances, if data storage is needed, we generally consider traditional relational databases (such as: Mysql, SqlServer, Oracle, etc.)

  The characteristics of relational data: 1. Generally stored in the form of a table structure, two-dimensional mode 2. The storage is structured, and the data needs to have a fixed data structure to adapt to the table structure storage 3. There is a certain relationship between the tables ( Foreign keys, etc.) 4. Generally support related queries between complex tables such as Sql, but there are some differences in syntax 5. Support things. Disadvantages: 1. For capacity expansion, it supports vertical expansion, adding disks, does not support dynamic expansion, horizontal expansion support is relatively complex (techniques such as sub-database and table) 2. Table structure modification is more complex, data structure is relatively fixed 3. Read and write speed is relatively It will be slower, especially in high-speed read and write, disk I/O will be a certain bottleneck.

 Under such circumstances, No sql (non-relational) came into being. Its characteristics: 1. Store non-fixed structure, text, graphics, video, etc. 2. No relationship between tables 3. Guaranteed final consistency of data 4. Support massive data storage and ensure high concurrent reading Writing efficiency 5. Supports distributed, can store data in fragments, and expansion is relatively simple.

No sql There are various types of databases according to different storage types. KV structure: Redis, MemcacheDB, etc. Document: MongoDb, etc...

Today we mainly talk about Redis. The main features of Redis: 1. Rich data types (String, Hash, Set, List, etc.) 2. Stand-alone and distributed 3. Support persistence, data expiration strategy 4. Support multiple languages ​​5. , Cluster, high availability, etc.

Three, Redis installation and basic operations

   Installation: Redis installation, my last Redis installation has been explained, so I won’t repeat it ( Redis installation )

Basic operation 

1. Start: redis-server /usr/local/soft/redis-5.0.5/redis.conf

2. Select the database select 1 (there are 16 databases by default, 0,1,...15)

3. Clear the current database -> flushdb / flushall (clear all)

4、set  key value 、get  key

Specific command reference: http://redisdoc.com/index.html

Four, the basic data types of Redis

1. String character type

a. The character type should be regarded as the most commonly used type. set / get are operation commands for String type.

b. String types that can be stored include: string, integer, floating point

c. Syntax: set key value [expiration EX seconds|PX milliseconds][NX|XX] Refer to the official website for details

Set the expiration time, based on this we can set up distributed locks, automatically release locks and other operations

 d. Storage principle: Because Redis has a KV structure and is implemented through hashTable, there will be a dictEntry for each key-value pair. The following data structure

 The key is a string, and redis does not use the character array in C, but uses SDS, and the value is not stored as a string word, but stored in redisObject.

You can use the type command to view the external type -> type key

There are actually several encoding types according to the value inside the string.

1. int, which stores 8 bytes of long integer (long, 2^63-1).
2. embstr, which stands for SDS (Simple Dynamic String) in embstr format,
stores a string of less than 44 bytes.
3. Raw, which stores strings larger than 44 bytes (39 bytes before version 3.2)

What is SDS (SDS is actually divided into several types according to the length of storage, such as: sdshdr5(2^5)sdshdr8(2^8)...)

sds features: 1. If you need to dynamically expand the sds, you don’t have to worry about overflow. 2. The time complexity of calculating the length of characters is o(1). 3. Any type of character can be stored (such as: \0) 4. Judge len judge whether the character ends

Why does redis need to use SDS to store strings?

Because redis is implemented in C language, and in C, string storage is implemented through char[] array, and char[] has an end tag ('\0'), so if it is a string (audio, video, If there is (\0) in files such as pictures when converted to binary, then there will be a problem. Therefore, the need can only be realized by itself.

1. When using a character array, sufficient space must be allocated, otherwise it will overflow

2. If you need to calculate the length of the character, you need to traverse the array, and the time complexity is o(n).

3. The change of the character length of the array in C requires a new memory allocation

What is the difference between embstr and raw?

embstr will allocate 1 memory space, raw need to allocate twice the memory space. Therefore, compared with raw, the advantage of embstr is that one less space is allocated when it is created, one less space is released when it is deleted, and all the data of the object is connected together, which is convenient to find. The disadvantages of embstr are also obvious. If the length of the string increases and memory needs to be reallocated, the entire RedisObject and SDS need to reallocate space, so embstr in Redis is implemented as read-only . The embstr encoding method and the raw encoding method are separated by 39 bytes before version 3.0, that is, if the length of a string value is less than or equal to 39 bytes, it is encoded according to embstr, otherwise it is encoded according to raw. After the 3.2 version, it becomes 44 bytes as a boundary.

When are int and embstr converted to raw?

1. int, when the stored int is no longer an integer or exceeds the range of int (2^63-1), if the integer 1 is stored at the beginning, and the append character a. later, the storage will be modified to raw. There is also the beginning: embstr storage (read-only), and then append a value (need to be modified to raw). Then the memory will be reallocated, and the type will be converted to raw. So even if the string does not exceed 44. And the internal encoding conversion will not be reversed (only from small to large)

 Application scenarios: cache, distributed lock, global ID, IP current limit, etc...

2.Hash

hset h1 f 6
hset h1 e 5
hmset h1 a 1 b 2 c 3 d 4

Storage type: an unordered hash table containing key-value pairs. value can only be a string, other types cannot be nested

Scenarios where Hash is not suitable:
1. Field cannot set the expiration time separately
2. No bit operation
3. Data volume distribution needs to be considered (when the value is very large, it cannot be distributed to multiple nodes)

Store string, the main difference between Hash and String?
1. Gather all relevant values ​​into one key to save memory space
2. Use only one key to reduce key conflicts
3. When you need to obtain values ​​in batches, you only need to use one command to reduce memory/IO/CPU consumption

Obviously Hash is also a KV structure, a bit similar to HashMap. When storing the Hash data type, there are two data structures implemented at the bottom:

ziplist: OBJ_ENCODING_ZIPLIST (when the length of the key and value is less than 64 bytes, there is a compressed list structure storage. Compressed list -> special coded doubly linked list)
hashtable: OBJ_ENCODING_HT (when the number of key-value pairs is less than 512 bytes, hash table)

3. List list

The structure: can act as a queue and stack

Storage principle: In the early days when the amount of data was not large, ZipList was used. After version 3.2, quickLis was used uniformly (a doubly linked list is stored, and each node is a ZipList)

A QuickList is composed of multiple quicklistNodes, and each quicklistNode is composed of multiple ZipLists

4. Set collection ( unordered collection of String type, the maximum storage amount is 2^32-1 (about 4 billion) )

Add one or more elements (sadd myset ab c)

Storage implementation principle: Redis uses intset or hashtable to store set. If the elements are of integer type, use intset to store.
If it is not an integer type, use hashtable (array + linked list storage to store structure)

How does KV store the elements of set? The key is the value of the element, and the value is null.

Application scenario: random access to elements, mutual attention, likes, etc.

5. Zset collection (ordered)

Each element has a score (score). Generally, when the number of elements is less than 128 or the length of all members is less than 64 bytes, it is stored in ZipList, and the internal sorting is performed according to the two moves of score, otherwise it is passed through the skip list (skipList + dict) storage

What is a skip list (skipList)?

Principle: When a new value is added, the level (let's understand as the number of levels) of the value is calculated. Each level is a linked list. Through the calculated level, which level starts to compare.

1. For example; add 23, the calculated level is 2, then start the comparison from the second level

  23 compare with 7 -> compare with greater than 19 -> compare with greater than 26

2. Then the next level, compare 22-> greater than, then it is between 22 and 26

Wait, there are other types, follow up... I still have to thank the gupao teacher for helping me understand these contents.

If the above content is improperly described, please refer to the officials and gods..

Guess you like

Origin blog.csdn.net/u010200793/article/details/104863334
Recommended