Learning diary [Redis]

Redis

1. Purchase and arrange ECS

(https://i.loli.net/2021/06/22/j8ReENFV5G3mtyq.png)]

2. Download and install redis

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-uqcogsbr-1624360166546)( https://i.loli.net/2021/06/22/4sDk5AdctF6fOLr.png )]

linux Installation Tips

Move the installation package to the / opt directory and extract it for installation

3. Enter the extracted file

make

Default path for redis installation: / usr/local/bin

3.1. Create myconf directory

3.2. Modify redis.com in myconf directory Conf file

3.3. Log in to redis

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG awkiulzh-1624360166558)( https://i.loli.net/2021/06/22/tHGJgp2OlbFMU7B.png )]

3.4. Close redis shutdown

4. Redis benchmark test performance

Redis benchmark: its own stress testing tool

Test command:

4.1. Start the redis server first

4.2 pressure test

redis-benchmark -h localhost -p 6379 -c 100 -n 100000

I can see that the student machine is still very fierce.

5. Basic knowledge

redis has 16 databases by default

The 0 used by default;

16 databases: DB 0~DB 15
DB 0 is used by default. You can use select n to switch to DB n. dbsize can view the size of the current database, which is related to the number of key s.

127.0.0.1:6379> config get databases # View the number of databases on the command line
1) "databases"
2) "16"

127.0.0.1:6379> select 8 # Switch database DB 8
OK
127.0.0.1:6379[8]> dbsize # View database size
(integer) 0

# Data cannot be exchanged between different databases, and dbsize is based on the number of key s in the database.
127.0.0.1:6379> set name sakura 
OK
127.0.0.1:6379> SELECT 8
OK
127.0.0.1:6379[8]> get name # Key value pairs in db0 cannot be obtained in db8.
(nil)
127.0.0.1:6379[8]> DBSIZE
(integer) 0
127.0.0.1:6379[8]> SELECT 0
OK
127.0.0.1:6379> keys *
1) "counter:__rand_int__"
2) "mylist"
3) "name"
4) "key:__rand_int__"
5) "myset:__rand_int__"
127.0.0.1:6379> DBSIZE # size is related to the number of key s
(integer) 5

keys *: view all key s in the current database.

Flush DB: clear the key value pairs in the current database.

Flush all: clear key value pairs of all databases.

Redis is single threaded, and redis is based on memory operation.

Therefore, the performance bottleneck of Redis is not CPU, but machine memory and network bandwidth.

So why is Redis so fast and high-performance? QPS up to 10W+

Why is Redis single thread so fast?

Myth 1: a high-performance server must be multi-threaded?
Myth 2: multithreading (CPU context will switch!) It must be more efficient than single thread!

Core: Redis puts all data in memory, so using single thread to operate is the most efficient. Multithreading (CPU context switching: time-consuming operation!), For the memory system, if there is no context switching, the efficiency is the highest. Multiple reads and writes are on one CPU. In the case of storing data in memory, single thread is the best solution.

6. Five data types

Redis is an open source (BSD licensed) data structure server with in memory storage, which can be used as database, cache and message queue agent. It supports string, hash table, list, set, ordered set, bitmap, hyperlogs and other data types. Built in replication, Lua script, LRU retraction, transaction and disk persistence at different levels. At the same time, it provides high availability through Redis Sentinel and automatic partition through Redis Cluster.

Redis-key

Any data type in redis is saved in the form of key value in the database. The operation of redis key is used to complete the operation of data in the database.

Learn the following commands:

  • exists key: determines whether the key exists
  • del key: delete key value pairs
  • move key db: move the key value pair to the specified database
  • expire key second: sets the expiration time of the key value pair
  • type key: view the data type of value
127.0.0.1:6379> keys * # View all key s in the current database
(empty list or set)
127.0.0.1:6379> set name qinjiang # set key
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> move age 1 # Move key value pairs to the specified database
(integer) 1
127.0.0.1:6379> EXISTS age # Determine whether the key exists
(integer) 0 # non-existent
127.0.0.1:6379> EXISTS name
(integer) 1 # existence
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379[1]> del age # Delete key value pair
(integer) 1 # Number of deleted


127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> EXPIRE age 15 # Sets the expiration time of key value pairs

(integer) 1 # Set success start count
127.0.0.1:6379> ttl age # View the remaining expiration time of the key
(integer) 13
127.0.0.1:6379> ttl age
(integer) 11
127.0.0.1:6379> ttl age
(integer) 9
127.0.0.1:6379> ttl age
(integer) -2 # -2 means that the key is expired, - 1 means that the expiration time is not set for the key

127.0.0.1:6379> get age # Expired key s will be automatically delete d
(nil)
127.0.0.1:6379> keys *
1) "name"

127.0.0.1:6379> type name # View the data type of value
string

About TTL commands

Redis's key returns the expiration time of the key through the TTL command. Generally speaking, there are three types:

The current key has no expiration time set, so - 1 will be returned
The current key has a set expiration time, and the key has expired, so - 2 will be returned
The current key has a set expiration time, and the key has not expired yet, so the normal remaining time of the key will be returned
About renaming RENAME and RENAMENX

RENAME key newkey modify the name of the key
RENAMENX key newkey renames the key to newkey only if the newkey does not exist.
Learn more commands: https://www.redis.net.cn/order/

String (string)

Ordinary set and get are skipped directly.

[external chain picture transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ybnafmvb-1624360166566)( https://i.loli.net/2021/06/22/CRr68XWdFZa3vmy.png )]

String similar usage scenario: value can be either a string or a number. For example:

  • Counter
  • Count the quantity of multiple units: uid:123666: follow 0
  • Number of fans
  • Object storage cache

List

Redis list is a simple string list, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list

A list can contain up to 232 - 1 elements (4294967295, with more than 4 billion elements per list).

Keywords: Database Redis

Added by Otoom on Thu, 27 Jan 2022 18:43:57 +0200