preface

Hello, long time no see, and for a period of time. Most of my colleagues left, but the job was still the same, so I had to stick to the top of my head. Now that I’ve finally taken a break, I’ve decided to open the Redis source code series. I hope I don’t slap you in the face.



What is Redis?

Redis is a NoSQL database based on key-value pairs. The main data types include String, hash, list, set, Zset, bitmaps, GEO and so on.

Note: If the interview asks Redis about data types, the first five are basic and must be answered correctly, and the last two are highlights.

The advantage of redis

1. Data is stored in memory, which is fast to read and write. If persistence is required, you can enable the configuration to periodically store data on hard disks.

2. Use a single thread to avoid thread contention and consumption of context switching between threads.

3. Replication can be performed between the primary and secondary nodes for horizontal expansion to overcome the limitation of single-node deployment.

4. Provide sentinels and clusters to ensure high availability of the entire service when a node fails.

Note: the advantages of the above will be single chapter to discuss, now have a general impression on the line, do not need to memorize 😂.

Redis basic installation

What speak above is space, the eye said to remember, the brain said to have no 🤣. Let’s go ahead and install redis on Linux and see how it works. As for Windows is relatively simple, belong to out of the box, here will not write.

1. Download the compressed package

Go to the website to download the download. Redis. IO/releases/installation package, download redis4 advice is. Then look at the underlying structure also using Redis4.



Redis4 and REDIS3

1. In redis4, the underlying structure of type string for different length of string into sdshdr5, sdshdr8, sdshdr16, sdshdr32, a variety of structures such as sdshdr64, more to save memory, and redis3 USES only int, embstr, raw.

2. In Redis4, persistence can use a mixture of RDB and AOF.

2. Upload the file to the server and decompress the file

Use the remote management tool to copy the compressed package to the Linux server and decompress the package



3. Go to the SRC directory and run make install



4. Enable the background startup configuration

Conf file and change daemonize to yes so it can run in the background.



6. Start

Start Redis using redis-server. Note that port number is 6379.



7. Connect to the client

Go to the SRC directory and use redis-cli to connect to the client.



Redis commonly used API

Type string

1. Set the value set [key] [value] [expried time] [nx | xx]

The value is value and the expiration time is Expried time. Nx indicates that the configuration is successful only when the nX does not exist. Xx indicates that the configuration is successful only when it exists.

We first set hello to the STR variable, and then tried to update STR with nx, but found that the update failed because nx indicates that STR does not exist, which it does. Finally, xx was used to update, and it was found that the update was successful.



2. Obtain the value get key



Mset [key value] [key value] [key value]

The data is divided according to group setting, and each key corresponds to a value. When the setting is successful, the query finds that it is garbled, not the Chinese we set.



In fact, this is not garbled, but the client display problem, we just need to start the client with — RAW, specify the display format.



Mget [key] [key] [key]



Hash type

1. Set hset key field value



2. Obtain the value hget key field

Hmset key [field] [value] [field] [value]



Hmget key [field] [field] [field]



5. Obtain all the fields hkeys of a key



List the type

1. Insert new data lpush key value from the left side of the list

Insert elements A, B, and c to the left of the list, returning the length 3 of the current list.



2. Insert new data rpush key value from the right of list

Insert two elements d, e to the right of the list, return the length 5 of the current list.



3. Query all elements of the list

Note the order in which the three elements are inserted to the left. Lpush inserts new elements to the head of the list each time, so the order is C, B, and A.



4. Obtain the llen key of the list length



5. Delete the element LPOP key from the left

Deletes the first element of the linked list, returning the value of the deleted element c. Let’s look at the current elements of list, b, A, D, e.



6. Delete the rPOP key from the right

As with the deleted element from the left above, the return value is the deleted element.



7. Delete the specified lREM key count value

Delete the specified element, with emphasis on count, where count is divided into three cases:

1). When count>0, delete at most count elements from left to right.

2). When count<0, delete at most count elements from right to left.

3). When count=0, delete all elements that meet the condition.



The set type

Sadd key[field] [field] [field]

Insert three strings into myset1, namely hello, world, and Java, and return the value of successful insertion.

Srem key [field] [field]



3. Calculate the number of elements scard key



Sismember key [field]

The hello element in myset was removed earlier, so the return value of the first statement is 0, indicating that it does not exist. World is not deleted, so the return value is 1, indicating that it exists.



5. Get all sisMembers keys



Sunion [key] [key]

We first look at the data for myset1 and myset2, and then use sunion to get their union.



Sinter [key] [key]



Sdiff [key] [key]

Notice the difference between the key here, if it’s sdiff myset1 myset2, that means myset1 minus the intersection of the two, so it’s world and cc.



And vice versa.



Zset type

Zadd key [score member] [score member]



2. Calculate the length zcard key



Zrevrank key member(zrevrank key member)

Myzset in a total of three students, Zhang SAN, Li Si, Wang Wu, their scores were 80 points, 90 points, 70 points, that their sorting is the first li Si 90 points, the second Zhang SAN 80 points, the third Wang Wu 70 points.

Zrank represents a positive rank and returns a subscript value. Zrevrank represents the inverse ranking, and the return value is also a subscript.



conclusion

This is an introduction to Redis, mainly from what Redis is, what benefits, how to quickly install on Linux, common data structures and API use. The emphasis is to start with the population, get a general impression of it, and understand the difference between it and a relational database. As for the applicable scenarios of some common data types, the underlying design biases us below.

If you feel that writing is also good, trouble to give a praise 👍, your recognition is the power of my writing!

If you think something is wrong, please feel free to comment.

All right, bye.

And a focus on

The resources

Redis Chinese storage garble problem

Redis development and operation

Redis Deep Adventures: Core Principles and Applied Practices